制作一个不和谐的机器人,但不断获得 "missing 1 required positional argument: 'self'"

Making a discord bot, but keep getting "missing 1 required positional argument: 'self'"

我正在制作一个 discord 机器人,它每隔几秒就会在聊天中随机生成一些句子。我试图使用 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")

您需要对 Text 对象的实例调用 make_sentence。

text_model.make_sentence(...)

我想你也想像这样使用你的自定义 class:

text_model = POSifiedText(text, state_size=1)