0

Hi,

Given a sentence, I'm trying to check if all the individual constituents of a word are present in it and if they are then do some further processing. I can't seem to get the logic to work.

wordlist = ['England area','Germanic 6th']

mysentence = 'The area now called England has been settled by people of various cultures for about 35,000 years, but it takes its name from the Angles, one of the Germanic tribes who settled during the 5th and 6th centuries.'

for words in wordlist:
    #split each individual word into constituent items
    word_items = words.split(' ')
    #the word "England area" is now two words "England" and "area" and I want to   

    #check if both of these words are present (anywhere) in the sentence, if BOTH are 

    #present then print the sentence, else check the next word in wordlist.

    for w in witems:
       if mysentence.find(w) >= 0:
              .....

I can check if the first constituent of the word is present "England) and if the second "area" is present but only individually, how to check if both are present?


Thanks,
Adi

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by kadvar
0

How about:

wordlist = ['England area','Germanic 6th', 'Finnish people']

mysentence = 'The area now called England has been settled by people of various cultures for about 35,000 years, but it takes its name from the Angles, one of the Germanic tribes who settled during the 5th and 6th centuries.'

for word_items in (word.split() for word in wordlist):
    if all(word in mysentence for word in word_items):
        print 'Found %r. Doing my stuff' % (' & '.join(word_items))
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.