2016-12-04 32 views
1

我試圖用this spinner來重寫字符串。TypeError:'instancemethod'對象不可迭代(Content Spinner)

,當我嘗試運行完全相同的代碼作爲自述:

bot.py:

108: from spinner import spinner 
109: s = spinner() 
110: spintax = s.getSpintax('Everything in moderation, including moderation.') 
111: spun = s.spin(spintax) 
112: print spintax, spun 

當我這樣做,它返回:

Traceback (most recent call last): 
    File "C:\Python27\Scripts\Reddit\bot.py", line 110, in <module> 
    spintax = s.getSpintax('Everything in moderation, including moderation.') 
    File "C:\Python27\Scripts\Reddit\spinner.py", line 56, in getSpintax 
    n, syn = self.getSynonyms(stem) 
    File "C:\Python27\Scripts\Reddit\spinner.py", line 36, in getSynonyms 
    for lemma in syn.lemmas: 
TypeError: 'instancemethod' object is not iterable 

相關該錯誤消息的代碼是:

spinner.py:

32: def getSynonyms(self, word): 
    33: # include the original word 
    34:  synonyms = [word] 
    35:  for syn in wordnet.synsets(word): 
    36:   for lemma in syn.lemmas: 
    37:    if lemma.name != word: 
    38:    # since wordnet lemma.name will include _ for spaces, we'll replace these with spaces 
    39:     w, n = re.subn("_", " ", lemma.name) 
    40:     synonyms.append(w) 
    41:  s = list(set(synonyms)) 
    42:  return len(s), s 
    43: 
    44: # transform text into spintax with the folowing steps 
    45: # 1. split the text to sentences 
    46: # 2. loop through the sentences and tokenize it 
    47: # 3. loop thorugh each token, find its stem and assemble all the synonyms of it into the spintax 
    48: def getSpintax(self, text): 
    49:  sentences = self.splitToSentences(text) 
    50:  stemmer = PorterStemmer() 
    51:  spintax = "" 
    52:  for sentence in sentences: 
    53:   tokens = regexp_tokenize(sentence, "[\w']+") 
    54:   for token in tokens: 
    55:    stem = stemmer.stem(token) 
    56:    n, syn = self.getSynonyms(stem) 
    57:    spintax += "{" 
    58:    spintax += token 
    59:    spintax += "|" 
    60:    for x in range(n): 
    61:     spintax += syn[x] 
    62:     if x < n-1: 
    63:      spintax += "|" 
    64:     else: 
    65:      spintax += "} " 
    66:  return spintax 

我在Python 3都嘗試和2

我不熟悉spinner.py,因爲我只是抓住它從互聯網上,我只是需要的東西,將旋轉文本我要自由。此外,什麼是以下行做:

synonyms = [word] 

如果任何人都可以推薦一些其他的免費文本微調我可以用我願意嘗試一些別的東西,但我想了一堆,這一個是最簡單的,我只想傳入一行文本或文件,並根據同義詞/等重新編寫它。這似乎是我所做的最好的選擇,我只是不知道代碼出了什麼問題。

+2

嘗試:'爲引理syn.lemmas()' - 注意括號 –

回答

1

@ hai-vu是對的。您需要將getSynonyms功能更改爲:

#  get all synonyms of a word from the wordnet database 
    def getSynonyms(self, word): 
#   include the original word 
     synonyms = [word] 
     for syn in wordnet.synsets(word): 
      for lemma in syn.lemmas(): 
       if lemma.name() != word: 
#      since wordnet lemma.name will include _ for spaces, we'll replace these with spaces 
        w, n = re.subn("_", " ", lemma.name()) 
        synonyms.append(w) 
     s = list(set(synonyms)) 
     return len(s), s 
+0

這個工作,我很欣賞的幫助。 你能告訴我什麼是「同義詞= [單詞]」行嗎?我真的不知道該如何看待這個問題,是否像將單詞變量作爲列表或其他東西一樣投入? – Lightja

+0

它創建同義詞作爲列表。並將單詞的內容放在該列表中。 –