Return ent.text and ent.label_ in dictionary format #11707
-
I created a custom NER spacy model and intend to create a single page app that takes in input and gives out json result of extracted entities. nlp = spacy.load("my-model")
doc = nlp(''' Patient is taking dolo 100mg for fever. Complains of sleeplessness and pain in left leg. History of CKD. Underwent gall bladder laparoscopy in feb 2022.''') Now to get entities and create a dictionary out of it I used the following code: entities = list(nlp.get_pipe('ner').labels)
extracted_entities = dict.fromkeys(entities, [])
Now all i want to do is get the entities into their respective list in the for ent in doc.ents:
label = ent.label_
extracted_entities[ent.label_].append(ent) But this code adds all entities in all the lists in |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @musicnandpeace , this may be more of how Python's extracted_entities = {ent: [] for ent in entities} Also, next time please format the codeblocks using backticks (```), it helps us better debug and copy-paste your sample code |
Beta Was this translation helpful? Give feedback.
Hi @musicnandpeace , this may be more of how Python's
dict.fromkeys
works than in spaCy. One solution is to use a dictionary comprehension:Also, next time please format the codeblocks using backticks (```), it helps us better debug and copy-paste your sample code