即使verb.exc添加了正确的值,为什么NLTK词形还原输出错误?

Leo*_*ieh 3 python nlp nltk wordnet lemmatization

当我打开verb.exc时,我可以看到

saw see
Run Code Online (Sandbox Code Playgroud)

虽然我在代码中使用了词形还原

>>>print lmtzr.lemmatize('saw', 'v')
saw
Run Code Online (Sandbox Code Playgroud)

怎么会发生这种情况?我在修改wordNet时会误解吗?

alv*_*vas 6

简而言之:

这是一种奇怪的例外情况.

还有一种情况,I saw the log the into half.其中"锯"是现在时态动词.

请参阅@nschneid解决方案,在提出的问题中使用更多细粒度标签:https://github.com/nltk/nltk/issues/1196


长期:

如果我们看看我们如何在NLTK中调用WordNet变形器:

>>> from nltk.stem import WordNetLemmatizer
>>> wnl = WordNetLemmatizer()
>>> wnl.lemmatize('saw', pos='v')
'saw'
>>> wnl.lemmatize('saw')
'saw'
Run Code Online (Sandbox Code Playgroud)

指定POS标签似乎是多余的.我们来看看lemmatizer代码本身:

class WordNetLemmatizer(object):
    def __init__(self):
        pass

    def lemmatize(self, word, pos=NOUN):
        lemmas = wordnet._morphy(word, pos)
        return min(lemmas, key=len) if lemmas else word
Run Code Online (Sandbox Code Playgroud)

它的作用是它依赖于_moprhywordnet语料库的属性来返回可能的引理.

如果我们遍历nltk.corpus.wordnet代码,我们会_morphy()在https://github.com/nltk/nltk/blob/develop/nltk/corpus/reader/wordnet.py#L1679上看到代码.

该函数的前几行从wordnet中读取异常文件verb.exc,即https://github.com/nltk/nltk/blob/develop/nltk/corpus/reader/wordnet.py#L1687

因此,如果我们在lemmatizer函数之外对异常进行临时搜索,我们会看到'saw' -> 'see':

>>> from nltk.corpus import wordnet as wn
>>> exceptions = wn._exception_map['v']
>>> exceptions['saw']
[u'see']
Run Code Online (Sandbox Code Playgroud)

所以如果我们_morphy()在lemmatizer之外调用函数:

>>> from nltk.corpus import wordnet as wn
>>> exceptions = wn._exception_map['v']
>>> wn._morphy('saw', 'v')
['saw', u'see']
Run Code Online (Sandbox Code Playgroud)

让我们回到WordNetLemmatizer.lemmatize()代码的返回行,我们看到return min(lemmas, key=len) if lemmas else word:

def lemmatize(self, word, pos=NOUN):
    lemmas = wordnet._morphy(word, pos)
    return min(lemmas, key=len) if lemmas else word
Run Code Online (Sandbox Code Playgroud)

这意味着该函数将以wn._morphy()最小长度返回输出.但是在这种情况下,saw和see都具有相同的长度,因此返回的列表中的第一个wn._morphy()将是返回的,即saw.

实际上,这WordNetLemmatizer.lemmatize()是这样做的:

>>> from nltk.corpus import wordnet as wn
>>> wn._morphy('saw', 'v')
['saw', u'see']
>>> min(wn._morphy('saw', 'v'), key=len)
'saw'
Run Code Online (Sandbox Code Playgroud)

所以问题是:

  • 如何在NLTK中避免这个"错误"?
  • 如何解决NLTK中的这个"错误"?

但请注意,它不是一个"错误",而是一个"特征"来表示表面词的其他可能的引理(尽管该特定上下文中的那个词很少见,例如I saw the log into half.


如何在NLTK中避免这个"错误"?

为了避免NLTK中的这个"错误",使用nltk.wordnet._morphy()而不是nltk.stem.WordNetLemmatizer.lemmatize()那种方式,你总是得到一个可能的列表列表,而不是按长度过滤的引理.引理:

>>> from nltk.corpus import wordnet as wn
>>> exceptions = wn._exception_map['v']
>>> wn._morphy('saw', pos='v')
['saw', 'see']
Run Code Online (Sandbox Code Playgroud)

更多的选择比错误的选择更好.


如何修复NLTK中的这个"bug"?

除了min(lemmas, key=len)次优之外,_morphy()在处理异常时函数有点不一致,因为复数单词中的罕见含义本身可能是一个引理,例如teeth用于引用假牙,请参阅http://wordnetweb.princeton. EDU /的Perl/webwn?S =齿

>>> wn._morphy('teeth', 'n')
['teeth', u'tooth']
>>> wn._morphy('goose', 'n')
['goose']
>>> wn._morphy('geese', 'n')
[u'goose']
Run Code Online (Sandbox Code Playgroud)

因此,必须nltk.wordnet._morphy()在异常列表之后的函数中引入引理选择中的错误.一个快速的黑客是如果输入表面字出现在例外列表中,则立即返回例外列表的第一个实例,例如:

from nltk.corpus import wordnet as wn
def _morphy(word, pos):
    exceptions = wn._exception_map[pos]
    if word in exceptions:
        return exceptions[word]

    # Else, continue the rest of the _morphy code.
Run Code Online (Sandbox Code Playgroud)