Python Forum
if statement follow up questions
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if statement follow up questions
#1
so here I'm working on follow up if statements it works good with input() as it stands but what im trying to do is when use audio to ask the follow up questions and exe them.

I can get a response and exe a command with audio no problem however when im trying to ask a follow up question I can't get it to work

for example

wake word = ada
she is listening now
if I say 'lights' she turns them on
but I want her to say right after that 'would you like me to dim them?
if I say 'yes' she will use the 50% dim cmd

I can get this to work using the input() way I have set up below just not with audio any idea

from phue import Bridge
from os import system
import speech_recognition as sr
import warnings
import random
b = Bridge('xxx.xxx.x.x')  # bridge ip adress
b.connect()  # connect to the bridge
b.get_api()
warnings.filterwarnings('ignore')
def recordaudio():
    #record audio
    r = sr.Recognizer()  # creating recognizer
    # open microphone and start recording
    with sr.Microphone() as source:
        print('listening:')# for testing perpouses

        #r.pause_threshold = .7

        audio = r.listen(source)
    # use google speech recegnizor
    data = '' #the audio that is said
    try:
        data = r.recognize_google(audio)
        print('you said: '+data) # 'you said' + the data that was said
    except sr.UnknownValueError:
        print('google speech recognition could not understand the audio, unknown error')
    except sr.RequestError as e:
        print('request results from google speech recognition service error'+ e)
    return data

#-------------function to get the AI response---------#
def assistantResponse (text):
    print(text)
    system("say {}".format(text))


#--------------what is your name-------------#
def name(text):
    WAKE_WORD = ['what is your name']
    text = text.lower()
    for phrase in WAKE_WORD:
        if phrase in text:
            return True
    return False


#-------------creating a function for wake word(s) or phrase------------#
def wakeWord(text):
    WAKE_WORDS = ['eta', 'ada', 'hey ada', 'hi ada', 'aida', 'haida',] #list of wake words
    text = text.lower() #converting text to all lower case words
    #check to see if the users command or text contains a wake word/phrase
    for phrase in WAKE_WORDS:
        if phrase in text:
            return True
    #if the wake word isnt found in the text from the loop it returns false
    return False

def my_room_light(My_room_l, My_room_l_bri):
    b.set_light([1], 'on', My_room_l)
    b.set_light([1], 'bri', My_room_l_bri)


# num = input("Enter number :")
# print(num)
# name1 = input("Enter name : ")
# print(name1)
#
# # Printing type of input value
# print("type of number", type(num))
# print("type of name", type(name1))

def greeting(text):
    GREETING_INPUTS = ['hi', 'hey', 'hello', 'greetings',]
    GREETING_RESPONSES = ['hi', 'hello how are you', 'hello', 'hi how are you']
    #if the users input is a greeting then return a randomly chosen greeting response
    for word in text.split():
        if word.lower() in GREETING_INPUTS:
            return random.choice(GREETING_RESPONSES) +'.'
    #if no greeting is detected then return an empty string
    return ''


def yes(text):
    YES = ['yes']
    text = text.lower()
    for phrase in YES:
        if phrase in text:
            return True

    return False


def no(text):
    NO = ['no']
    text = text.lower()

    for phrase in NO:
        if phrase in text:
            return True

    return False


def lower(text):
    HIGHER = ['higher', 'brighter', ]
    text = text.lower()

    for phrase in HIGHER:
        if phrase in text:
            return True

    return False


def higher(text):
    LOWER = ['lower', 'dim', 'dimmer',]
    text = text.lower()

    for phrase in LOWER:
        if phrase in text:
            return True

    return False

while True:
    # ---------------always listen to wake word--------------#
    # record the audio
    # text = recordaudio()
    # response = ''
    # -----------start check for wake word or phrase-----------#
    text = input('cmd: ')
    # if (wakeWord(text) or name(text) == True):  # added or name(text)
    #     # check for greeting by user
    #     response = response + greeting(text)



    if 'on' in text:
        my_room_light(True, 254)
        # response = 'would you like me to dim them:'
        text = input('would you like me to dim them:')

        if higher(text) == True:
            print(text)
            my_room_light(True, 191)

        if lower(text) == True:
            print(text)
            my_room_light(True, 64)

        if no(text) == True:
            print('okay')

        if yes(text) == True:
            my_room_light(True, 127)
            print(text)
            
            text = input('sufficient?:')

            if no(text) == True:

                
                text = input('would you like me to make them lower?')

                if higher(text) == True:
                    print(text)
                    my_room_light(True, 191)
                elif yes(text) == True:

                    my_room_light(True, 64)


            elif yes(text) == True:
                print(text)
                


            elif higher(text) == True:
                print(text)
                my_room_light(True, 191)

            elif lower(text) == True:
                print(text)
                my_room_light(True, 64)
                
    # assistantResponse()
Reply
#2
Does the google side need a specific prompt? When first invoked, the function prompts with "cmd: ". But after turning on, it prompts with "would you like me to dim them:". Do you have logs on the google side to tell what it's receiving from your function and what it's sending?
Reply
#3
idk anything about the google side the 'cmd' was just something random I used to access the text with an input and I # out all the audio. if I activate the audio I use the wake word which is ada but it will listen for an if statement command. I can say lights and it will turn them on. still, I want it to ask a follow up question with in that if statement.

maybe it because the while loop thinks it completed the task and returns to the top waiting for the wake word again before it will do any other commands but I want it to follow down that path with a follow up command until the whole task is actually completed that's the only thing I can think of that is happening

because with the input('cmd') if I type 'on' it turns them on and follows up with ('would you like me to dim them') if I type yes in that input it will dim them, it will follow that path. it won't go back to the original ('cmd') that made me go down that 'on' path and that's exactly what I want just can't get it to work with audio
Reply
#4
The problem was with how the responses were set up I got it
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Spyder IDE - make variable explorer to follow the color scheme of the Editor Antonio 0 5,395 May-05-2018, 10:20 PM
Last Post: Antonio
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 34,269 Nov-25-2017, 06:21 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020