Amazing-Python-Scripts
105 строк · 2.8 Кб
1import os2import pyttsx33import speech_recognition as sr4import tkinter.messagebox as tmessage5import wolframalpha6
7from os.path import exists8
9listener = sr.Recognizer()10engine = pyttsx3.init()11voices = engine.getProperty('voices')12engine.setProperty('voice', voices[0].id)13wolfprimeaplahe_app = input('Enter the API Token')14
15
16def audio(audio):17engine.say(audio)18engine.runAndWait()19
20
21def welcomeInst():22print('Welcome to Calculator :)')23audio('Welcome to Calculator :)')24print('If you want calculate something please tell calcualte and then your expression')25audio('If you want calculate something please tell calcualte and then your expression')26print('For example CALCULATE 7 PLUS 8 or CALCULATE sin30 plus cot20')27audio('For example CALCULATE 7 PLUS 8 or CALCULATE sin30 plus cot20')28
29
30def _takeCommand():31r = sr.Recognizer()32with sr.Microphone() as source:33print("Listening....")34audio("Listning...")35r.pause_threshold = 236r.energy_threshold = 300037audio = r.listen(source)38
39try:40print("Recognizing...")41audio("Recognizing...")42query = r.recognize_google(audio, language='en-In')43print(query)44
45except Exception as e:46tmessage.showinfo('Error', f'{e}')47print("Didn't understand you...\nCan you repeat?...")48return "NONE"49
50return query51
52
53def _calculate():54client = wolframalpha.Client(wolfprimeaplahe_app)55indx = spech.lower().split().index('calculate')56query = spech.split()[indx + 1:]57res = client.query(''.join(query))58answerr = next(res.results).text59space = '\n'60ourQuery = ''.join(query)61Question = 'Your Query was :- '62Answer = 'Your answer was :- '63finalAnswer = Question + str(ourQuery) + \64space + Answer + str(answerr) + space65
66if exists('./Voice Calculator/maths.txt'):67with open('./Voice Calculator/maths.txt', 'a', encoding='utf-8') as mth:68mth.write(finalAnswer)69mth.close()70else:71history = open('./Voice Calculator/maths.txt', 'w', encoding='utf-8')72history.write(finalAnswer)73history.close()74print("The answer is " + answerr)75audio("the answer is %s" % answerr)76
77
78welcomeInst()79
80while True:81
82spech = _takeCommand().lower()83
84if 'calculate' in spech:85_calculate()86
87elif 'clear' in spech:88
89if exists('./Voice Calculator/maths.txt'):90with open('./Voice Calculator/maths.txt', 'r+') as file:91file.truncate(0)92file.close()93print('done')94
95else:96tmessage.showinfo('Error', 'No file exists with this name')97
98elif 'history' in spech:99os.system('./Voice Calculator/maths.txt')100
101elif 'quit' in spech or 'exit' in spech:102quit()103
104else:105tmessage.showinfo('Opps', "Didn't understand")106