-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
99 lines (88 loc) · 3.09 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import csv
import pandas as pd
import re
import os
import speech_recognition as sr
from fuzzywuzzy import fuzz
from fuzzywuzzy import process
from google.cloud import speech_v1p1beta1 as speech
archivo_csv = 'products.csv'
medications = []
def limpiar_nombre(nombre):
subcadena = ""
for caracter in nombre:
if caracter.isdigit():
break
subcadena += caracter
return subcadena
archivo_salida = 'medications.csv'
# Load medication names from CSV
with open(archivo_csv, newline='', encoding='utf-8') as csvfile:
# Leer el archivo CSV
lector_csv = csv.reader(csvfile, delimiter=';')
# Iterar sobre cada fila del archivo
for fila in lector_csv:
# Agregar el elemento de la segunda columna a la lista
medications.append(fila[1])
# Eliminar el encabezado
medications = medications[1:]
#print("Medications:", medications)
def recognize_speech_1():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
recognizer.adjust_for_ambient_noise(source) # Adjust for ambient noise
audio = recognizer.listen(source)
try:
print("Recognizing...")
text = recognizer.recognize_google(audio, language='en-US')
return text.lower()
except sr.UnknownValueError:
print("Sorry, I couldn't understand what you said.")
return ""
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
return ""
# def recognize_speech_2():
# client = speech.SpeechClient()
# config = {
# "language_code": "es-ES",
# "enable_automatic_punctuation": True,
# }
# with sr.Microphone() as source:
# print("Listening...")
# recognizer.adjust_for_ambient_noise(source) # Adjust for ambient noise
# audio = recognizer.listen(source)
# try:
# print("Recognizing...")
# audio_content = audio.get_wav_data()
# response = client.recognize(config=config, audio={"content": audio_content})
# text = response.results[0].alternatives[0].transcript
# return text.lower()
# except IndexError:
# print("Sorry, I couldn't understand what you said.")
# return ""
# except Exception as e:
# print("An error occurred: {}".format(e))
# return ""
def find_medication(input_text):
max_similarity = 0
best_match = None
print("Input text:", input_text)
for medication in medications:
similarity = fuzz.ratio(input_text, medication.lower())
if similarity > max_similarity:
print("Similarity:", similarity, "Medication:", medication)
max_similarity = similarity
best_match = medication
return best_match
if __name__ == "__main__":
while True:
user_input = recognize_speech_1()
if user_input:
print("You said:", user_input)
best_match = find_medication(user_input)
if best_match:
print("Best match:", best_match)
else:
print("No match found.")