TypeError:“instancemethod”对象不可编辑(内容微调器)

2024-06-26 00:07:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用this spinner重写字符串。在

当我尝试运行与自述文件中相同的代码时:

在机器人.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

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

^{pr2}$

该错误消息的相关代码为:

在微调器.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

我尝试过python3和python2

我不熟悉微调器.py因为我只是从网上抓到的,我只需要一些免费的东西来为我旋转文字。另外,下面的行是做什么的:

synonyms = [word]

如果有人能推荐一些其他的自由文本微调器,我愿意尝试其他东西,但我尝试了一堆,这一个是最直接的,我只想传递一行文本,或文件,并让它重新写基于同义词/等。这似乎是最好的选择,我只是不知道什么是代码出了问题。在


Tags: the代码textinselftokenforsentences
1条回答
网友
1楼 · 发布于 2024-06-26 00:07:45

@海伍是对的。您需要将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

相关问题 更多 >