RECONOCIMIENTO / app.py
mariavhrh's picture
Update app.py
584d3e4
import streamlit as st
from bokeh.models.widgets import Button
from bokeh.models import CustomJS
from streamlit_bokeh_events import streamlit_bokeh_events
# Load pandas
import pandas as pd
import numpy as np
from nltk.stem import SnowballStemmer
from inflector import Inflector, English, Spanish
inflector = Inflector(Spanish)
import unidecode
import unicodedata
n=1
p=0
query="none"
df = pd.read_excel('preguntas_qh_tags2.xlsx',engine="openpyxl")
def remove_accents(input_str):
nfkd_form = unicodedata.normalize('NFKD', input_str)
return u"".join([c for c in nfkd_form if not unicodedata.combining(c)])
#var recognition = new webkitSpeechRecognition();
#precission=50
#precission = st.text_input('Input % of tags in sentence (0-100):')
stt_button = Button(label="Speak")#, width=100)
#stt_button = st.button(label="Speak")
stt_button.js_on_event("button_click", CustomJS(code="""
var recognition = new webkitSpeechRecognition();
recognition.lang = "es-ES";
recognition.continuous = true;
recognition.interimResults = true;
recognition.onresult = function (e) {
var value = "";
for (var i = e.resultIndex; i < e.results.length; ++i) {
if (e.results[i].isFinal) {
value += e.results[i][0].transcript;
}
}
if ( value != "") {
document.dispatchEvent(new CustomEvent("GET_TEXT", {detail: value}));
}
}
recognition.start();
"""))
result = streamlit_bokeh_events(
stt_button,
events="GET_TEXT",
key="listen",
refresh_on_update=False,
override_height=75,
debounce_time=0)
#import QuirohelpTagsMethod as QHT
placeholder = st.empty()
# Replace the placeholder with some text:
placeholder.text("Escuchando...")
if result:
if "GET_TEXT" in result:
#result2=QHT.Look4tag(result.get("GET_TEXT"))
placeholder.text("Entendido!")
st.text("Entendido!")
# Clear all those elements:
placeholder.empty()
st.write(result.get("GET_TEXT"))
#st.write(result2)
#st.write("PEDRO")
#spanish_stemmer = SnowballStemmer('spanish')
#query=(spanish_stemmer.stem(result.get("GET_TEXT")))
query=result.get("GET_TEXT")
###################################
#STEMATIZAR QUERY
###################################
if query!="none":
if query!="salir":
st.text("query: "+query)
query = query.lower()
st.text("query: "+query)
result=query.split()
st.text("query: "+query)
df2=[]
for index,row in df.iterrows():
list_words=df.loc[index,'TAGS2']#busca columna tag2
list_words = list_words.split(",")
df2.append(list_words)
#spanish_stemmer = SnowballStemmer('spanish')
#query=(spanish_stemmer.stem(query))
ls5=[]
list_words=[]
for indexw, word in enumerate(result):
result[indexw]=inflector.singularize(str(word))
st.text("result:" + str(result))
count_words = np.zeros(len(df.index), dtype=int)
for index1,row in enumerate(df2):
ls4=[]
#list_words=df.loc[index,'TAGS2']#busca columna tag2
#print("list words: "+str(list_words))
#list_words = list_words.translate({ ord("?"): None })
#list_words = list_words.translate({ ord("¿"): None })#quitan los interrogantes
#print("list words: "+str(list_words))
#ls = list_words.split(",")
#print("ls: "+str(ls))
#ls = [string for string in ls if string != ""]
for word in row :
if word!=[]:
for num in range (100):
num=num/10
num=str(num)
if word==num:
num = num.split(".")
ls4.append(num[0])
ls4.append("con")
ls4.append(num[1])
p=p+1
if p==0:
ls4.append(word)
p=0
ls5.append(ls4)
#WE CHECK THE FIRST WORD (QUESTION CLAUSE)
if str(remove_accents(ls5[index1][0]))==remove_accents(result[0]):# and ls[1]==first_word:
for resulted in result[1:]:
#print("word: "+word)#el tag2 con el que lo compara
#print("ls[1:] :"+str(ls[1:]))#la fila de tag2 con las que lo compara
for index2,word in enumerate(ls5[index1][1:]) :
if str (resulted )=="exactitec"or str (resulted )=="exactite"or str (resulted )=="exactec":
resulted ="exactech"
if str(resulted)=="h":
resulted="dh"
if str(resulted)=="colón":
resulted="coron"
if str(resulted)=="trave" or str(resulted)=="travé":
resulted="nope"
if str(resulted)=="lasersolvo"or str(resulted)=="lásersolvo"or str(resulted)=="lásersolbo"or str(resulted)=="lasersolbo":
resulted="solvo"
if str(resulted)=="solbo":
resulted="solvo"
if str(resulted)=="maya":
resulted="malla"
if str(resulted)=="pilos"or str(resulted)=="pilo":
resulted="philo"
if str(resulted)=="filos"or str(resulted)=="filo":
resulted="philo"
if str(resulted)=="sinces" or str(resulted)=="sinc":
resulted="synthe"
if str(resulted)=="sintes" or str(resulted)=="sint":
resulted="synthe"
if str(resulted)=="axos" or str(resulted)=="axo":
resulted="axso"
if str(resulted)=="uno":
resulted="1"
#print("resulted:"+str(resulted))
if str(resulted)=="dos" or str(resulted)=="do":
resulted="2"
#print("resulted:"+str(resulted))
if str(resulted)=="tres" or str(resulted)=="tr":
resulted="3"
#print("resulted:"+str(resulted))
if str(resulted)=="cuatro"or str(resulted)=="cuatr":
resulted="4"
#print("resulted:"+str(resulted))
if str(resulted)=="cinco"or str(resulted)=="cinc":
resulted="5"
#print("resulted:"+str(resulted))
if str(resulted)=="seis"or str(resulted)=="sei":
resulted="6"
#print("resulted:"+str(resulted))
if str(resulted)=="siete":
resulted="7"
#print("resulted:"+str(resulted))
if str(resulted)=="ocho"or str(resulted)=="och":
resulted="8"
#print("resulted:"+str(resulted))
if str(resulted)=="nueve"or str(resulted)=="nuev":
resulted="9"
#print("resulted:"+str(resulted))
if str(resulted)=="cero":
resulted="0"
#WE THEN CHECK HOW MANY WORDS IN THE QUERY ARE IN NURSES QUESTIONS
#print(index1)
if str(remove_accents(word)).lower() == str(remove_accents(resulted)).lower():
count_words[index1]=count_words[index1]+1
ls5[index1].pop(index2)
indexmax = np.argwhere(count_words == np.amax(count_words))
all_zeros = not np.any(indexmax)
#st.text(count_words)
if not np.all(count_words==0):
for indexin in indexmax:
st.text(indexin)
st.text(count_words[indexin])
st.text("Si has preguntado...\n")
st.text(df.iloc[indexin,2])
st.text("La respuesta es...\n")
st.text(df.iloc[indexin,3])
#if query=="salir":
#break
print("GRACIAS!")