加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Voice Command Calculator 1.00 KB
一键复制 编辑 原始数据 按行查看 历史
kunalkaushik987 提交于 2020-10-08 16:24 . Create Voice Command Calculator
import operator
import speech_recognition as s_r
print("Your speech_recognition version is: "+s_r.__version__)
r = s_r.Recognizer()
my_mic_device = s_r.Microphone(device_index=1)
with my_mic_device as source:
print("Say what you want to calculate, example: 3 plus 3")
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
my_string=r.recognize_google(audio)
print(my_string)
def get_operator_fn(op):
return {
'+' : operator.add,
'-' : operator.sub,
'x' : operator.mul,
'divided' :operator.__truediv__,
'divided by' :operator.__truediv__,
'divide' :operator.__truediv__,
'Divided' :operator.__truediv__,
'Divided by' :operator.__truediv__,
'Divide' :operator.__truediv__,
'Mod' : operator.mod,
'mod' : operator.mod,
'^' : operator.xor,
}[op]
def eval_binary_expr(op1, oper, op2):
op1,op2 = int(op1), int(op2)
return get_operator_fn(oper)(op1, op2)
print(eval_binary_expr(*(my_string.split())))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化