entry="Where in the world is Carmen San Diego"
goal=["Where in the", "world is", "Carmen San Diego"]
I am trying to create a procedure that will search for chunks of words within "entry" that are members of the "goal" list. I would like to preserve word order in these subsets.
This is what I have so far. I'm not really sure how to complete this or if I'm approaching it the right way.
span=1
words = entry.split(" ")
initial_list= [" ".join(words[i:i+span]) for i in range(0, len(words), span)]
x=len(initial_list)
initial_string= " ".join(initial_list)
def backtrack(A,k):
if A in goal:
print
else:
while A not in goal:
k=k-1
A= " ".join(initial_list[0:k])
if A in goal:
print A
words=A.split(" ")
firstmatch= [" ".join(words[i:i+span]) for i in range(0, len(words), span)]
newList = []
for item in initial_list:
if item not in firstmatch:
newList.append(item)
nextchunk=" ".join(newList)
backtrack(initial_string,x)
The output so far is just this:
"Where in the"
Desired Output:
"Where in the"
"world is"
"Carmen San Diego"
I've been spinning my wheels trying to find a proper algorithm for this, and I think it requires either backtracking or search pruning, I'm not really sure. Ideally, a solution would work for any "entry" and "goal" list. Any comments are much appreciated.