我正在尝试使用lesk算法替换句子中的单词。目前我已经使用了wordnet。我无法在其中集成lesk。有人可以帮我吗?
from nltk.corpus import wordnet
from nltk.tokenize import word_tokenize
from nltk.wsd import lesk
from random import randint
import nltk.data
text = "I am going to Banglore this weekend"
output = ""
words = word_tokenize(text)
a= []
for i in range(len(words)):
a.append(lesk(words,words[i],'n'))
for i in range(0,len(words)):
replacements = []
# Only replace nouns with nouns, vowels with vowels.
for syn in wordnet.synsets(words[i]):
if tagged[i][1] == 'NNP' or tagged[i][1] == 'DT':
break
word_type = tagged[i][1][0].lower()
if syn.name().find("."+word_type+"."):
# extract the word only
r = syn.name()[0:syn.name().find(".")]
replacements.append(r)
if len(replacements) > 0:
# Choose a random replacement
replacement = replacements[randint(0,len(replacements)-1)]
output = output + " " + replacement
else:
output = output + " " + words[i]
print(wordnet.synsets(words[i]))
print(output)
我想使用:
的输出for i in range(len(words)):
a.append(lesk(words,words[i],'n'))
用同义词替换单词。即lesk(lesk_sense)的输出
如果有人有什么主意,请指导我。