ok, so I had this code to run speech recognition and gather an utterance. then wanted to check that with a word that is already saved in a database.
def recog():
r = sr.Recognizer()
print("Speak Now:")
with sr.Microphone() as source:
audio_data = r.listen(source)
try:
print("recognizing")
recog.result = str(r.recognize_wit(audio_data, key="XDAYDVOGVTRP652K6TAWOWQR73WHJY6S"))
print(recog.result)
return recog.result
except sr.UnknownValueError:
print('Recognition error')
I've used this code segment below to retrieve the word needed to compare with.
conn = sqlite3.connect('VIT.db')
c = conn.cursor()
post_ac = "select ans from ansc where id=" + str(i)
c.execute(post_ac)
conn.commit()
records = c.fetchone()
print(records)
print_c = ''+str(records)
conn.close()
Then I've used an if statement to check if they match
if print_c == recog():
print("Correct")
else:
print("Incorrect")
but this code always gives me "correct" as the answer. What have I done wrong and how can I improve my code? I'm fairly new to python and tkinter, so excuse my mistakes. p.s: fyi the data is retrieved successfully and correctly from the database and also from the recognition. I'm having the trouble with comparison is all.
after a while I kinda figured out what's wrong and I've changed the code. All this time it was comparing
'string'
instead of
string
conn = sqlite3.connect('VIT.db')
c = conn.cursor()
post_ac = "select ans from ansc where id=" + str(i)
c.execute(post_ac)
records = c.fetchone()
print_c = str(records[0])
print(print_c)
so all I had to do was change print_c = ''+str(records)
into print_c = str(records[0])
, so it will compare correctly in the if statement. Thanks for anyone tried to answer and hope this helps for anyone needed.