Home > Enterprise >  Check if substring is in a string as a individual word
Check if substring is in a string as a individual word

Time:07-25

I am trying to create a voice assistant with Python. For many commands I use trigger words like "exit", "time", "quit" etc.

But since it keeps track of the voice, sometimes it picks up voice it shouldn't, like for the word "sentimental" it tells me the time. I have used word barriers (\\b), but I want singular words to work as well.

So "I acquitted him" wouldn't close the program. But "quit" or "please quit" would.

This is my current json setup:

{
    "BasicOperations": {
        "goodbye": ["'bye", "exit", "quit", "see you"],
        "time": ["time ", "getting late"],
        "calculations": [" ", "-", "*", "/", "^"]
    }

}

And this is the code for checking:

    # Tell the time
    if any(string in query for string in commands["time"]):
        engine.say('The time is '   time)
        engine.runAndWait()
    
    # Quit the program
    elif any(string in query for string in commands["goodbye"]):
        engine.say("Good night sir! Have a nice sleep." if is_night else "Bye sir, have a great day!" )
        engine.runAndWait()
        exit()
    
    elif any(string in query for string in commands["calculations"]):
        #op1, op2 = int(op1), int(op2)

        for op in commands["calculations"]:
            if (op in query):
                print(op)

        #answer =  get_operator_fn()(op1, op2)
        engine.say("Good night sir! Have a nice sleep." if is_night else "Bye sir, have a great day!" )
        engine.runAndWait()
        exit()

CodePudding user response:

Use re.search with starting and ending anchors:

# Tell the time
regex = r'^(?:'   r'|'.join(re.escape(x) for x in commands["time"])   r')$'
if re.search(regex, query):
    engine.say('The time is '   time)
    engine.runAndWait()

# Quit the program
regex = r'^(?:'   r'|'.join(re.escape(x) for x in commands["goodbye"])   r')$'
elif re.search(regex, query):
    engine.say("Good night sir! Have a nice sleep." if is_night else "Bye sir, have a great day!" )
    engine.runAndWait()
    exit()

regex = r'^(?:'   r'|'.join(re.escape(x) for x in commands["calculations"])   r')$'
elif re.search(regex, query):
    #op1, op2 = int(op1), int(op2)

    for op in commands["calculations"]:
        if (op in query):
            print(op)

    #answer =  get_operator_fn()(op1, op2)
    engine.say("Good night sir! Have a nice sleep." if is_night else "Bye sir, have a great day!" )
    engine.runAndWait()
    exit()

CodePudding user response:

Define a function which can be called in the for loop condition, which will have checks for the following:

  • If the string is the first word in the query or the letter in the previous index is a space
  • If the string is the last word in the query or the letter in the next index is a space

If both these conditions are true, return True else return False.

def present(string, query):
    ind = query.find(string)
    if ind != -1:
        if ind==0 or (ind>0 and query[ind-1]==' '):
            if ind len(string)==len(query) or (query[ind len(string)]==' '):
                return True
    return False

#Tell the time
    if any(present(string, query) for string in commands["time"]):
        engine.say('The time is '   time)
        engine.runAndWait()
    
    # Quit the program
    elif any(present(string, query) for string in commands["goodbye"]):
        engine.say("Good night sir! Have a nice sleep." if is_night else "Bye sir, have a great day!" )
        engine.runAndWait()
        exit()
    
    elif any(present(string, query) for string in commands["calculations"]):
        #op1, op2 = int(op1), int(op2)

        for op in commands["calculations"]:
            if (op in query):
                print(op)

        #answer =  get_operator_fn()(op1, op2)
        engine.say("Good night sir! Have a nice sleep." if is_night else "Bye sir, have a great day!" )
        engine.runAndWait()
        exit()

  • Related