进行数学运算和时间查询:- # -*- coding: utf-8 -*-
- from chatterbot import ChatBot
-
- bot = ChatBot(
- "Math & Time Bot",
- logic_adapters=[
- "chatterbot.logic.MathematicalEvaluation",
- "chatterbot.logic.TimeLogicAdapter"
- ],
- input_adapter="chatterbot.input.VariableInputTypeAdapter",
- output_adapter="chatterbot.output.OutputAdapter"
- )
-
-
- def chot_math_time(text=''):
- response = bot.get_response(text)
- return response
-
- print(chot_math_time('what is 1 + 1'))
- print(chot_math_time('现在几点了'))
复制代码 结果:- 1 + 1 = 2
- The current time is 05:32 PM
-
- 进程已结束,退出代码为 0
复制代码 由于该模块只支持英文,我们使用中文语音时,需要将语音中的数字进行剥离,具体代码如下:- def Split_num_letters(astr):
- nums = []
- astr = astr +'无'
- num1 = ''
- for i in range(len(astr)-1):
- if astr[i].isdigit()== True and astr[i+1].isdigit()==False:
- nums.append(num1)
- num1 = ''
- elif astr[i].isdigit() == False and astr[i+1].isdigit() == True:
- num1 = num1 + astr[i+1]
- elif astr[i].isdigit() == True and astr[i+1].isdigit() ==True:
- num1 = num1 + astr[i+1]
- if astr[0].isdigit():
- nums[0] = astr[0] + nums[0]
- print(nums)
- return nums
-
-
- Split_num_letters('你知道120乘20等于多少吗')
- Split_num_letters('120乘20等于多少吗')
复制代码结果: 该函数配合其他函数使用,即可实现语音识别进行简单运算 - ['120', '20']
-
- 进程已结束,退出代码为 0
复制代码
|