Python uses NLTK to convert words into their stem form or basic form
Environmental construction and preparation work:
1. Install Python: on the official website https://www.python.org/downloads/ Download and install the latest version of Python.
2. Install NLTK: Execute 'pip install nltk' on the command line to install NLTK.
3. Install dependent corpus: Execute 'import nltk' and 'nltk. download()' in the Python interactive interface to open the NLTK downloader, select the required data package for download.
Dependent class libraries:
-NLTK: Python library for Natural language processing.
Dataset:
-WordNet: a data set containing English words and their synonyms, Hyponymy and hypernymy and other information. You can use NLTK's WordNet data package by downloading it.
Sample data:
We use words from input sentences to demonstrate how to stem them, such as converting the word 'running' to 'run'.
Complete sample explanation:
In the following example, we will use NLTK's PorterStemmer class to implement stem processing.
python
import nltk
from nltk.stem import PorterStemmer
#Initialize stemming processor
stemmer = PorterStemmer()
#Example Input Sentence
sentence = "I was running in the park"
#Split sentences into words
words = nltk.word_tokenize(sentence)
#Perform stem processing on each word
stemmed_words = [stemmer.stem(word) for word in words]
#Output processed results
print(stemmed_words)
Output results:
`['I', 'wa', 'run', 'in', 'the', 'park']`
Full source code:
python
import nltk
from nltk.stem import PorterStemmer
def stem_words(sentence):
#Initialize stemming processor
stemmer = PorterStemmer()
#Split sentences into words
words = nltk.word_tokenize(sentence)
#Perform stem processing on each word
stemmed_words = [stemmer.stem(word) for word in words]
return stemmed_words
#Example Input Sentence
sentence = "I was running in the park"
#Stem processing of sentences
stemmed_sentence = " ".join(stem_words(sentence))
#Output processed results
print(stemmed_sentence)
Output results:
`I wa run in the park`