habulaj commited on
Commit
f3ca73d
·
1 Parent(s): 96376b2

Update routers/profanity.py

Browse files
Files changed (1) hide show
  1. routers/profanity.py +36 -43
routers/profanity.py CHANGED
@@ -1,53 +1,46 @@
1
  # routers/profanity.py
2
 
3
- from fastapi import APIRouter, Query
4
  from better_profanity import profanity
5
 
6
- # Cria um roteador para a API de palavrões
7
  router = APIRouter()
8
 
9
- # Define a rota para verificar o texto
10
  @router.get("/profanity/check/")
11
  def check_profanity(text: str = Query(..., description="Text to be checked for profanity")):
12
  """
13
- Checks if the text contains profanity and returns details in English.
14
  """
15
- # Load default profanity words
16
- profanity.load_censor_words()
17
-
18
- # Detects profanity
19
- contains_profanity = profanity.contains_profanity(text)
20
-
21
- # Censor the text
22
- censored_text = profanity.censor(text)
23
-
24
- # Split text into words
25
- words = text.split()
26
- total_word_count = len(words)
27
-
28
- # Identify offensive words
29
- offensive_words = [
30
- word for word in words
31
- if profanity.contains_profanity(word)
32
- ]
33
- offensive_word_count = len(offensive_words)
34
-
35
- # Calculate offensive percentage
36
- offensive_percentage = (offensive_word_count / total_word_count * 100) if total_word_count > 0 else 0
37
-
38
- # Determine severity level
39
- if offensive_percentage == 0:
40
- severity = "mild"
41
- elif offensive_percentage <= 30:
42
- severity = "moderate"
43
- else:
44
- severity = "severe"
45
-
46
- return {
47
- "contains_profanity": contains_profanity,
48
- "offensive_words": offensive_words,
49
- "offensive_word_count": offensive_word_count,
50
- "total_word_count": total_word_count,
51
- "offensive_percentage": round(offensive_percentage, 2),
52
- "severity": severity
53
- }
 
1
  # routers/profanity.py
2
 
3
+ from fastapi import APIRouter, Query, HTTPException
4
  from better_profanity import profanity
5
 
6
+ # Create a router for the profanity API
7
  router = APIRouter()
8
 
 
9
  @router.get("/profanity/check/")
10
  def check_profanity(text: str = Query(..., description="Text to be checked for profanity")):
11
  """
12
+ Check if the text contains profanity and return the percentage of offensive words.
13
  """
14
+ try:
15
+ # Validate input
16
+ if not text.strip():
17
+ raise HTTPException(status_code=400, detail="The text must not be empty.")
18
+
19
+ # Load default dictionary of offensive words
20
+ profanity.load_censor_words()
21
+
22
+ # Split the text into words
23
+ words = text.split()
24
+ total_word_count = len(words)
25
+
26
+ # Validate word count
27
+ if total_word_count == 0:
28
+ raise HTTPException(status_code=400, detail="The text must contain at least one word.")
29
+
30
+ # Identify offensive words
31
+ offensive_words = [
32
+ word for word in words if profanity.contains_profanity(word)
33
+ ]
34
+ offensive_word_count = len(offensive_words)
35
+
36
+ # Calculate the percentage of offensive words
37
+ offensive_percentage = (offensive_word_count / total_word_count * 100)
38
+
39
+ # Return the percentage rounded to 2 decimal places
40
+ return {
41
+ "offensive_percentage": round(offensive_percentage, 2)
42
+ }
43
+
44
+ except Exception as e:
45
+ # Catch unexpected errors and return a generic error message
46
+ raise HTTPException(status_code=500, detail="An unexpected error occurred.") from e