我正在制作一个不和谐的机器人,每隔几秒就会将随机生成的句子喷射到聊天中。我试图使用nltk模块使句子结构更好一些,但是我抓住了一个错误并且无法弄清楚它。(我对python很新,并且一直在学习我需要知道的一切沿。)
错误:
File "/root/PycharmProjects/untitled/Loop.py", line 29, in background_loop
messages = [(POSifiedText.make_sentence(tries=8, max_overlap_total=14, default_max_overlap_ratio=5.6,))]
TypeError: make_sentence() missing 1 required positional argument: 'self'
代码:
import asyncio
import random
import discord.ext.commands
import markovify
import nltk
import re
class POSifiedText(markovify.Text):
def word_split(self, sentence):
words = re.split(self.word_split_pattern , sentence )
words = ["::".join(tag) for tag in nltk.pos_tag ( words )]
return words
def word_join(self, words):
sentence = " ".join(word.split("::")[0] for word in words )
return sentence
with open("/root/sample.txt") as f:
text = f.read()
text_model = (markovify.Text(text, state_size=1))
client = discord.Client()
async def background_loop():
await client.wait_until_ready()
while not client.is_closed:
channel = client.get_channel('ChannelIdHere')
messages = [(POSifiedText.make_sentence(tries=8, max_overlap_total=14, default_max_overlap_ratio=5.6,))]
await client.send_message(channel, random.choice(messages))
await asyncio.sleep(10)
client.loop.create_task(background_loop())
client.run("TokenHere")
答案 0 :(得分:1)
您需要在Text对象的实例上调用make_sentence。
text_model.make_sentence(...)
我认为你也想使用你的自定义类:
text_model = POSifiedText(text, state_size=1)