Natural Language Processing with spaCy
Azadeh Mobasher
Principal Data Scientist
import spacy
nlp = spacy.load("en_core_web_sm")
text = "My cat will fish for a fish tomorrrow in a fishy way."
print([(token.text, token.pos_, spacy.explain(token.pos_))
for token in nlp(text)])
I will fish tomorrow.
I ate fish.
verb -> pescaré
noun -> pescado
import spacy nlp = spacy.load("en_core_web_sm") verb_text = "I will fish tomorrow." noun_text = "I ate fish."
print([(token.text, token.pos_) for token in nlp(verb_text) if "fish" in token.text], "\n") print([(token.text, token.pos_) for token in nlp(noun_text) if "fish" in token.text])
[('fish', 'VERB', 'verb')]
[('fish', 'NOUN', 'noun')]
Dependency label | Description |
---|---|
nsubj | Nominal subject |
root | Root |
det | Determiner |
dobj | Direct object |
aux | Auxiliary |
displaCy
can draw dependency treesdoc = nlp("We understand the differences.")
spacy.displacy.serve(doc, style="dep")
.dep_
attribute to access the dependency label of a token
doc = nlp("We understand the differences.")
print([(token.text, token.dep_, spacy.explain(token.dep_)) for token in doc])
[('We', 'nsubj', 'nominal subject'), ('understand', 'ROOT', 'root'),
('the', 'det', 'determiner'), ('differences', 'dobj', 'direct object'),
('.', 'punct', 'punctuation')]
Natural Language Processing with spaCy