Spaces:
Sleeping
Sleeping
Update ner_tool.py
Browse files- ner_tool.py +24 -11
ner_tool.py
CHANGED
@@ -16,8 +16,12 @@ class NamedEntityRecognitionTool(Tool):
|
|
16 |
# Perform named entity recognition on the input text
|
17 |
entities = ner_analyzer(text)
|
18 |
|
19 |
-
# Prepare a list to store
|
20 |
-
|
|
|
|
|
|
|
|
|
21 |
|
22 |
for entity in entities:
|
23 |
label = entity.get("entity", "UNKNOWN")
|
@@ -30,15 +34,24 @@ class NamedEntityRecognitionTool(Tool):
|
|
30 |
|
31 |
# Check for multi-token entities
|
32 |
if "##" in word:
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
token_entities.append({"token": sub_token, "label": label, "entity_text": entity_text})
|
37 |
else:
|
38 |
-
#
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
# Print the identified
|
42 |
-
print(f"
|
43 |
|
44 |
-
return {"entities":
|
|
|
16 |
# Perform named entity recognition on the input text
|
17 |
entities = ner_analyzer(text)
|
18 |
|
19 |
+
# Prepare a list to store word-level entities
|
20 |
+
word_entities = []
|
21 |
+
|
22 |
+
# Initialize variables to track the current word and its label
|
23 |
+
current_word = ""
|
24 |
+
current_label = None
|
25 |
|
26 |
for entity in entities:
|
27 |
label = entity.get("entity", "UNKNOWN")
|
|
|
34 |
|
35 |
# Check for multi-token entities
|
36 |
if "##" in word:
|
37 |
+
# Concatenate sub-tokens to form the complete word
|
38 |
+
current_word += entity_text
|
39 |
+
current_label = label
|
|
|
40 |
else:
|
41 |
+
# If it's the first token of a new word, add the previous word to the list
|
42 |
+
if current_word:
|
43 |
+
word_entities.append({"word": current_word, "label": current_label, "entity_text": current_word})
|
44 |
+
current_word = ""
|
45 |
+
current_label = None
|
46 |
+
|
47 |
+
# Add the current token as a new word
|
48 |
+
word_entities.append({"word": word, "label": label, "entity_text": entity_text})
|
49 |
+
|
50 |
+
# Check for any remaining word
|
51 |
+
if current_word:
|
52 |
+
word_entities.append({"word": current_word, "label": current_label, "entity_text": current_word})
|
53 |
|
54 |
+
# Print the identified word-level entities
|
55 |
+
print(f"Word-level Entities: {word_entities}")
|
56 |
|
57 |
+
return {"entities": word_entities} # Return a dictionary with the specified output component
|