Views
No views yet
1from transformers import pipeline
2
3def print_ner(sentences):
4 """Cleaning and printing NER results
5
6 """
7 for sentence in sentences:
8 last_entity_type = sentence[0]['entity']
9 last_index = sentence[0]['index']
10 word = sentence[0]['word']
11 for i, token in enumerate(sentence):
12 if (i > 0):
13 if (token['entity'] == last_entity_type) and (token['index'] == last_index + 1):
14 word = word + '' + token['word']
15
16 else:
17 word = word.replace('▁', ' ')
18 print(f"{word[1:]} {last_entity_type}")
19 word = token['word']
20 last_entity_type = token['entity']
21 last_index = token['index']
22
23 if i == len(sentence) - 1:
24 word = word.replace('▁', ' ')
25 print(f"{word[1:]} {last_entity_type}")
26
27
28pipe = pipeline(model='RashidNLP/NER-Deberta')
29sentence = pipe(["Elon Musk will be at SpaceX's Starbase facility in Boca Chica for the orbital launch of starship next month"])
30print_ner(sentence)
31