1
嘗試在NLTK中使用標記化來執行簡單釋放。在Python中使用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")
做最後一行(意譯(「敏捷的棕色狐狸跳過懶狗」)),它給了我的錯誤就這樣經過只列出連接列表(不是「方法」)列表
在這種情況下似乎有什麼錯?
如果您已經包含完整的堆棧跟蹤,它會顯示錯誤觸發的位置,回答您的問題會容易得多。 – alexis
更正了synonymIfExists – HalfPintBoy