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