首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:'instancemethod‘对象不可迭代(内容旋转器)

TypeError:'instancemethod‘对象不可迭代(内容旋转器)
EN

Stack Overflow用户
提问于 2016-12-04 05:47:46
回答 1查看 3.4K关注 0票数 1

我正在尝试使用这个旋转器重写字符串。

当我试图运行与自述相同的代码时:

bot.py:

代码语言:javascript
复制
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

当我这样做时,它会返回:

代码语言:javascript
复制
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:

代码语言:javascript
复制
   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,因为我只是从互联网上抓取它,我只是需要一些东西,可以为我提供免费的短信。另外,下面一行是做什么的:

代码语言:javascript
复制
synonyms = [word]

如果有人能推荐一些其他免费的文本旋转器,我愿意尝试其他的东西,但我尝试了一些,这是最直接的,我只是想传递一行文本,或文件,并让它重新编写基于同义词/等。这似乎是最好的选择,我有这样做,我只是不知道哪里出了问题的代码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-04 11:02:12

@海武是对的。您需要将getSynonyms函数更改为:

代码语言:javascript
复制
#     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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40955820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档