NLP(十三) 词义消歧

时间:2023-03-09 03:29:30
NLP(十三) 词义消歧

原文链接:http://www.one2know.cn/nlp13/

  • 一个词可能有多个词义
例句 解释
She is my date date: 约会,日期
You have taken too many leaves to skip cleaning leaves in the garden leave:休息,树叶

用Lesk算法

  • 代码
import nltk

def understandWordSenseExamples():
words = ['wind','date','left']
print('-- examples --')
for word in words:
syns = nltk.corpus.wordnet.synsets(word)
for syn in syns[:2]:
for example in syn.examples()[:2]:
print('{} -> {} -> {}'.format(word,syn.name(),example))
# 打印 : 单词 -> 同义词集 -> 例句 def understandBuiltinWSD():
print('-- built-in wsd --')
maps = [
('It is the fish net that you are using to catch fish ?','fish','n'),
('Please dont point your finger at others.','point','n'),
('I went to the river bank to see the sun rise','bank','n'),
]
for m in maps:
print("Sense '{}' for '{}' -> '{}'".format(m[0],m[1],nltk.wsd.lesk(m[0],m[1],m[2]))) if __name__ == "__main__":
understandWordSenseExamples()
understandBuiltinWSD()

输出:

-- examples --
wind -> wind.n.01 -> trees bent under the fierce winds
wind -> wind.n.01 -> when there is no wind, row
wind -> wind.n.02 -> the winds of change
date -> date.n.01 -> what is the date today?
date -> date.n.02 -> his date never stopped talking
left -> left.n.01 -> she stood on the left
-- built-in wsd --
Sense 'It is the fish net that you are using to catch fish ?' for 'fish' -> 'Synset('pisces.n.02')'
Sense 'Please dont point your finger at others.' for 'point' -> 'Synset('point.n.25')'
Sense 'I went to the river bank to see the sun rise' for 'bank' -> 'Synset('savings_bank.n.02')'