I'm not even sure how to explain what I'm trying to do in words, so I shall use examples:
I want to take a set of options in a string, like this (the format can change if it's easier that way):
is (my|the) car (locked|unlocked) (now)?
And have it spit out the following list of strings:
is my car locked
is the car locked
is my car unlocked
is the car unlocked
is my car locked now
is the car locked now
is my car unlocked now
is the car unlocked now
I need to be able to do this for a an Alexa application since it doesn't accept regex for natural language processing (WHY!?).
Thanks in advance.
What you probably want is itertools.product()
. For example, you might use it like this:
import itertools
# set up options
opt1 = ["my", "the"]
opt2 = ["locked", "unlocked"]
opt3 = [" now", ""] # You'll have to use an empty string to make something optional
# The sentence you want to template
s = "is {} car {}{}?"
# Do all combinations
for combination in itertools.product(opt1, opt2, opt3):
print(s.format(*combination))
This prints:
is my car locked now?
is my car locked?
is my car unlocked now?
is my car unlocked?
is the car locked now?
is the car locked?
is the car unlocked now?
is the car unlocked?