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

Dani AI

Generated

A concise follow-up to and : the one-line substring check from is a good quick fix, but it can give false positives (matching inside longer words) and may break on punctuation or duplicate-word requirements. A more robust pattern is to tokenize the sentence and the query items, normalize case, then test membership (or counts) against that token set.

A token-based test (keeps apostrophes, lowercases, avoids substring matches):

import re

def has_all_items(sentence, phrase):
    sent_tokens = set(re.findall(r"\b[\w']+\b", sentence.lower()))
    phrase_tokens = re.findall(r"\b[\w']+\b", phrase.lower())
    return set(phrase_tokens).issubset(sent_tokens)

If exact counts matter (for example the phrase contains the same word twice), use a Counter-based check:

from collections import Counter
import re

def has_all_items_with_counts(sentence, phrase):
    s = re.findall(r"\b[\w']+\b", sentence.lower())
    p = re.findall(r"\b[\w']+\b", phrase.lower())
    s_count = Counter(s)
    p_count = Counter(p)
    return all(s_count[w] >= p_count[w] for w in p_count)

Notes and tips: pre-tokenize the sentence once if checking many phrases. Use casefold() instead of lower() for aggressive Unicode normalization. Adjust the token regex (for hyphens, non-ASCII letters, etc.) to match your data. If you need ordered or multi-word exact-phrase matches instead of independent tokens, use re.search(r'\b' + re.escape(phrase) + r'\b', sentence, flags=re.IGNORECASE). These approaches avoid substring pitfalls and handle punctuation reliably.

Recommended Answers

All 2 Replies

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.