我正在使用nltk的wordnet_lemmatizer。理想情况下,“我是”这个词应该被解释为“我”。
我尝试过以下POS标记:
wordnet_lemmatizer.lemmatize("I'm", wordnet.ADV)
wordnet_lemmatizer.lemmatize("I'm", wordnet.ADJ)
wordnet_lemmatizer.lemmatize("I'm", wordnet.VERB)
wordnet_lemmatizer.lemmatize("I'm", wordnet.NOUN)enter code here
所有人都回归“我是”而不是“我”, 知道我可能缺少什么吗?
答案 0 :(得分:1)
首先使用Tokenize和POS标记,然后使用该标记作为WordNetLemmatizer.lemmatize()
的{{1}}参数输入
>>> from nltk import pos_tag, word_tokenize
>>> from nltk.stem import WordNetLemmatizer
>>>
>>> wnl = WordNetLemmatizer()
>>>
>>> def penn2morphy(penntag):
... """ Converts Penn Treebank tags to WordNet"""
... morphy_tag = {'NN':'n', 'JJ':'a',
... 'VB':'v', 'RB':'r'}
... try:
... return morphy_tag[penntag[:2]]
... except:
... return 'n' # default to Nouns.
...
...
>>> def lemmatize_sent(tokenized_sent):
... return [wnl.lemmatize(word.lower(), penn2morphy(tag)) for word, tag in pos_tag(tokenized_sent)]
...
>>> lemmatize_sent("I'm")
['i', "'", 'm']