nltk 库中的 WordNetLemmatizer 将满足您的需求。这是python3代码:
#!Python3 -- this is lemmatize_s.py
import nltk
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize
print ("This program will lemmatize your input until you ask for it to 'end'.")
while True:
sentence = input("Type one or more words (or 'end') and press enter:")
if (sentence == "end"):
break
tokens = word_tokenize(sentence)
lemmatizer = WordNetLemmatizer()
Output=[lemmatizer.lemmatize(word) for word in tokens]
print (Output);
从命令行运行它:
eyeMac2016:james$ python3 lemmatize_s.py
This program will lemmatize your input until you ask for it to 'end'.
Type one or more words (or 'end') and press enter:books ashes
['book', 'ash']
Type one or more words (or 'end') and press enter:end
eyeMac2016:james$