def notes_command(mode, space=""): cm = Communication() def end(): cm.voice(finish) while(True): #Beep sound to notify the user when to speak playsound.playsound('Sound.mp3') command = cm.recognize_speech_from_mic() if command["transcription"]: break if not command["success"]: break cm.voice("I didn't catch that. What did you say?\n") if "yes" in command["transcription"]: cm.voice("Very well! Appending punctuation!") punctuation() elif "no" in command["transcription"]: notes_command('a', "\n") else: cm.voice("Sorry, that's not a command.") end() def punctuation(): readfile = open('note.txt', 'r') writefile = open('note.txt', 'a') for line in readfile: if line.find("period") != -1: writefile.replace("period", ". ") if line.find("comma") != -1: writefile.replace("comma", ", ") if line.find("exclamation mark") != -1: writefile.replace("exclamation mark", "! ") if line.find("question mark") != -1: writefile.replace("question mark", "? ") if line.find("newline") != -1: writefile.replace("newline", "\n") """#rtn = re.split('([.!?] *)', line) rtn = ([line[i:i+1] for i in range(0, len(line), len(line))]) #final = ''.join([i.capitalize() for i in rtn]) writefile.replace(rtn, rtn.capitalize())""" notes = open('note.txt', mode) start = "Ready when you are!" finish = "Will that be all?" cm.voice(start) while(True): #Beep sound to notify the user when to speak playsound.playsound('Sound.mp3') command = cm.recognize_speech_from_mic() if command["transcription"]: break if not command["success"]: break cm.voice("I didn't catch that. What did you say?\n") notes.write(space + "{}".format(command["transcription"].capitalize())) notes.close() end()
def listen(): cm = Communication() WORDS = ["Open", "System", "Notes", "Manual", "Weather", "Joke"] misunderstand = "I didn't catch that. What did you say?" sorry = "Sorry, I can't do that." command_is_correct = False def model(text): cm = Communication() # This function will pass your text to the machine learning model # and return the top result with the highest confidence def classify(text): key = "653eced0-1840-11ea-97a0-956abe0146c6b32257f5-24cc-4bfa-b944-21223a9adadc" url = "https://machinelearningforkids.co.uk/api/scratch/" + key + "/classify" response = requests.get(url, params={"data": text}) if response.ok: responseData = response.json() topMatch = responseData[0] return topMatch else: response.raise_for_status() # CHANGE THIS to something you want your machine learning model to classify demo = classify(text) label = demo["class_name"] return label while (True): #Beep sound to notify the user when to speak pygame.mixer.music.load('Sound.mp3') pygame.mixer.music.set_volume(0.3) pygame.mixer.music.play() time.sleep(1) pygame.mixer.music.stop() command = cm.recognize_speech_from_mic() error = "ERROR: {}".format(command["error"]) speech = "You said: {}".format(command["transcription"]) if command["transcription"]: break if not command["success"]: break cm.voice(misunderstand) # if there was an error, stop the game if command["error"]: cm.voice(error) # show the user the transcription #cm.voice(speech) if WORDS[0] in command["transcription"]: open_command(command["transcription"]) elif WORDS[1] in command["transcription"]: system_command(command["transcription"]) elif WORDS[3] in command["transcription"]: notes_command('w') elif WORDS[4] in command["transcription"]: weather_command() elif WORDS[5] in command["transcription"]: jokes_command() else: cm.voice(sorry) listen()