In [45]:
import spacy
from spacy import displacy
In [46]:
nlp = spacy.load('en')
In [47]:
def display_entities(doc):
if doc.ents:
for ents in doc.ents:
print(ents.text," - ",ents.label_," - ",str(spacy.explain(ents.label_)))
else:
print("No entities found in the document")
In [48]:
doc1 = nlp(u"Hi How are you")
display_entities(doc1)
In [61]:
doc2 = nlp(u"Welcome to London. Hope you have a good time Mr. Anderson. When is your next flight to Bombay? Could you come over sometime to Zurich / Swiss")
display_entities(doc2)
In [62]:
displacy.render(doc2,style='ent',jupyter=True)
In [51]:
doc3 = nlp(u"Can i borrow 1000 rupees from you? or you can give me 100 dollars of Microsoft stocks")
display_entities(doc3)
In [52]:
displacy.render(doc3,style='ent',jupyter=True)
In [53]:
doc4 = nlp(u'Tesla to build a U.K. factory for $6 million')
display_entities(doc4) ## note : Tesla is not recorgnised
In [54]:
from spacy.tokens import Span
ORG = doc4.vocab.strings[u'ORG'] ## returns id of ORG label
print(ORG)
new_ent = Span(doc4,0,1,label=ORG)
## add entity to an existing doc
doc4.ents = list(doc4.ents) + [new_ent] ## note you can run only once
In [55]:
display_entities(doc4)
In [56]:
displacy.render(doc4,'ent', jupyter=True)
In [ ]: