Reputation: 9840
I have run into a logic problem.
I have a string declared as follows:
fruits = "banana grapes apple"
vegetables = "potatoes cucumber carrot"
Now there are some text sentences, and i have to search for the word that is in front of the text format <vegetables> <fruits>
I ate carrot grapes ice cream for dessert.
Answer : ate
Dad and mom brought banana cucumber and milk.
Answer : brought
What i was thinking is to split the sentence and put it in an array, and then look for the sequence, i was able to break the sentence but matching the sequence is a problem.
wd = sentence.split(' ')
for x in wd.strip().split():
# now i will have to look for the sequence
Now, i will have to look for the text that is in front of the text format
Upvotes: 1
Views: 103
Reputation: 250881
You're using wrong data-structures here, convert fruits and vegetables to sets. Then the problem is very easy to solve:
>>> fruits = set("banana grapes apple".split())
>>> vegetables = set("potatoes cucumber carrot".split())
>>> fruits_vegs = fruits | vegetables
>>> from string import punctuation
def solve(text):
spl = text.split()
#use itertools.izip and iterators for memory efficiency.
for x, y in zip(spl, spl[1:]):
#strip off punctuation marks
x,y = x.translate(None, punctuation), y.translate(None, punctuation)
if y in fruits_vegs and x not in fruits_vegs:
return x
...
>>> solve('I ate carrot grapes ice cream for dessert.')
'ate'
>>> solve('Dad and mom brought banana cucumber and milk.')
'brought'
>>> solve('banana cucumber and carrot.')
'and'
Upvotes: 2
Reputation: 14098
fruits = "banana grapes apple".split(" ")
vegetables = "potatoes cucumber carrot".split(" ")
sentence = 'Dad and mom brought banana cucumber and milk.'
wd = sentence.split(' ')
for i, x in enumerate(wd):
if (x in fruits or x in vegetables) and i > 0:
print wd[i-1]
break
Upvotes: 1
Reputation: 179402
You can do this with regex:
def to_group(l):
''' make a regex group from a list of space-separated strings '''
return '(?:%s)' % ('|'.join(l.split()))
pattern = r'(\w+) %s %s' % (to_group(vegetables), to_group(fruits))
print re.findall(pattern, string)
Upvotes: 1