2017-02-28 14 views
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") 

做最後一行(意譯(「敏捷的棕色狐狸跳過懶狗」)),它給了我的錯誤就這樣經過只列出連接列表(不是「方法」)列表

在這種情況下似乎有什麼錯?

+0

如果您已經包含完整的堆棧跟蹤,它會顯示錯誤觸發的位置,回答您的問題會容易得多。 – alexis

+0

更正了synonymIfExists – HalfPintBoy

回答

1

的錯誤是在synonyms()lemmasSynset一個類的方法,和nameLemma一類方法。這意味着,你必須通過提供(),以及明確地稱他們爲功能,像這樣:

def synonyms(word, tag): 
    lemma_lists = [ss.lemmas() for ss in wn.synsets(word, pos(tag))] 
    lemmas = [lemma.name() for lemma in sum(lemma_lists, [])] 
    return set(lemmas) 

如果解決這個問題,你的錯誤信息消失。