Question

I am creating a program that you talk into, to give it a command which it then runs. The problem is that when I run the program and I talk into it, it might capitalize some letters that I don't have capitalized in the code, so then it won't run the command. Is there a way to make python not worry about if the words are capitalized? I have already tried capitalizing names but it seems to capitalize them when it wants to. Code:

import speech
words = {"test1", "hello"}
test = speech.input("test: ")
tokens = test.split()
if words.intersection(tokens):
    print 'hi'
else:
    print 'test'
Was it helpful?

Solution

Here's an idea: convert the input into lowercase before making any comparisons:

tokens = test.lower().split()

And make sure that all the elements in words are in lowercase, too:

words = { e.lower() for e in words }

By doing the above, we're comparing only lowercase characters throughout the program, so capitalization should not be a problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top