- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当我打开 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
属性来返回可能的词条。
如果我们遍历 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
因此,如果我们在 lemmatizer 函数之外对异常进行临时搜索,我们确实会看到 '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 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
这意味着该函数将以最小长度返回 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'
那么问题是:
但请注意,它不完全是“错误”,而是表示表面词的其他可能词条的“特征”(尽管该词在特定上下文中很少见,例如 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']
更多的选择总比错误的选择好。
如何修复 NLTK 中的这个“错误”?
除了 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()
函数中引入了引理选择错误。一个快速的技巧是如果输入的表面词出现在异常列表中,则立即返回异常列表的第一个实例,例如:
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.
关于python - 为什么即使 verb.exc 添加了正确的值,NLTK 词形还原也有错误的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33594721/
我是一名优秀的程序员,十分优秀!