如何将 CHATBOT 未回答的问题存储在文本文件中

2024-01-24

我是新手python并使用构建聊天机器人聊天机器人库,我想将用户提出的聊天机器人无法回答的问题(我的意思是存储未回答的问题)存储在text文件或database以便我们稍后可以给出他们的答案。 这是代码聊天机器人构造函数

self.chatbot = ChatBot(
            "GUI Bot",
            storage_adapter="chatterbot.storage.SQLStorageAdapter",
            logic_adapters=[{
                'import_path': 'chatterbot.logic.BestMatch',
                'default_response': 'I am sorry, but I do not understand.',
                'maximum_similarity_threshold': 0.75
} ]
        )

这是类的完整代码

class TkinterGUIExample(tk.Tk):

    def __init__(self, *args, **kwargs):
        """
        Create & set window variables.
        """
        tk.Tk.__init__(self, *args, **kwargs)

        self.chatbot = ChatBot(
            "GUI Bot",
            storage_adapter="chatterbot.storage.SQLStorageAdapter",
            logic_adapters=[{
                'import_path': 'chatterbot.logic.BestMatch',
                'default_response': 'I am sorry, but I do not understand.',
                'maximum_similarity_threshold': 0.75
} ]
        )


        for files in os.listdir('C:/Users/HP/Desktop/FYP BOT/training_data/'):
            con=open('C:/Users/HP/Desktop/FYP BOT/training_data/'+files,'r').readlines()
            trainer = ListTrainer(self.chatbot)
            trainer.train(con)
        self.title("Chatterbot")

        self.initialize()

必须有一种方法可以知道聊天机器人中使用了哪个逻辑适配器,或者是否没有使用它们。我能想到的最简单的方法是使用default_response.

Set default_response = '-2E-'或者是其他东西。接下来,添加 if else 条件以查看值是否str(bot.get_response(userText))等于-2E-。如果它们匹配,则意味着未使用任何逻辑适配器,并且未找到与用户输入的匹配项。

没有使用逻辑适配器意味着它是一个没有答案的输入。您现在可以附加存储在的用户输入userText到一个文本文件。

Code:

## initialize chatter bot
bot = ChatBot(
    'robot',
    storage_adapter='chatterbot.storage.SQLStorageAdapter',
    preprocessors=[
        'chatterbot.preprocessors.clean_whitespace',
    ],
    logic_adapters=[
        {
            'import_path': 'chatterbot.logic.BestMatch',
            'default_response': '-2E-',
            'maximum_similarity_threshold': 0.90,
            'statement_comparison_function': chatterbot.comparisons.levenshtein_distance,
            'response_selection_method': chatterbot.response_selection.get_first_response
        },
        'chatterbot.logic.MathematicalEvaluation'
    ],
    database_uri='sqlite:///database.db',
    read_only=True
)

以下是在代码中使用的示例逻辑。您应该根据自己的要求修改此逻辑。

## Open a file to write unknown user inputs
with open("unanswered.txt", "a") as f:

    ## Loop and get user input
    ## Check to see if none of the logic adapters was used
    if str(bot.get_response(userText)) == "-2E-":
        f.write(userText)
        return "Sorry, I do not understand."
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 CHATBOT 未回答的问题存储在文本文件中 的相关文章

随机推荐