NLTK Python中的词义消歧

27 投票
6 回答
18955 浏览
提问于 2025-04-16 04:04

我刚接触NLTK这个Python库,想找一些示例应用来做词义消歧。搜索结果里有很多算法,但没有找到示例应用。我只是想输入一个句子,然后通过wordnet库了解每个单词的意思。

我在PERL里找到了一个类似的模块。http://marimba.d.umn.edu/allwords/allwords.html。请问在NLTK Python里有没有这样的模块呢?

6 个回答

8

没错,实际上有一本书是NLTK团队写的,这本书有好几章专门讲分类的内容,他们还特别介绍了如何使用WordNet。你也可以从Safari上购买这本书的纸质版。

顺便提一下:NLTK是由自然语言处理的学者们编写的,主要用于他们的入门编程课程。

19

最近,部分 pywsd 的代码已经被移植到最新版本的 NLTK 中,具体在 wsd.py 模块里,可以试试:

>>> from nltk.wsd import lesk
>>> sent = 'I went to the bank to deposit my money'
>>> ambiguous = 'bank'
>>> lesk(sent, ambiguous)
Synset('bank.v.04')
>>> lesk(sent, ambiguous).definition()
u'act as the banker in a game or in gambling'

为了获得更好的词义消歧(WSD)效果,建议使用 pywsd 库,而不是 NLTK 模块。一般来说,pywsd 中的 simple_lesk() 表现会比 NLTK 中的 lesk 更好。我会尽量在有空的时候更新 NLTK 模块。


针对 Chris Spencer 的评论,请注意 Lesk 算法的局限性。我只是提供了这些算法的准确实现,并不是说它能解决所有问题,详细信息可以查看这个链接:http://en.wikipedia.org/wiki/Lesk_algorithm

另外请注意,虽然:

lesk("My cat likes to eat mice.", "cat", "n")

可能无法给你正确答案,但你可以使用 pywsd 中的 max_similarity() 实现:

>>> from pywsd.similarity import max_similiarity
>>> max_similarity('my cat likes to eat mice', 'cat', 'wup', pos='n').definition 
'feline mammal usually having thick soft fur and no ability to roar: domestic cats; wildcats'
>>> max_similarity('my cat likes to eat mice', 'cat', 'lin', pos='n').definition 
'feline mammal usually having thick soft fur and no ability to roar: domestic cats; wildcats'

@Chris,如果你需要一个 python 的 setup.py 文件,随便问我,我会写的……

撰写回答