【发布时间】:2017-02-28 12:03:15
【问题描述】:
尝试在 NLTK 中使用标记化来做简单的释义。
执行以下功能:
from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
from nltk.corpus import wordnet as wn
def tag(sentence):
words = word_tokenize(sentence)
words = pos_tag(words)
return words
def paraphraseable(tag):
return tag.startswith('NN') or tag == 'VB' or tag.startswith('JJ')
def pos(tag):
if tag.startswith('NN'):
return wn.NOUN
elif tag.startswith('V'):
return wn.VERB
def synonyms(word, tag):
return set([lemma.name for lemma in sum([ss.lemmas for ss in wn.synsets(word, pos(tag))],[])])
def synonymIfExists(sentence):
for (word, t) in tag(sentence):
if paraphraseable(t):
syns = synonyms(word, t)
if syns:
if len(syns) > 1:
yield [word, list(syns)]
continue
yield [word, []]
def paraphrase(sentence):
return [x for x in synonymIfExists(sentence)]
paraphrase("The quick brown fox jumps over the lazy dog")
在完成最后一行之后(paraphrase("The quick brown fox jumps over the lazy dog"))它给了我这样的错误:
只能将列表(不是“方法”)连接到列表
在这种情况下似乎有什么问题?
【问题讨论】:
-
如果您包含了完整的堆栈跟踪,它会更容易回答您的问题,它显示了触发错误的位置。
-
更正了同义词IfExists