spaCy is an industrial-strength NLP library: one call runs tokenization, part-of-speech tagging, sentence splitting, lemmatization and named-entity recognition.
pip install spacy
python -m spacy download en_core_web_smTokens
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The cost of Iphone in U.K is 699$")
print([t.text for t in doc]) # ['The', 'cost', 'of', 'Iphone', 'in', 'U.K', 'is', '699', '$']Parts of speech & lemmas
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The cost of Iphone in U.K is 699$")
print([(t.text, t.pos_) for t in doc][:4]) # [('The', 'DET'), ('cost', 'NOUN'), ('of', 'ADP'), ('Iphone', 'PROPN')]
print([t.lemma_ for t in nlp("She was running faster")]) # ['she', 'be', 'run', 'fast']| Tag | Meaning | Tag | Meaning |
|---|---|---|---|
NOUN | Noun | VERB | Verb |
PROPN | Proper noun | AUX | Auxiliary (is, was) |
ADJ | Adjective | ADP | Preposition |
DET | Determiner (the, a) | NUM | Number |
Sentences & entities
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("This is the first sentence. Please check it. Let's study now")
print([s.text for s in doc.sents]) # ['This is the first sentence.', 'Please check it.', "Let's study now"]
ents = nlp("Sundar Pichai joined Google in 2004 in California.").ents
print([(e.text, e.label_) for e in ents]) # [('Sundar Pichai', 'PERSON'), ('Google', 'ORG'), ('2004', 'DATE'), ('California', 'GPE')]