A menudo no recordamos el día de la fecha debido a la carga de trabajo que estamos haciendo. Entonces, aquí hay un programa de Python con la ayuda de la cual podemos encontrar el día de la cita con solo un simple chat con nuestra computadora portátil o móvil.
Módulos necesarios
- DateTime: esta es una biblioteca en Python con la ayuda de la cual podemos manipular la fecha y la hora. Viene preinstalado con python para que no tengamos que instalarlo.
- pyttsx3: Esta es una biblioteca de conversión de texto a voz. Ayuda a comunicarse con el usuario. Este módulo no viene integrado con Python. Para instalarlo, escriba el siguiente comando en la terminal.
pip install pyttsx3
- SpeechRecognition: ayuda en el reconocimiento de voz. Este módulo no viene integrado con Python. Para instalarlo, escriba el siguiente comando en la terminal.
pip install SpeechRecognition
Ahora vamos a codificar El programa para decir la fecha con la ayuda del reconocimiento de voz.
Paso 1: Haz el método para tomar los comandos. Para que nuestro programa pueda tomar nuestros comandos.
Python3
import datetime import pyttsx3 import speech_recognition as sr def take_commands(): # Making the use of Recognizer and Microphone # Method from Speech Recognition for taking # commands r = sr.Recognizer() with sr.Microphone() as source: print('Listening') # seconds of non-speaking audio before # a phrase is considered complete r.pause_threshold = 0.7 audio = r.listen(source) try: print("Recognizing") # for listening the command in indian english Query = r.recognize_google(audio, language='en-in') # for printing the query or the command that we give print("the query is printed='", Query, "'") except Exception as e: # this method is for handling the exception # and so that assistant can ask for telling # again the command print(e) print("Say that again sir") return "None" return Query
Paso 2: Hacer el método Speak para que nuestro Programa nos hable.
Python3
def Speak(audio): # initial constructor of pyttsx3 engine = pyttsx3.init() # getter and setter method voices = engine.getProperty('voices') engine.setProperty('voice', voices[1].id) engine.say(audio) engine.runAndWait()
Paso 3: Decir el método del día
Python3
def tellDay(): # the weekday method is a method from datetime # library which helps us to an integer # corresponding to the day of the week # this dictionary will help us to map the # integer with the day and we will check for # the condition and if the condition is true # it will return the day day = datetime.datetime.today().weekday() + 1 Day_dict = {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday', 7: 'Sunday'} if day in Day_dict.keys(): day_of_the_week = Day_dict[day] print(day_of_the_week) Speak("The day is " + day_of_the_week)
Paso 4: El método principal que nos ayudará a ejecutar el programa
Python3
if __name__ == '__main__': command=take_commands() if "day" in command: tellDay()
Programa completo:
Python3
import datetime import pyttsx3 import speech_recognition as sr def take_commands(): # Making the use of Recognizer and Microphone # Method from Speech Recognition for taking # commands r = sr.Recognizer() with sr.Microphone() as source: print('Listening') # seconds of non-speaking audio before # a phrase is considered complete r.pause_threshold = 0.7 audio = r.listen(source) try: print("Recognizing") # for listening the command in indian english Query = r.recognize_google(audio, language='en-in') # for printing the query or the command that we give print("the query is printed='", Query, "'") except Exception as e: # this method is for handling the exception # and so that assistant can ask for telling # again the command print(e) print("Say that again sir") return "None" return Query def Speak(audio): # initial constructor of pyttsx3 engine = pyttsx3.init() # getter and setter method voices = engine.getProperty('voices') engine.setProperty('voice', voices[1].id) engine.say(audio) engine.runAndWait() def tellDay(): # the weekday method is a method from datetime # library which helps us to an integer # corresponding to the day of the week # this dictionary will help us to map the # integer with the day and we will check for # the condition and if the condition is true # it will return the day day = datetime.datetime.today().weekday() + 1 Day_dict = {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday', 7: 'Sunday'} if day in Day_dict.keys(): day_of_the_week = Day_dict[day] print(day_of_the_week) Speak("The day is " + day_of_the_week) # Driver Code if __name__ == '__main__': command=take_commands() if "day" in command: tellDay()
Producción:
Listening Recognizing the query is printed=' today's day ' Wednesday
Nota: También se crea una salida generada por voz.
Publicación traducida automáticamente
Artículo escrito por abhisheksrivastaviot18 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA