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

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))

Thank you very much, that works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.