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)


No entities found in the document

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)


London  -  GPE  -  Countries, cities, states
Anderson  -  PERSON  -  People, including fictional
Bombay  -  GPE  -  Countries, cities, states
Zurich  -  GPE  -  Countries, cities, states

In [62]:
displacy.render(doc2,style='ent',jupyter=True)


Welcome to London GPE . Hope you have a good time Mr. Anderson PERSON . When is your next flight to Bombay GPE ? Could you come over sometime to Zurich GPE / Swiss

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)


1000  -  CARDINAL  -  Numerals that do not fall under another type
100 dollars  -  MONEY  -  Monetary values, including unit
Microsoft  -  ORG  -  Companies, agencies, institutions, etc.

In [52]:
displacy.render(doc3,style='ent',jupyter=True)


Can i borrow 1000 CARDINAL rupees from you? or you can give me 100 dollars MONEY of Microsoft ORG stocks

In [53]:
doc4 = nlp(u'Tesla to build a U.K. factory for $6 million')

display_entities(doc4)  ## note : Tesla is not recorgnised


U.K.  -  GPE  -  Countries, cities, states
$6 million  -  MONEY  -  Monetary values, including unit
Adding missing tokens as named-entities

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


383

In [55]:
display_entities(doc4)


Tesla  -  ORG  -  Companies, agencies, institutions, etc.
U.K.  -  GPE  -  Countries, cities, states
$6 million  -  MONEY  -  Monetary values, including unit

In [56]:
displacy.render(doc4,'ent', jupyter=True)


Tesla ORG to build a U.K. GPE factory for $6 million MONEY

In [ ]: