Python Speech Recognition (Calculator)

The first step is about importing the module. I tried several methods and found that it seems that the win7 system does not have a speech recognition function. I use win10 and need to download a package again.

In this way, the win32com.client module can be used


import win32com.client

# # 1. Create a speaker object
# speaker = win32com.client.Dispatch("SAPI.SpVoice")
# # 2. Through this speaker object, you can directly play the corresponding voice string
# speaker.Speak("My name is: Wang Yunlong")
class Caculator:

def __check_num_zsq(func):

def inner(self, n):
if not isinstance(n,int):
raise TypeError("There is a problem with the current data type , should be an integer data")
return func(self, n)
return inner
def __say(self,word):
# 1. Create a speaker object
speaker = win32com.client.Dispatch("SAPI.SpVoice")
# 2. Through this speaker object, you can directly play the corresponding speech string
speaker.Speak(word)

def __create_say_zsq(word = ""):
def __say_zsq(func):
def inner(self, n):
self.__say(word + str(n))
return func(self, n)

return inner
return __say_zsq

@__check_num_zsq
@__create_say_zsq()
def __init__(self,num):
self.__result = num

@__check_num_zsq
@__create_say_zsq("加")
def jia(self,n):
self.__result += n
return self

@__check_num_zsq
@__create_say_zsq("减去")
def jian(self,n):
self.__result -= n
return self

@__check_num_zsq
@__create_say_zsq("乘以")
def chen(self,n):
self.__result *= n
return self

def show(self):
self.__say("The result of the calculation is: %d"%self.__result)
print("The result of the calculation is: %d"%self.__result)
return self

def clear(self):
self.__result = 0
return self

@property
def result(self):
return self.__result
c1 = Caculator(10)
# The idea of ​​chain programming can be done with return self itself
c1.jia(6 ).jian(4).chen(5).show().clear().jia(555).jian(500).show()
c1.result

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324382078&siteId=291194637