找回密码
 立即注册
查看: 467|回复: 0

基于python和深度学习实现本地离线智能语音控制终端(五)

[复制链接]

86

主题

0

回帖

894

积分

中级会员

积分
894
发表于 2024-4-24 14:26:17 | 显示全部楼层 |阅读模式
学会聊天

这里我基于chatterbot和第三方的语义库,高度定制化地 训练自己的机器人聊天对话系统

1.环境配置:
  1. pip install chatterbot
  2. pip install chatterbot_corpus
复制代码
2.尝试训练官方的中文数据集并使用

  1. from chatterbot import ChatBot
  2. from chatterbot.trainers import ChatterBotCorpusTrainer
  3. import logging

  4. '''
  5. This is an example showing how to train a chat bot using the
  6. ChatterBot Corpus of conversation dialog.
  7. '''

  8. # Enable info level logging
  9. # logging.basicConfig(level=logging.INFO)
  10. chatbot = ChatBot('Example Bot')

  11. # Start by training our bot with the ChatterBot corpus data
  12. trainer = ChatterBotCorpusTrainer(chatbot)

  13. def train():
  14.     trainer.train(
  15.         'chatterbot.corpus.chinese'
  16.     )

  17. def chat(word = ''):
  18.     word = chatbot.get_response(word)
  19.     return word

  20. def test1():
  21.     train()
  22.     while 1:
  23.         print(chatbot.get_response(input(">")))

  24. test1()
复制代码
若无报错则,可以继续下一步,训练自己的数据集,实现高度定制化。

3.我提供一个数据集:https://download.csdn.net/download/weixin_51331359/76479596

大概长这样:




下载后,在你的项目文件夹中建立一个名为 corpus  的文件夹,把下载好的  corpus.txt 放进去。

进行训练,代码:

  1. from chatterbot import ChatBot
  2. from chatterbot.trainers import ListTrainer
  3. from chatterbot.trainers import ChatterBotCorpusTrainer


  4. # 构建ChatBot并指定Adapter
  5. my_bot = ChatBot(
  6.     'COCO',
  7.     storage_adapter='chatterbot.storage.SQLStorageAdapter',
  8.     logic_adapters=[
  9.         {
  10.             'import_path': 'chatterbot.logic.BestMatch',
  11.             'threshold': 0.65,#低于置信度,则默认回答
  12.             'default_response':'coco没听懂'
  13.         }
  14.     ]
  15. )


  16. def train_myword():
  17.     file = open("./corpus/corpus.txt", 'r', encoding='utf-8')
  18.     corpus = []
  19.     print('开始加载语料!')
  20.     # 导入语料库
  21.     while 1:
  22.         try:
  23.             line = file.readline()
  24.             if not line:
  25.                 break
  26.             if line == '===\n':
  27.                 continue
  28.             temp = line.strip('\n')
  29.             # print(temp)
  30.             corpus.append(temp)
  31.         except:
  32.             pass
  33.     file.close()
  34.     print('语料加载完毕')
  35.     print('》'*30)

  36.     #my_bot = ChatBot("coco")
  37.     #my_bot.set_trainer(ListTrainer)
  38.     trainer = ListTrainer(my_bot)
  39.     print('开始训练!')
  40.     trainer.train(corpus[:10000])
  41.     print('训练完毕!')

  42. def chat1():
  43.     while True:
  44.         print(my_bot.get_response(input("user:")))

  45. def chat_my(word = ''):
  46.     word = my_bot.get_response(word)
  47.     return word

  48. def test1():
  49.     train_myword()
  50.     chat1()

  51. test1()
复制代码
训练完毕:
  1. 开始加载语料!
  2. 语料加载完毕
  3. 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
  4. 开始训练!
  5. List Trainer: [####################] 100%
  6. 训练完毕!
复制代码
这里我只训练语库的前10000条对话,建议不要训练太多的条对话,贪多嚼不烂,就算训练出来了也有很高的回复延迟,甚至直接无法运行回复,非常影响用户体验。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|EnMaking

GMT+8, 2025-3-31 09:07 , Processed in 0.052750 second(s), 24 queries .

Powered by Qiqirobot

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表