146 lines
1.4 KiB
Plaintext
146 lines
1.4 KiB
Plaintext
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Import spaCy and load the language library
|
|
import spacy
|
|
nlp = spacy.load('en_core_web_sm')
|
|
|
|
# Create a Doc object
|
|
doc = nlp(u'Tesla is looking at buying U.S. startup for $6 million')
|
|
|
|
# Print each token separately
|
|
for token in doc:
|
|
print(token.text, token.pos_, token.dep_)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nlp.pipeline
|
|
|
|
|
|
nlp.pipe_names
|
|
|
|
|
|
|
|
|
|
|
|
doc2 = nlp(u"Tesla isn't looking into startups anymore.")
|
|
|
|
for token in doc2:
|
|
print(token.text, token.pos_, token.dep_)
|
|
|
|
|
|
|
|
|
|
|
|
doc2
|
|
|
|
|
|
doc2[0]
|
|
|
|
|
|
type(doc2)
|
|
|
|
|
|
|
|
|
|
|
|
doc2[0].pos_
|
|
|
|
|
|
|
|
|
|
|
|
doc2[0].dep_
|
|
|
|
|
|
|
|
|
|
|
|
spacy.explain('PROPN')
|
|
|
|
|
|
spacy.explain('nsubj')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Lemmas (the base form of the word):
|
|
print(doc2[4].text)
|
|
print(doc2[4].lemma_)
|
|
|
|
|
|
# Simple Parts-of-Speech & Detailed Tags:
|
|
print(doc2[4].pos_)
|
|
print(doc2[4].tag_ + ' / ' + spacy.explain(doc2[4].tag_))
|
|
|
|
|
|
# Word Shapes:
|
|
print(doc2[0].text+': '+doc2[0].shape_)
|
|
print(doc[5].text+' : '+doc[5].shape_)
|
|
|
|
|
|
# Boolean Values:
|
|
print(doc2[0].is_alpha)
|
|
print(doc2[0].is_stop)
|
|
|
|
|
|
|
|
|
|
|
|
doc3 = nlp(u'Although commmonly attributed to John Lennon from his song "Beautiful Boy", \
|
|
the phrase "Life is what happens to us while we are making other plans" was written by \
|
|
cartoonist Allen Saunders and published in Reader\'s Digest in 1957, when Lennon was 17.')
|
|
|
|
|
|
life_quote = doc3[16:30]
|
|
print(life_quote)
|
|
|
|
|
|
type(life_quote)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
doc4 = nlp(u'This is the first sentence. This is another sentence. This is the last sentence.')
|
|
|
|
|
|
for sent in doc4.sents:
|
|
print(sent)
|
|
|
|
|
|
doc4[6].is_sent_start
|
|
|
|
|
|
|