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

Why NLTK lemmatization has wrong output even if verb.exc has added right value?

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

saw see

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

>>>print lmtzr.lemmatize('saw', 'v')
saw

怎么会这样?我在修改wordNet时理解错了吗?

简而言之:

这是一种奇怪的异常情况。

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

请参阅@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'

指定 POS 标记似乎是多余的。让我们看一下词形还原器代码本身:

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

它的作用是依赖 wordnet 语料库的 _moprhy 属性 到 return 可能的引理。

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

处看到 _morphy() 代码

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

因此,如果我们在词形还原器函数之外对异常进行临时搜索,我们确实会看到 'saw' -> 'see':

>>> from nltk.corpus import wordnet as wn
>>> exceptions = wn._exception_map['v']
>>> exceptions['saw']
[u'see']

所以如果我们在词形还原器之外调用 _morphy() 函数:

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

让我们回到WordNetLemmatizer.lemmatize()代码的return行,我们看到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

所以这意味着函数将 return 来自 wn._morphy() 的输出具有最小长度。但在这种情况下,saw 和 see 的长度相同,因此 wn._morphy() 编辑的 return 列表中的第一个将是 returned,即 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'

所以问题是:

  • 如何在 NLTK 中避免这种情况 "bug"?
  • 如何在 NLTK 中修复这个 "bug"?

但请注意,它不完全是 "bug" 而是 "feature" 来表示表面词的其他可能词条(尽管该词在该特定上下文中很少见,例如 I saw the log into half .


如何在 NLTK 中避免这个 "bug"?

为了在 NLTK 中避免这个 "bug",使用 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']

选择多总比选择错好。


如何在 NLTK 中修复此 "bug"?

除了 min(lemmas, key=len) 是次优的,_morphy() 函数在处理异常时有点不一致,因为复数词中的稀有含义可能本身就是一个引理,例如使用 teeth 来指代假牙,参见 http://wordnetweb.princeton.edu/perl/webwn?s=teeth

>>> wn._morphy('teeth', 'n')
['teeth', u'tooth']
>>> wn._morphy('goose', 'n')
['goose']
>>> wn._morphy('geese', 'n')
[u'goose']

所以一定是在异常列表之后的nltk.wordnet._morphy()函数中引入了引理选择错误。如果输入的表面词出现在例外列表中,一个快速的技巧是立即 return 例外列表的第一个实例,例如:

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.