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

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

[复制链接]

86

主题

0

回帖

894

积分

中级会员

积分
894
发表于 2024-4-24 14:35:10 | 显示全部楼层 |阅读模式
进行数学运算和时间查询:
  1. # -*- coding: utf-8 -*-
  2. from chatterbot import ChatBot

  3. bot = ChatBot(
  4.     "Math & Time Bot",
  5.     logic_adapters=[
  6.         "chatterbot.logic.MathematicalEvaluation",
  7.         "chatterbot.logic.TimeLogicAdapter"
  8.     ],
  9.     input_adapter="chatterbot.input.VariableInputTypeAdapter",
  10.     output_adapter="chatterbot.output.OutputAdapter"
  11. )


  12. def chot_math_time(text=''):
  13.     response = bot.get_response(text)
  14.     return response

  15. print(chot_math_time('what is 1 + 1'))
  16. print(chot_math_time('现在几点了'))
复制代码
结果:
  1. 1 + 1 = 2
  2. The current time is 05:32 PM

  3. 进程已结束,退出代码为 0
复制代码
由于该模块只支持英文,我们使用中文语音时,需要将语音中的数字进行剥离,具体代码如下:
  1. def Split_num_letters(astr):
  2.     nums = []
  3.     astr = astr +'无'
  4.     num1 = ''
  5.     for i in range(len(astr)-1):
  6.         if astr[i].isdigit()== True and astr[i+1].isdigit()==False:
  7.             nums.append(num1)
  8.             num1 = ''
  9.         elif astr[i].isdigit() == False and astr[i+1].isdigit() == True:
  10.             num1 = num1 + astr[i+1]
  11.         elif astr[i].isdigit() == True and astr[i+1].isdigit() ==True:
  12.                 num1 = num1 + astr[i+1]
  13.     if astr[0].isdigit():
  14.         nums[0] = astr[0] + nums[0]
  15.     print(nums)
  16.     return nums


  17. Split_num_letters('你知道120乘20等于多少吗')
  18. Split_num_letters('120乘20等于多少吗')
复制代码

结果: 该函数配合其他函数使用,即可实现语音识别进行简单运算

  1. ['120', '20']

  2. 进程已结束,退出代码为 0
复制代码


回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-3-31 08:09 , Processed in 0.052898 second(s), 23 queries .

Powered by Qiqirobot

© 2001-2025 Discuz! Team.

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