且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用泡菜保存聊天机器人模型

更新时间:2023-11-19 08:26:46

您可以尝试一下.它为我工作.首次运行代码时,将使用trainer = ChatterBotCorpusTrainer(bot)trainer.train("static/chatterbot_data.yml")来训练机器人.它将在项目文件夹中生成文件database.db.一旦生成了数据库文件(如果没有更多的语料库更改),请注释下面的最后两行代码,而无需重新培训即可运行.

You can try this. It worked for me. When the code is run first time trainer = ChatterBotCorpusTrainer(bot) and trainer.train("static/chatterbot_data.yml") is used to train the bot. It will generate a file database.db in project folder. Once the database file is generated if there is no more change to corpus then comment last two lines of code below to run without retraining.

代码:

## 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': 'I am sorry, but I do not understand.',
            '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
)


## training corpus list
## Disable these two lines below AFTER first run when a *.db file is generated in project directory
trainer = ChatterBotCorpusTrainer(bot)
trainer.train("static/chatterbot_data.yml")