Spaces:
Sleeping
Sleeping
Commit ·
962e2af
1
Parent(s): 5684c35
Fix: NER 3.2.1
Browse files- app/api/enrichment.py +15 -7
app/api/enrichment.py
CHANGED
|
@@ -424,7 +424,7 @@ def query_hf_ner(text: str) -> List[Dict]:
|
|
| 424 |
for attempt in range(3):
|
| 425 |
try:
|
| 426 |
payload = {
|
| 427 |
-
"inputs": text[:
|
| 428 |
"parameters": {"aggregation_strategy": "simple"},
|
| 429 |
"options": {"wait_for_model": True}
|
| 430 |
}
|
|
@@ -448,14 +448,16 @@ def query_hf_ner(text: str) -> List[Dict]:
|
|
| 448 |
return []
|
| 449 |
|
| 450 |
def run_regex_heuristic(text: str) -> List[str]:
|
|
|
|
| 451 |
keywords = ["السيد", "السيدة", "رئيس", "عضو", "نائب"]
|
| 452 |
found_names = []
|
| 453 |
for kw in keywords:
|
| 454 |
-
|
|
|
|
| 455 |
matches = re.findall(pattern, text)
|
| 456 |
for m in matches:
|
| 457 |
clean_m = m.replace('.', ' ').strip()
|
| 458 |
-
if len(clean_m) >
|
| 459 |
found_names.append(clean_m)
|
| 460 |
return found_names
|
| 461 |
|
|
@@ -478,15 +480,21 @@ def extract_names_from_text(text: str, ner_results: List[dict] = None) -> List[d
|
|
| 478 |
label = entity.get('entity_group') or entity.get('entity', '')
|
| 479 |
if any(p in label.upper() for p in person_labels):
|
| 480 |
word = entity.get('word', '').replace('##', '').replace(' ', ' ').strip()
|
| 481 |
-
if len(word) >
|
| 482 |
candidates.append(word)
|
|
|
|
|
|
|
| 483 |
|
| 484 |
# 3. Heuristique Globale (Si NER insuffisant)
|
| 485 |
if not candidates or len(candidates) < 2:
|
| 486 |
-
|
| 487 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 488 |
|
| 489 |
-
clean_candidates = list(set([n.strip() for n in candidates if len(n.strip()) >
|
| 490 |
|
| 491 |
# 4. Moteur de Rôles Flou (Fuzzy Logic ±10 words)
|
| 492 |
roles_keywords = {
|
|
|
|
| 424 |
for attempt in range(3):
|
| 425 |
try:
|
| 426 |
payload = {
|
| 427 |
+
"inputs": text[:4000],
|
| 428 |
"parameters": {"aggregation_strategy": "simple"},
|
| 429 |
"options": {"wait_for_model": True}
|
| 430 |
}
|
|
|
|
| 448 |
return []
|
| 449 |
|
| 450 |
def run_regex_heuristic(text: str) -> List[str]:
|
| 451 |
+
"""Extrait les noms via regex sur un texte donné (permet le support Bidi)."""
|
| 452 |
keywords = ["السيد", "السيدة", "رئيس", "عضو", "نائب"]
|
| 453 |
found_names = []
|
| 454 |
for kw in keywords:
|
| 455 |
+
# Accepte les noms plus longs (jusqu'à 3 mots) et plus courts (min 3 chars)
|
| 456 |
+
pattern = rf"{kw}[\s\.]*([\u0621-\u064A]+(?:[\s\.]+[\u0621-\u064A]+){{1,3}})"
|
| 457 |
matches = re.findall(pattern, text)
|
| 458 |
for m in matches:
|
| 459 |
clean_m = m.replace('.', ' ').strip()
|
| 460 |
+
if len(clean_m) >= 3:
|
| 461 |
found_names.append(clean_m)
|
| 462 |
return found_names
|
| 463 |
|
|
|
|
| 480 |
label = entity.get('entity_group') or entity.get('entity', '')
|
| 481 |
if any(p in label.upper() for p in person_labels):
|
| 482 |
word = entity.get('word', '').replace('##', '').replace(' ', ' ').strip()
|
| 483 |
+
if len(word) >= 3:
|
| 484 |
candidates.append(word)
|
| 485 |
+
if candidates:
|
| 486 |
+
print(f"DEBUG: NER Candidates found: {candidates}")
|
| 487 |
|
| 488 |
# 3. Heuristique Globale (Si NER insuffisant)
|
| 489 |
if not candidates or len(candidates) < 2:
|
| 490 |
+
regex_hits = run_regex_heuristic(text_norm)
|
| 491 |
+
if not regex_hits:
|
| 492 |
+
regex_hits = run_regex_heuristic(text_norm[::-1])
|
| 493 |
+
candidates += regex_hits
|
| 494 |
+
if regex_hits:
|
| 495 |
+
print(f"DEBUG: Regex Heuristic hits: {regex_hits}")
|
| 496 |
|
| 497 |
+
clean_candidates = list(set([n.strip() for n in candidates if len(n.strip()) >= 3]))
|
| 498 |
|
| 499 |
# 4. Moteur de Rôles Flou (Fuzzy Logic ±10 words)
|
| 500 |
roles_keywords = {
|