Cap98
Cap98

Reputation: 21

Printing message when speech recognition result is not expected

I use voice recognition and check a list for a colour, if what I said isn't in the list then it displays 'Colour not found' if it is found it displays 'Colour Found' I only want it to display each message once. The problem I am having is how to get the 'Colour not found' msg to display correctly.

# 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")

Above is my code and below is one scenario.

Say something!

You said: blue

Colour Found!

Say something!

You said: Green

Colour Found!

Say something!

You said: Dave Dave

Say something!

You said: quit

You checked for 2 colours and had 2 successful attempts

There should be 3 attempts, but 2 successful attempts.

But if i do it the other way around:

Say something!

You said: Dave Dave

Colour not found!

Say something!

You said: Steve Steve

Colour not found!

Say something!

You said: red

Colour Found!

Say something!

You said: black

Colour Found!

Say something!

You said: quit

You checked for 4 colours and had 2 successful attempts

I know the reason it is doing this, but i cannot figure out a way around it.

Upvotes: 2

Views: 654

Answers (2)

Aditya
Aditya

Reputation: 630

# 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")

Upvotes: 1

Sivashankar
Sivashankar

Reputation: 554

hi i am a c# programmer i dont know about python syntaxes..here is my idea in c# try this it works `

    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")

Upvotes: 0

Related Questions