Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -22,14 +22,8 @@ async def analyze_text(payload: TextPayload):
|
|
| 22 |
doc = nlp(payload.text)
|
| 23 |
entity_freq = Counter([ent.text.lower() for ent in doc.ents])
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
'johnson & johnson', 'ben & jerry', 'tiffany & co', 'h&m', 'at&t',
|
| 28 |
-
'dolce & gabbana', 'marks & spencer', 'bed bath & beyond'
|
| 29 |
-
]
|
| 30 |
-
|
| 31 |
-
# UI Noise and Number Words to ignore
|
| 32 |
-
trash_words = ['zero', 'one', 'two', 'three', 'privacy & cookie', 'accept decline']
|
| 33 |
|
| 34 |
processed_entities = []
|
| 35 |
seen = set()
|
|
@@ -38,11 +32,10 @@ async def analyze_text(payload: TextPayload):
|
|
| 38 |
raw_text = ent.text.strip()
|
| 39 |
e_lower = raw_text.lower()
|
| 40 |
|
| 41 |
-
# 1. Filter out
|
| 42 |
if e_lower in trash_words or raw_text.isdigit():
|
| 43 |
continue
|
| 44 |
|
| 45 |
-
# 2. Smart Splitting Logic (Bing and Yandex -> Bing, Yandex)
|
| 46 |
sub_items = [raw_text]
|
| 47 |
if e_lower not in brand_safety:
|
| 48 |
if " and " in e_lower or " & " in raw_text:
|
|
@@ -55,25 +48,26 @@ async def analyze_text(payload: TextPayload):
|
|
| 55 |
if f_lower in seen or len(final_text) < 2:
|
| 56 |
continue
|
| 57 |
|
| 58 |
-
#
|
| 59 |
words = f_lower.split()
|
| 60 |
if len(words) > 1 and words[-1] == words[0]:
|
| 61 |
continue
|
| 62 |
|
| 63 |
label = ent.label_
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
concepts = ['privacy', 'cookies', 'cookie', 'booster', 'get', 'noticed', 'free', 'social media']
|
| 67 |
tech_items = ['exif', 'mediaif', 'cookiesif', 'json', 'api', 'schema', 'vps', 'server', 'gravatar']
|
| 68 |
|
| 69 |
-
if any(c
|
| 70 |
label = "CONCEPT"
|
| 71 |
elif any(t in f_lower for t in tech_items):
|
| 72 |
label = "PRODUCT"
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
# 5. Length Filter (Max 3 words for entities)
|
| 77 |
if len(final_text.split()) >= 4:
|
| 78 |
continue
|
| 79 |
|
|
@@ -85,10 +79,5 @@ async def analyze_text(payload: TextPayload):
|
|
| 85 |
})
|
| 86 |
seen.add(f_lower)
|
| 87 |
|
| 88 |
-
# Return up to 50 results
|
| 89 |
sorted_entities = sorted(processed_entities, key=lambda x: x['frequency'], reverse=True)
|
| 90 |
-
|
| 91 |
-
return {
|
| 92 |
-
"status": "success",
|
| 93 |
-
"entities": sorted_entities[:50]
|
| 94 |
-
}
|
|
|
|
| 22 |
doc = nlp(payload.text)
|
| 23 |
entity_freq = Counter([ent.text.lower() for ent in doc.ents])
|
| 24 |
|
| 25 |
+
brand_safety = ['johnson & johnson', 'at&t', 'h&m']
|
| 26 |
+
trash_words = ['zero', 'one', 'two', 'three', 'privacy & cookie', 'accept decline', 'ip']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
processed_entities = []
|
| 29 |
seen = set()
|
|
|
|
| 32 |
raw_text = ent.text.strip()
|
| 33 |
e_lower = raw_text.lower()
|
| 34 |
|
| 35 |
+
# 1. Filter out trash, numbers, and very short 2-letter acronyms from being ORGs
|
| 36 |
if e_lower in trash_words or raw_text.isdigit():
|
| 37 |
continue
|
| 38 |
|
|
|
|
| 39 |
sub_items = [raw_text]
|
| 40 |
if e_lower not in brand_safety:
|
| 41 |
if " and " in e_lower or " & " in raw_text:
|
|
|
|
| 48 |
if f_lower in seen or len(final_text) < 2:
|
| 49 |
continue
|
| 50 |
|
| 51 |
+
# Repetition filter
|
| 52 |
words = f_lower.split()
|
| 53 |
if len(words) > 1 and words[-1] == words[0]:
|
| 54 |
continue
|
| 55 |
|
| 56 |
label = ent.label_
|
| 57 |
|
| 58 |
+
# Force Category logic
|
| 59 |
+
concepts = ['privacy', 'cookies', 'cookie', 'booster', 'get', 'noticed', 'free', 'social media', 'ip']
|
| 60 |
tech_items = ['exif', 'mediaif', 'cookiesif', 'json', 'api', 'schema', 'vps', 'server', 'gravatar']
|
| 61 |
|
| 62 |
+
if any(c == f_lower for c in concepts): # Direct match for short words like IP
|
| 63 |
label = "CONCEPT"
|
| 64 |
elif any(t in f_lower for t in tech_items):
|
| 65 |
label = "PRODUCT"
|
| 66 |
+
|
| 67 |
+
# Final check: 2-letter words like 'IP' should almost never be an 'ORG'
|
| 68 |
+
if len(final_text) <= 2 and label == "ORG":
|
| 69 |
+
label = "CONCEPT"
|
| 70 |
|
|
|
|
| 71 |
if len(final_text.split()) >= 4:
|
| 72 |
continue
|
| 73 |
|
|
|
|
| 79 |
})
|
| 80 |
seen.add(f_lower)
|
| 81 |
|
|
|
|
| 82 |
sorted_entities = sorted(processed_entities, key=lambda x: x['frequency'], reverse=True)
|
| 83 |
+
return {"status": "success", "entities": sorted_entities[:50]}
|
|
|
|
|
|
|
|
|
|
|
|