我使用语音识别并检查列表中的颜色,如果我说的不在列表中,则会显示“未找到颜色”#39;如果发现它显示“找到颜色”'我只希望它显示每条消息一次。 我遇到的问题是如何找到未找到的颜色' msg正确显示。
# 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")
以上是我的代码,下面是一个场景。
说点什么!
你说:蓝色 找到了颜色!说点什么!
你说:绿色 找到了颜色!说点什么!
你说:戴夫戴夫
说点什么!
你说:退出
您检查了2种颜色并成功尝试了2次
应该尝试3次,但尝试成功2次。
但如果我这样做,反过来说:
说点什么!
你说:戴夫戴夫
找不到颜色!
说点什么!
你说:Steve Steve
找不到颜色!
说点什么!
你说:红色 找到了颜色!说点什么!
你说:黑色 找到了颜色!说点什么!
你说:退出
您检查了4种颜色并成功尝试了2次
我知道这样做的原因,但我无法找到解决方法。
答案 0 :(得分:1)
# speech recognition
import speech_recognition as speech
#a lot of variables used for loops and conditions
voice = speech.Recognizer()
# Maintain status of each attempt. You can just use two variables successful_attempts and total_attempts also but status list might help you in maintaining more things like a list of tuples (color, result) etc
status = []
# successful_attempts, total_attempts = 0, 0
while True:
#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"]
if "quit" in voiceRecog:
break
elif voiceRecog in colours:
status.append(1)
# successful_attempts += 1
print("\nColour Found!\n")
else:
status.append(0)
print("\nColour not found!\n\n")
# total_attempts += 1
#once loop exited by saying quit, print attempts and successful attempts
print("\nYou checked for", len(status), "colours and had", sum(status), "successful attempts")
#print("\nYou checked for", total_attempts, "colours and had", successful_attempts, "successful attempts")
答案 1 :(得分:0)
嗨,我是一个c#程序员,我不知道python语法..我的想法在c#中试试这个有效吗?
<item name="windowActionBarOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>