Spaces:
Build error
Build error
Commit
·
b3d7e5c
1
Parent(s):
26cb077
Implemented optimized NER function.
Browse files- modules/nlp/nemo_ner.py +17 -0
modules/nlp/nemo_ner.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
from collections import Counter
|
3 |
+
|
4 |
+
from nemo.collections import nlp as nemo_nlp
|
5 |
+
|
6 |
+
new_config = nemo_nlp.models.TokenClassificationModel.from_pretrained(model_name="ner_en_bert", return_config=True)
|
7 |
+
new_config.dataset.num_workers = 0
|
8 |
+
pretrained_ner_model = nemo_nlp.models.TokenClassificationModel.from_pretrained(
|
9 |
+
model_name="ner_en_bert", override_config_path=new_config)
|
10 |
+
|
11 |
+
|
12 |
+
def detect_ner(input_string):
|
13 |
+
results = pretrained_ner_model.add_predictions([input_string.replace('[', '').replace(']', '')])[0]
|
14 |
+
print(results)
|
15 |
+
tags = re.findall('\[.*?]', results)
|
16 |
+
output_string = "Found named entities: " + str(dict(Counter(tags)))[1:-1]
|
17 |
+
return output_string
|