Python uses spaCy to implement part of speech tagging
Preparation work:
1. Ensure that the Python interpreter has been installed.
2. Install the spaCy library: Run the command 'pip install - U space'.
3. Download the English model of spaCy: Run the command 'Python - m spacy download en'.
Dependent class libraries:
-SpaCy: used for Natural language processing tasks, including part of speech tagging, Named-entity recognition, etc.
Dataset:
SpaCy has pre trained a part of speech annotation model for English text, which we can use for demonstration of part of speech annotation.
The source code is as follows:
python
import spacy
#Load English model
nlp = spacy.load('en')
#Define sample data
text = "Apple is looking at buying U.K. startup for $1 billion"
#Word segmentation and part of speech tagging
doc = nlp(text)
for token in doc:
print(token.text, token.pos_)
Run the above code and the output result is as follows:
Apple PROPN
is VERB
looking VERB
at ADP
buying VERB
U.K. PROPN
startup NOUN
for ADP
$ SYM
1 NUM
billion NUM
The above code uses the English model from spaCy to annotate the part of speech of the sample data. Each word (token) in 'token. pos'_` The corresponding part of speech is stored in.