私は音声認識を使用して色のリストを確認しますが、リストにないと「色が見つかりません」と表示された場合は「色Found '私は各メッセージを一度しか表示したくない。 私が抱えている問題は、「色が見つかりません」というメッセージを正しく表示する方法です。音声認識結果が表示されない場合のメッセージの印刷
# speech recognition
import speech_recognition as speech
#a lot of variables used for loops and conditions
voice = speech.Recognizer()
condition = False
condition2 = False
condition3 = False
boolean = False
counter = 0
counter2 = 0
while condition == False:
#with microphone input as the input
with speech.Microphone() as source:
print("Say something!")
#variable = the input from the microphone
audio = voice.listen(source)
voiceRecog = voice.recognize_google(audio)
try:
#print the word that was heard
print("\nYou said: " + voiceRecog)
except speech.UnknownValueError:
#if couldnt recognise then print this msg
print("Sorry, we couldn't make that out. Try again!")
except speech.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
#list of colours for comparing
colours = ["blue", "red", "Green", "Orange", "yellow", "pink", "purple", "black"]
#loop to check the word spoken against each word in the list
for i in range(len(colours)):
#so that if colour found it doesnt claim colour not found
if boolean == False:
condition2 = False
#increase counters by 1 and change booleans to True
#if spoken word matches any words in the list then output
if colours[i] == voiceRecog:
boolean = True
condition2 = True
condition3 = True
counter += 1
counter2 += 1
print("\nColour Found!\n")
#if user says "quit" then all booleans = True (exit all loops)
elif voiceRecog == "quit":
boolean = True
condition = True
condition2 = True
condition3 = True
#if none of other conditions check value of i and of con2
else:
#if end of list is reached and con2 = False then con3 = False
if (i + 1) == len(colours) and condition2 == False:
condition3 = False
print(end = "")
#if con3 = False then counter increase and print 'colour not found'
if condition3 == False:
print("\nColour not found!\n\n")
counter += 1
#once loop exited by saying quit, print attempts and successful attempts
print("\nYou checked for", counter, "colours and had", counter2, "successful attempts")
上記は私のコードであり、以下は1つのシナリオです。
何かを言います!
あなたは言った:青
色が見つかりました!
何か言ってください!
あなたは言った:緑
色が見つかりました!
何か言ってください!
あなたは言った:デイブデイブ
は、何かを言います!
あなたは言った:あなたは2色をチェックし、3回の試行が、2つの成功した試みがあるはずです2つの成功した試み
を持っていた
を終了します。
しかし、私はそれを他の方法で回避を行う場合:
は、何かを言います!
あなたは言った:デイブデイブ
カラーが見つかりません!
何か言ってください!
あなたは言った:スティーブスティーブ
色が見つかりません!
何か言ってください!
あなたは言った:赤
色が見つかりました!
何か言ってください!
あなたは言った:黒
色が見つかりました!
何か言ってください!
あなたは言った:あなたは4色をチェックしていた
を辞め2つの成功した試み
私はそれがこれをやっている理由を知っているが、私はそれを回避する方法を把握することはできません。