from nltk.corpus import wordnet
from nltk.stem import WordNetLemmatizer
from nltk import word_tokenize, pos_tag
wnl = WordNetLemmatizer()
def get_wordnet_pos(tag):
# 还原单词的词性
if tag.startswith('J'):
return wordnet.ADJ
elif tag.startswith('V'):
return wordnet.VERB
elif tag.startswith('N'):
return wordnet.NOUN
elif tag.startswith('R'):
return wordnet.ADV
else:
return None
def lemmatize(text):
sent = word_tokenize(text.lower())
sent = pos_tag(sent)# 获取单词的词性
ret = []
for word, tag in sent:
pos = get_wordnet_pos(tag) or wordnet.NOUN
ret.append(wnl.lemmatize(word, pos=pos))
#通过调用实例化对象wnl的lemmatize方法,传入需要进行词性还原的单词以及对应的词性,返回词性还原后的结果
return ret
对句子的单词进行词形还原
于 2022-07-21 16:52:43 首次发布