Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -22,25 +22,27 @@ 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 |
processed_entities = []
|
| 28 |
seen = set()
|
| 29 |
|
| 30 |
for ent in doc.ents:
|
| 31 |
raw_text = ent.text.strip()
|
| 32 |
e_lower = raw_text.lower()
|
| 33 |
-
words = e_lower.split()
|
| 34 |
-
|
| 35 |
-
# --- NEW BUG FILTERING LOGIC ---
|
| 36 |
-
# 1. Remove "Repetition Bugs" (like Social Media Social)
|
| 37 |
-
if len(words) > 1 and words[-1] == words[0]:
|
| 38 |
-
continue
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
if
|
| 42 |
continue
|
| 43 |
|
|
|
|
| 44 |
sub_items = [raw_text]
|
| 45 |
if e_lower not in brand_safety:
|
| 46 |
if " and " in e_lower or " & " in raw_text:
|
|
@@ -53,18 +55,25 @@ async def analyze_text(payload: TextPayload):
|
|
| 53 |
if f_lower in seen or len(final_text) < 2:
|
| 54 |
continue
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
label = ent.label_
|
| 57 |
|
| 58 |
-
# Category
|
| 59 |
concepts = ['privacy', 'cookies', 'cookie', 'booster', 'get', 'noticed', 'free', 'social media']
|
| 60 |
-
tech_items = ['exif', 'mediaif', 'cookiesif', 'json', 'api', 'schema', 'vps', 'server']
|
| 61 |
|
| 62 |
if any(c in f_lower for c in concepts):
|
| 63 |
label = "CONCEPT"
|
| 64 |
elif any(t in f_lower for t in tech_items):
|
| 65 |
label = "PRODUCT"
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
#
|
| 68 |
if len(final_text.split()) >= 4:
|
| 69 |
continue
|
| 70 |
|
|
@@ -76,6 +85,7 @@ async def analyze_text(payload: TextPayload):
|
|
| 76 |
})
|
| 77 |
seen.add(f_lower)
|
| 78 |
|
|
|
|
| 79 |
sorted_entities = sorted(processed_entities, key=lambda x: x['frequency'], reverse=True)
|
| 80 |
|
| 81 |
return {
|
|
|
|
| 22 |
doc = nlp(payload.text)
|
| 23 |
entity_freq = Counter([ent.text.lower() for ent in doc.ents])
|
| 24 |
|
| 25 |
+
# Brands with '&' or 'and' that should NOT be split
|
| 26 |
+
brand_safety = [
|
| 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()
|
| 36 |
|
| 37 |
for ent in doc.ents:
|
| 38 |
raw_text = ent.text.strip()
|
| 39 |
e_lower = raw_text.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# 1. Filter out pure numbers, "zero", and UI trash
|
| 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 |
if f_lower in seen or len(final_text) < 2:
|
| 56 |
continue
|
| 57 |
|
| 58 |
+
# 3. Bug Filter: Avoid repetition bugs like "Social Media Social"
|
| 59 |
+
words = f_lower.split()
|
| 60 |
+
if len(words) > 1 and words[-1] == words[0]:
|
| 61 |
+
continue
|
| 62 |
+
|
| 63 |
label = ent.label_
|
| 64 |
|
| 65 |
+
# 4. Category Force Mapping
|
| 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 in f_lower for c in concepts):
|
| 70 |
label = "CONCEPT"
|
| 71 |
elif any(t in f_lower for t in tech_items):
|
| 72 |
label = "PRODUCT"
|
| 73 |
+
elif any(brand in f_lower for brand in brand_safety):
|
| 74 |
+
label = "ORG"
|
| 75 |
|
| 76 |
+
# 5. Length Filter (Max 3 words for entities)
|
| 77 |
if len(final_text.split()) >= 4:
|
| 78 |
continue
|
| 79 |
|
|
|
|
| 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 {
|