2016-11-19 11 views
2

私は音声認識を使用して色のリストを確認しますが、リストにないと「色が見つかりません」と表示された場合は「色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つの成功した試み

私はそれがこれをやっている理由を知っているが、私はそれを回避する方法を把握することはできません。

答えて

0

こんにちは、私はPythonのsyntaxes..hereについて知らないC#のプログラマがC#で、私の考えですよ、それが `

variable succes_Counter 
variable unsuccess_Counter = 0 
boolean color_not found = true 
for i in range(len(colours)): 
    { 

      if colours[i] == voiceRecog 
      { 
       print("color found + colours[i] + ") 
       succes_Counter += 1 

       //HERE IT EXISTS FROM THIS LOOP 

       break 
      } 
      elseif voiceRecog == "quit" 

      //HERE IT EXISTS FROM THIS LOOP 
       break 


      else 
       if color_not found == true 
       { 
       print(" color not found") 

       unsuccess_Counter +=1 

       color_not found = false 
       } 
     } 


      color_not found = true 
      print("\nYou checked for", unsuccess_Counter, "colours and had", succes_Counter, "successful attempts") 
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") 
を作品にこれを試してください