Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -77,7 +77,6 @@ def detect_entities(text):
|
|
| 77 |
while i < len(tokens):
|
| 78 |
token = tokens[i]
|
| 79 |
|
| 80 |
-
# Sentence boundary
|
| 81 |
if token in [".", "!", "?"]:
|
| 82 |
sentence_start = True
|
| 83 |
i += 1
|
|
@@ -87,74 +86,31 @@ def detect_entities(text):
|
|
| 87 |
i += 1
|
| 88 |
continue
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
# RULE 1 — FIRST WORD OF SENTENCE (FIXED)
|
| 92 |
-
# ====================================================
|
| 93 |
if sentence_start:
|
| 94 |
-
# 1️⃣ Dictionary has highest priority
|
| 95 |
match = find_dictionary_match(tokens, i)
|
| 96 |
if match:
|
| 97 |
-
entities.append({
|
| 98 |
-
"text": match["text"],
|
| 99 |
-
"type": match["type"]
|
| 100 |
-
})
|
| 101 |
i += match["length"]
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
if token[0].isupper():
|
| 107 |
-
phrase = [token]
|
| 108 |
-
j = i + 1
|
| 109 |
-
|
| 110 |
-
while (
|
| 111 |
-
j < len(tokens)
|
| 112 |
-
and tokens[j].isalpha()
|
| 113 |
-
and tokens[j][0].isupper()
|
| 114 |
-
):
|
| 115 |
-
phrase.append(tokens[j])
|
| 116 |
-
j += 1
|
| 117 |
-
|
| 118 |
-
phrase_text = " ".join(phrase)
|
| 119 |
-
|
| 120 |
-
# Capitalized first phrase not in vocab → UNKNOWN
|
| 121 |
-
if phrase_text.lower() not in VOCAB:
|
| 122 |
-
entities.append({
|
| 123 |
-
"text": phrase_text,
|
| 124 |
-
"type": "UNKNOWN"
|
| 125 |
-
})
|
| 126 |
-
|
| 127 |
-
i = j
|
| 128 |
-
sentence_start = False
|
| 129 |
-
continue
|
| 130 |
|
| 131 |
-
# 3️⃣ Lowercase first word → leave untouched
|
| 132 |
-
i += 1
|
| 133 |
sentence_start = False
|
| 134 |
continue
|
| 135 |
|
| 136 |
-
#
|
| 137 |
-
# RULE 2 — SECOND WORD ONWARDS (UNCHANGED)
|
| 138 |
-
# ====================================================
|
| 139 |
match = find_dictionary_match(tokens, i)
|
| 140 |
if match:
|
| 141 |
-
entities.append({
|
| 142 |
-
"text": match["text"],
|
| 143 |
-
"type": match["type"]
|
| 144 |
-
})
|
| 145 |
i += match["length"]
|
| 146 |
continue
|
| 147 |
|
| 148 |
-
# Capital phrase grouping (e.g. Shopping Mall)
|
| 149 |
if token[0].isupper():
|
| 150 |
phrase = [token]
|
| 151 |
j = i + 1
|
| 152 |
-
|
| 153 |
-
while (
|
| 154 |
-
j < len(tokens)
|
| 155 |
-
and tokens[j].isalpha()
|
| 156 |
-
and tokens[j][0].isupper()
|
| 157 |
-
):
|
| 158 |
phrase.append(tokens[j])
|
| 159 |
j += 1
|
| 160 |
|
|
@@ -162,14 +118,11 @@ def detect_entities(text):
|
|
| 162 |
"text": " ".join(phrase),
|
| 163 |
"type": "UNKNOWN"
|
| 164 |
})
|
| 165 |
-
|
| 166 |
i = j
|
| 167 |
continue
|
| 168 |
|
| 169 |
-
# Normal word
|
| 170 |
i += 1
|
| 171 |
|
| 172 |
-
return entities
|
| 173 |
|
| 174 |
|
| 175 |
# ====================================================
|
|
|
|
| 77 |
while i < len(tokens):
|
| 78 |
token = tokens[i]
|
| 79 |
|
|
|
|
| 80 |
if token in [".", "!", "?"]:
|
| 81 |
sentence_start = True
|
| 82 |
i += 1
|
|
|
|
| 86 |
i += 1
|
| 87 |
continue
|
| 88 |
|
| 89 |
+
# RULE 1 — FIRST WORD
|
|
|
|
|
|
|
| 90 |
if sentence_start:
|
|
|
|
| 91 |
match = find_dictionary_match(tokens, i)
|
| 92 |
if match:
|
| 93 |
+
entities.append({"text": match["text"], "type": match["type"]})
|
|
|
|
|
|
|
|
|
|
| 94 |
i += match["length"]
|
| 95 |
+
else:
|
| 96 |
+
if token.lower() not in VOCAB:
|
| 97 |
+
entities.append({"text": token, "type": "UNKNOWN"})
|
| 98 |
+
i += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
|
|
|
|
|
|
| 100 |
sentence_start = False
|
| 101 |
continue
|
| 102 |
|
| 103 |
+
# RULE 2 — SECOND WORD ONWARDS
|
|
|
|
|
|
|
| 104 |
match = find_dictionary_match(tokens, i)
|
| 105 |
if match:
|
| 106 |
+
entities.append({"text": match["text"], "type": match["type"]})
|
|
|
|
|
|
|
|
|
|
| 107 |
i += match["length"]
|
| 108 |
continue
|
| 109 |
|
|
|
|
| 110 |
if token[0].isupper():
|
| 111 |
phrase = [token]
|
| 112 |
j = i + 1
|
| 113 |
+
while j < len(tokens) and tokens[j].isalpha() and tokens[j][0].isupper():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
phrase.append(tokens[j])
|
| 115 |
j += 1
|
| 116 |
|
|
|
|
| 118 |
"text": " ".join(phrase),
|
| 119 |
"type": "UNKNOWN"
|
| 120 |
})
|
|
|
|
| 121 |
i = j
|
| 122 |
continue
|
| 123 |
|
|
|
|
| 124 |
i += 1
|
| 125 |
|
|
|
|
| 126 |
|
| 127 |
|
| 128 |
# ====================================================
|