Check for words in a sentenceHow do I check if a list is empty?How do I check whether a file exists without exceptions?What's the canonical way to check for type in Python?How do I check if a string is a number (float)?Check if a given key already exists in a dictionaryChecking whether a variable is an integer or notHow to check if the string is empty?Regular expression finditer: search twice on the same symbolspython Program to check whether two words are anagramsregex to match the minimum length of each word in a sentence in Python

Why were movies shot on film shot at 24 frames per second?

Fancy String Replace

C++20 constexpr std::copy optimizations for run-time

What is the hex versus octal timeline?

Defense against attacks using dictionaries

Start from ones

Sun setting in East!

Mathematical uses of string theory

Is it safe to remove the bottom chords of a series of garage roof trusses?

What is the history of the university asylum law?

Are modern clipless shoes and pedals that much better than toe clips and straps?

Would this system work to purify water?

Is using a hyperlink to close a modal a poor design decision?

How to use "Du hast/ Du hattest'?

In an emergency, how do I find and share my position?

Was there ever a treaty between 2 entities with significantly different translations to the detriment of one party?

LeetCode: Pascal's Triangle C#

Why in most German places is the church the tallest building?

Rule based coloured background for labeling in QGIS

Is it possible to get crispy, crunchy carrots from canned carrots?

Compelling story with the world as a villain

Which household object drew this pattern?

Why does The Ancient One think differently about Doctor Strange in Endgame than the film Doctor Strange?

Can you feel passing through the sound barrier in an F-16?



Check for words in a sentence


How do I check if a list is empty?How do I check whether a file exists without exceptions?What's the canonical way to check for type in Python?How do I check if a string is a number (float)?Check if a given key already exists in a dictionaryChecking whether a variable is an integer or notHow to check if the string is empty?Regular expression finditer: search twice on the same symbolspython Program to check whether two words are anagramsregex to match the minimum length of each word in a sentence in Python






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I write a program in Python. The user enters a text message. It is necessary to check whether there is a sequence of words in this message. Sample. Message: "Hello world, my friend.". Check the sequence of these two words: "Hello", "world". The Result Is "True". But when checking the sequence of these words in the message: "Hello, beautiful world "the result is"false". When you need to check the presence of only two words it is possible as I did it in the code, but when combinations of 5 or more words is difficult. Is there any small solution to this problem?



s=message.text
s=s.lower()
lst = s.split()
elif "hello" in lst and "world" in lst :
if "hello" in lst:
c=lst.index("hello")
if lst[c+1]=="world" or lst[c-1]=="world":
E=True
else:
E=False









share|improve this question





















  • 1





    If you split "hello, beautiful world" into a list the list will contain "hello,", not "hello".

    – Matthias
    Mar 27 at 16:09












  • Do you also want "world hello" to return true? If there are 5 words, can they be in any order?

    – Alex Hall
    Mar 27 at 16:12












  • @Matthias Yes, but if the message will be: " World how are you? Hello everyone." My program is quite working for such a case, but if you need to find consistently standing, but in any order 5 words?...

    – The person with the question
    Mar 27 at 16:14











  • Strip out the punctuation before you split, and you probably want to turn everything into a single case (e.g. lower()).

    – AChampion
    Mar 27 at 16:17











  • @Alex Yes they can be in any order, but consistently i.e. between them there is no other words. "hello", "my", "dear","friend", "Dima" or "my", "dear","friend","Dima", "hello" or other variations.

    – The person with the question
    Mar 27 at 16:20

















1















I write a program in Python. The user enters a text message. It is necessary to check whether there is a sequence of words in this message. Sample. Message: "Hello world, my friend.". Check the sequence of these two words: "Hello", "world". The Result Is "True". But when checking the sequence of these words in the message: "Hello, beautiful world "the result is"false". When you need to check the presence of only two words it is possible as I did it in the code, but when combinations of 5 or more words is difficult. Is there any small solution to this problem?



s=message.text
s=s.lower()
lst = s.split()
elif "hello" in lst and "world" in lst :
if "hello" in lst:
c=lst.index("hello")
if lst[c+1]=="world" or lst[c-1]=="world":
E=True
else:
E=False









share|improve this question





















  • 1





    If you split "hello, beautiful world" into a list the list will contain "hello,", not "hello".

    – Matthias
    Mar 27 at 16:09












  • Do you also want "world hello" to return true? If there are 5 words, can they be in any order?

    – Alex Hall
    Mar 27 at 16:12












  • @Matthias Yes, but if the message will be: " World how are you? Hello everyone." My program is quite working for such a case, but if you need to find consistently standing, but in any order 5 words?...

    – The person with the question
    Mar 27 at 16:14











  • Strip out the punctuation before you split, and you probably want to turn everything into a single case (e.g. lower()).

    – AChampion
    Mar 27 at 16:17











  • @Alex Yes they can be in any order, but consistently i.e. between them there is no other words. "hello", "my", "dear","friend", "Dima" or "my", "dear","friend","Dima", "hello" or other variations.

    – The person with the question
    Mar 27 at 16:20













1












1








1


1






I write a program in Python. The user enters a text message. It is necessary to check whether there is a sequence of words in this message. Sample. Message: "Hello world, my friend.". Check the sequence of these two words: "Hello", "world". The Result Is "True". But when checking the sequence of these words in the message: "Hello, beautiful world "the result is"false". When you need to check the presence of only two words it is possible as I did it in the code, but when combinations of 5 or more words is difficult. Is there any small solution to this problem?



s=message.text
s=s.lower()
lst = s.split()
elif "hello" in lst and "world" in lst :
if "hello" in lst:
c=lst.index("hello")
if lst[c+1]=="world" or lst[c-1]=="world":
E=True
else:
E=False









share|improve this question
















I write a program in Python. The user enters a text message. It is necessary to check whether there is a sequence of words in this message. Sample. Message: "Hello world, my friend.". Check the sequence of these two words: "Hello", "world". The Result Is "True". But when checking the sequence of these words in the message: "Hello, beautiful world "the result is"false". When you need to check the presence of only two words it is possible as I did it in the code, but when combinations of 5 or more words is difficult. Is there any small solution to this problem?



s=message.text
s=s.lower()
lst = s.split()
elif "hello" in lst and "world" in lst :
if "hello" in lst:
c=lst.index("hello")
if lst[c+1]=="world" or lst[c-1]=="world":
E=True
else:
E=False






python utf-8






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 16:18









Prune

51.3k14 gold badges39 silver badges61 bronze badges




51.3k14 gold badges39 silver badges61 bronze badges










asked Mar 27 at 16:04









The person with the questionThe person with the question

306 bronze badges




306 bronze badges










  • 1





    If you split "hello, beautiful world" into a list the list will contain "hello,", not "hello".

    – Matthias
    Mar 27 at 16:09












  • Do you also want "world hello" to return true? If there are 5 words, can they be in any order?

    – Alex Hall
    Mar 27 at 16:12












  • @Matthias Yes, but if the message will be: " World how are you? Hello everyone." My program is quite working for such a case, but if you need to find consistently standing, but in any order 5 words?...

    – The person with the question
    Mar 27 at 16:14











  • Strip out the punctuation before you split, and you probably want to turn everything into a single case (e.g. lower()).

    – AChampion
    Mar 27 at 16:17











  • @Alex Yes they can be in any order, but consistently i.e. between them there is no other words. "hello", "my", "dear","friend", "Dima" or "my", "dear","friend","Dima", "hello" or other variations.

    – The person with the question
    Mar 27 at 16:20












  • 1





    If you split "hello, beautiful world" into a list the list will contain "hello,", not "hello".

    – Matthias
    Mar 27 at 16:09












  • Do you also want "world hello" to return true? If there are 5 words, can they be in any order?

    – Alex Hall
    Mar 27 at 16:12












  • @Matthias Yes, but if the message will be: " World how are you? Hello everyone." My program is quite working for such a case, but if you need to find consistently standing, but in any order 5 words?...

    – The person with the question
    Mar 27 at 16:14











  • Strip out the punctuation before you split, and you probably want to turn everything into a single case (e.g. lower()).

    – AChampion
    Mar 27 at 16:17











  • @Alex Yes they can be in any order, but consistently i.e. between them there is no other words. "hello", "my", "dear","friend", "Dima" or "my", "dear","friend","Dima", "hello" or other variations.

    – The person with the question
    Mar 27 at 16:20







1




1





If you split "hello, beautiful world" into a list the list will contain "hello,", not "hello".

– Matthias
Mar 27 at 16:09






If you split "hello, beautiful world" into a list the list will contain "hello,", not "hello".

– Matthias
Mar 27 at 16:09














Do you also want "world hello" to return true? If there are 5 words, can they be in any order?

– Alex Hall
Mar 27 at 16:12






Do you also want "world hello" to return true? If there are 5 words, can they be in any order?

– Alex Hall
Mar 27 at 16:12














@Matthias Yes, but if the message will be: " World how are you? Hello everyone." My program is quite working for such a case, but if you need to find consistently standing, but in any order 5 words?...

– The person with the question
Mar 27 at 16:14





@Matthias Yes, but if the message will be: " World how are you? Hello everyone." My program is quite working for such a case, but if you need to find consistently standing, but in any order 5 words?...

– The person with the question
Mar 27 at 16:14













Strip out the punctuation before you split, and you probably want to turn everything into a single case (e.g. lower()).

– AChampion
Mar 27 at 16:17





Strip out the punctuation before you split, and you probably want to turn everything into a single case (e.g. lower()).

– AChampion
Mar 27 at 16:17













@Alex Yes they can be in any order, but consistently i.e. between them there is no other words. "hello", "my", "dear","friend", "Dima" or "my", "dear","friend","Dima", "hello" or other variations.

– The person with the question
Mar 27 at 16:20





@Alex Yes they can be in any order, but consistently i.e. between them there is no other words. "hello", "my", "dear","friend", "Dima" or "my", "dear","friend","Dima", "hello" or other variations.

– The person with the question
Mar 27 at 16:20












3 Answers
3






active

oldest

votes


















0















I will clarify your requirement first:



  • ignore case


  • consecutive sequence


  • match in any order, like permutation or anagram


  • support duplicated words


if the number is not too large, you can try this easy-understanding but not the fastest way.



  1. split all words in text message

  2. join them with ' '

  3. list all the permutation of words and join them with ' ' too, For
    example, if you want to check sequence of ['Hello', 'beautiful', 'world']. The permutation will be 'Hello beautiful world',
    'Hello world beautiful', 'beautiful Hello world'... and so on.

  4. and you can just find whether there is one permutation such as
    'hello beautiful world' is in it.

The sample code is here:



import itertools
import re

# permutations brute-force, O(nk!)
def checkWords(text, word_list):
# split all words without space and punctuation
text_words= re.findall(r"[w']+", text.lower())

# list all the permutations of word_list, and match
for words in itertools.permutations(word_list):
if ' '.join(words).lower() in ' '.join(text_words):
return True
return False

# or use any, just one line
# return any(' '.join(words).lower() in ' '.join(text_words) for words in list(itertools.permutations(word_list)))
def test():
# True
print(checkWords('Hello world, my friend.', ['Hello', 'world', 'my']))
# False
print(checkWords('Hello, beautiful world', ['Hello', 'world']))
# True
print(checkWords('Hello, beautiful world Hello World', ['Hello', 'world', 'beautiful']))
# True
print(checkWords('Hello, beautiful world Hello World', ['Hello', 'world', 'world']))


But it costs a lot when words number is large, k words will generate k! permutation, the time complexity is O(nk!).



I think a more efficient solution is sliding window. The time complexity will decrease to O(n):



import itertools
import re
import collections

# sliding window, O(n)
def checkWords(text, word_list):
# split all words without space and punctuation
text_words = re.findall(r"[w']+", text.lower())
counter = collections.Counter(map(str.lower, word_list))
start, end, count, all_indexes = 0, 0, len(word_list), []

while end < len(text_words):
counter[text_words[end]] -= 1
if counter[text_words[end]] >= 0:
count -= 1
end += 1

# if you want all the index of match, you can change here
if count == 0:
# all_indexes.append(start)
return True

if end - start == len(word_list):
counter[text_words[start]] += 1
if counter[text_words[start]] > 0:
count += 1
start += 1

# return all_indexes
return False





share|improve this answer



























  • This is a good start. It should be pointed out exactly which characters a searched word is allowed to contain with this code. It also needs to be generalised to find the search words in any order (but still together).

    – Alex Hall
    Mar 27 at 16:28












  • Okay, but I also need the "world hello" answer to be True. And if words 3 the any a combination of of these words. Should I use "permutations" for this ?

    – The person with the question
    Mar 27 at 16:30











  • sorry, I misunderstand at first time, you can see whether the new version is what you need @The person with the question

    – recnac
    Mar 27 at 16:39











  • Thanks. That's what I need.

    – The person with the question
    Mar 27 at 16:44











  • You are welcome, and it costs a lot when words count increase, it will generate K! permutation. I will update a more efficient way maybe later. @The person with the question

    – recnac
    Mar 27 at 16:49


















1















The straightforward way is to use a loop. Split your message into individual words, and then check for each of those in the sentence in general.



word_list = message.split() # this gives you a list of words to find
word_found = True
for word in word_list:
if word not in message2:
word_found = False

print(word_found)


The flag word_found is True iff all words were found in the sentence. There are many ways to make this shorter and faster, especially using the all operator, and providing the word list as an in-line expression.



word_found = all(word in message2 for word in message.split())


Now, if you need to restrict your "found" property to matching exact words, you'll need more preprocessing. The above code is too forgiving of substrings, such as finding "Are you OK ?" in the sentence "your joke is only barely funny". For the more restrictive case, you should break message2 into words, strip those words of punctuation, drop them to lower-case (to make matching easier), and then look for each word (from message) in the list of words from message2.



Can you take it from there?






share|improve this answer
































    0















    I don't know if that what you really need but this worked you can tested



    message= 'hello world'
    message2= ' hello beautiful world'
    if 'hello' in message and 'world' in message :
    print('yes')
    else :
    print('no')
    if 'hello' in message2 and 'world' in message2 :
    print('yes')


    out put :
    yes
    yes






    share|improve this answer

























    • Words must be consistently mooted. On the option " hello beautiful world "the result should be"No".

      – The person with the question
      Mar 27 at 16:22













    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55381690%2fcheck-for-words-in-a-sentence%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0















    I will clarify your requirement first:



    • ignore case


    • consecutive sequence


    • match in any order, like permutation or anagram


    • support duplicated words


    if the number is not too large, you can try this easy-understanding but not the fastest way.



    1. split all words in text message

    2. join them with ' '

    3. list all the permutation of words and join them with ' ' too, For
      example, if you want to check sequence of ['Hello', 'beautiful', 'world']. The permutation will be 'Hello beautiful world',
      'Hello world beautiful', 'beautiful Hello world'... and so on.

    4. and you can just find whether there is one permutation such as
      'hello beautiful world' is in it.

    The sample code is here:



    import itertools
    import re

    # permutations brute-force, O(nk!)
    def checkWords(text, word_list):
    # split all words without space and punctuation
    text_words= re.findall(r"[w']+", text.lower())

    # list all the permutations of word_list, and match
    for words in itertools.permutations(word_list):
    if ' '.join(words).lower() in ' '.join(text_words):
    return True
    return False

    # or use any, just one line
    # return any(' '.join(words).lower() in ' '.join(text_words) for words in list(itertools.permutations(word_list)))
    def test():
    # True
    print(checkWords('Hello world, my friend.', ['Hello', 'world', 'my']))
    # False
    print(checkWords('Hello, beautiful world', ['Hello', 'world']))
    # True
    print(checkWords('Hello, beautiful world Hello World', ['Hello', 'world', 'beautiful']))
    # True
    print(checkWords('Hello, beautiful world Hello World', ['Hello', 'world', 'world']))


    But it costs a lot when words number is large, k words will generate k! permutation, the time complexity is O(nk!).



    I think a more efficient solution is sliding window. The time complexity will decrease to O(n):



    import itertools
    import re
    import collections

    # sliding window, O(n)
    def checkWords(text, word_list):
    # split all words without space and punctuation
    text_words = re.findall(r"[w']+", text.lower())
    counter = collections.Counter(map(str.lower, word_list))
    start, end, count, all_indexes = 0, 0, len(word_list), []

    while end < len(text_words):
    counter[text_words[end]] -= 1
    if counter[text_words[end]] >= 0:
    count -= 1
    end += 1

    # if you want all the index of match, you can change here
    if count == 0:
    # all_indexes.append(start)
    return True

    if end - start == len(word_list):
    counter[text_words[start]] += 1
    if counter[text_words[start]] > 0:
    count += 1
    start += 1

    # return all_indexes
    return False





    share|improve this answer



























    • This is a good start. It should be pointed out exactly which characters a searched word is allowed to contain with this code. It also needs to be generalised to find the search words in any order (but still together).

      – Alex Hall
      Mar 27 at 16:28












    • Okay, but I also need the "world hello" answer to be True. And if words 3 the any a combination of of these words. Should I use "permutations" for this ?

      – The person with the question
      Mar 27 at 16:30











    • sorry, I misunderstand at first time, you can see whether the new version is what you need @The person with the question

      – recnac
      Mar 27 at 16:39











    • Thanks. That's what I need.

      – The person with the question
      Mar 27 at 16:44











    • You are welcome, and it costs a lot when words count increase, it will generate K! permutation. I will update a more efficient way maybe later. @The person with the question

      – recnac
      Mar 27 at 16:49















    0















    I will clarify your requirement first:



    • ignore case


    • consecutive sequence


    • match in any order, like permutation or anagram


    • support duplicated words


    if the number is not too large, you can try this easy-understanding but not the fastest way.



    1. split all words in text message

    2. join them with ' '

    3. list all the permutation of words and join them with ' ' too, For
      example, if you want to check sequence of ['Hello', 'beautiful', 'world']. The permutation will be 'Hello beautiful world',
      'Hello world beautiful', 'beautiful Hello world'... and so on.

    4. and you can just find whether there is one permutation such as
      'hello beautiful world' is in it.

    The sample code is here:



    import itertools
    import re

    # permutations brute-force, O(nk!)
    def checkWords(text, word_list):
    # split all words without space and punctuation
    text_words= re.findall(r"[w']+", text.lower())

    # list all the permutations of word_list, and match
    for words in itertools.permutations(word_list):
    if ' '.join(words).lower() in ' '.join(text_words):
    return True
    return False

    # or use any, just one line
    # return any(' '.join(words).lower() in ' '.join(text_words) for words in list(itertools.permutations(word_list)))
    def test():
    # True
    print(checkWords('Hello world, my friend.', ['Hello', 'world', 'my']))
    # False
    print(checkWords('Hello, beautiful world', ['Hello', 'world']))
    # True
    print(checkWords('Hello, beautiful world Hello World', ['Hello', 'world', 'beautiful']))
    # True
    print(checkWords('Hello, beautiful world Hello World', ['Hello', 'world', 'world']))


    But it costs a lot when words number is large, k words will generate k! permutation, the time complexity is O(nk!).



    I think a more efficient solution is sliding window. The time complexity will decrease to O(n):



    import itertools
    import re
    import collections

    # sliding window, O(n)
    def checkWords(text, word_list):
    # split all words without space and punctuation
    text_words = re.findall(r"[w']+", text.lower())
    counter = collections.Counter(map(str.lower, word_list))
    start, end, count, all_indexes = 0, 0, len(word_list), []

    while end < len(text_words):
    counter[text_words[end]] -= 1
    if counter[text_words[end]] >= 0:
    count -= 1
    end += 1

    # if you want all the index of match, you can change here
    if count == 0:
    # all_indexes.append(start)
    return True

    if end - start == len(word_list):
    counter[text_words[start]] += 1
    if counter[text_words[start]] > 0:
    count += 1
    start += 1

    # return all_indexes
    return False





    share|improve this answer



























    • This is a good start. It should be pointed out exactly which characters a searched word is allowed to contain with this code. It also needs to be generalised to find the search words in any order (but still together).

      – Alex Hall
      Mar 27 at 16:28












    • Okay, but I also need the "world hello" answer to be True. And if words 3 the any a combination of of these words. Should I use "permutations" for this ?

      – The person with the question
      Mar 27 at 16:30











    • sorry, I misunderstand at first time, you can see whether the new version is what you need @The person with the question

      – recnac
      Mar 27 at 16:39











    • Thanks. That's what I need.

      – The person with the question
      Mar 27 at 16:44











    • You are welcome, and it costs a lot when words count increase, it will generate K! permutation. I will update a more efficient way maybe later. @The person with the question

      – recnac
      Mar 27 at 16:49













    0














    0










    0









    I will clarify your requirement first:



    • ignore case


    • consecutive sequence


    • match in any order, like permutation or anagram


    • support duplicated words


    if the number is not too large, you can try this easy-understanding but not the fastest way.



    1. split all words in text message

    2. join them with ' '

    3. list all the permutation of words and join them with ' ' too, For
      example, if you want to check sequence of ['Hello', 'beautiful', 'world']. The permutation will be 'Hello beautiful world',
      'Hello world beautiful', 'beautiful Hello world'... and so on.

    4. and you can just find whether there is one permutation such as
      'hello beautiful world' is in it.

    The sample code is here:



    import itertools
    import re

    # permutations brute-force, O(nk!)
    def checkWords(text, word_list):
    # split all words without space and punctuation
    text_words= re.findall(r"[w']+", text.lower())

    # list all the permutations of word_list, and match
    for words in itertools.permutations(word_list):
    if ' '.join(words).lower() in ' '.join(text_words):
    return True
    return False

    # or use any, just one line
    # return any(' '.join(words).lower() in ' '.join(text_words) for words in list(itertools.permutations(word_list)))
    def test():
    # True
    print(checkWords('Hello world, my friend.', ['Hello', 'world', 'my']))
    # False
    print(checkWords('Hello, beautiful world', ['Hello', 'world']))
    # True
    print(checkWords('Hello, beautiful world Hello World', ['Hello', 'world', 'beautiful']))
    # True
    print(checkWords('Hello, beautiful world Hello World', ['Hello', 'world', 'world']))


    But it costs a lot when words number is large, k words will generate k! permutation, the time complexity is O(nk!).



    I think a more efficient solution is sliding window. The time complexity will decrease to O(n):



    import itertools
    import re
    import collections

    # sliding window, O(n)
    def checkWords(text, word_list):
    # split all words without space and punctuation
    text_words = re.findall(r"[w']+", text.lower())
    counter = collections.Counter(map(str.lower, word_list))
    start, end, count, all_indexes = 0, 0, len(word_list), []

    while end < len(text_words):
    counter[text_words[end]] -= 1
    if counter[text_words[end]] >= 0:
    count -= 1
    end += 1

    # if you want all the index of match, you can change here
    if count == 0:
    # all_indexes.append(start)
    return True

    if end - start == len(word_list):
    counter[text_words[start]] += 1
    if counter[text_words[start]] > 0:
    count += 1
    start += 1

    # return all_indexes
    return False





    share|improve this answer















    I will clarify your requirement first:



    • ignore case


    • consecutive sequence


    • match in any order, like permutation or anagram


    • support duplicated words


    if the number is not too large, you can try this easy-understanding but not the fastest way.



    1. split all words in text message

    2. join them with ' '

    3. list all the permutation of words and join them with ' ' too, For
      example, if you want to check sequence of ['Hello', 'beautiful', 'world']. The permutation will be 'Hello beautiful world',
      'Hello world beautiful', 'beautiful Hello world'... and so on.

    4. and you can just find whether there is one permutation such as
      'hello beautiful world' is in it.

    The sample code is here:



    import itertools
    import re

    # permutations brute-force, O(nk!)
    def checkWords(text, word_list):
    # split all words without space and punctuation
    text_words= re.findall(r"[w']+", text.lower())

    # list all the permutations of word_list, and match
    for words in itertools.permutations(word_list):
    if ' '.join(words).lower() in ' '.join(text_words):
    return True
    return False

    # or use any, just one line
    # return any(' '.join(words).lower() in ' '.join(text_words) for words in list(itertools.permutations(word_list)))
    def test():
    # True
    print(checkWords('Hello world, my friend.', ['Hello', 'world', 'my']))
    # False
    print(checkWords('Hello, beautiful world', ['Hello', 'world']))
    # True
    print(checkWords('Hello, beautiful world Hello World', ['Hello', 'world', 'beautiful']))
    # True
    print(checkWords('Hello, beautiful world Hello World', ['Hello', 'world', 'world']))


    But it costs a lot when words number is large, k words will generate k! permutation, the time complexity is O(nk!).



    I think a more efficient solution is sliding window. The time complexity will decrease to O(n):



    import itertools
    import re
    import collections

    # sliding window, O(n)
    def checkWords(text, word_list):
    # split all words without space and punctuation
    text_words = re.findall(r"[w']+", text.lower())
    counter = collections.Counter(map(str.lower, word_list))
    start, end, count, all_indexes = 0, 0, len(word_list), []

    while end < len(text_words):
    counter[text_words[end]] -= 1
    if counter[text_words[end]] >= 0:
    count -= 1
    end += 1

    # if you want all the index of match, you can change here
    if count == 0:
    # all_indexes.append(start)
    return True

    if end - start == len(word_list):
    counter[text_words[start]] += 1
    if counter[text_words[start]] > 0:
    count += 1
    start += 1

    # return all_indexes
    return False






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 28 at 3:17

























    answered Mar 27 at 16:22









    recnacrecnac

    3,1976 gold badges9 silver badges38 bronze badges




    3,1976 gold badges9 silver badges38 bronze badges















    • This is a good start. It should be pointed out exactly which characters a searched word is allowed to contain with this code. It also needs to be generalised to find the search words in any order (but still together).

      – Alex Hall
      Mar 27 at 16:28












    • Okay, but I also need the "world hello" answer to be True. And if words 3 the any a combination of of these words. Should I use "permutations" for this ?

      – The person with the question
      Mar 27 at 16:30











    • sorry, I misunderstand at first time, you can see whether the new version is what you need @The person with the question

      – recnac
      Mar 27 at 16:39











    • Thanks. That's what I need.

      – The person with the question
      Mar 27 at 16:44











    • You are welcome, and it costs a lot when words count increase, it will generate K! permutation. I will update a more efficient way maybe later. @The person with the question

      – recnac
      Mar 27 at 16:49

















    • This is a good start. It should be pointed out exactly which characters a searched word is allowed to contain with this code. It also needs to be generalised to find the search words in any order (but still together).

      – Alex Hall
      Mar 27 at 16:28












    • Okay, but I also need the "world hello" answer to be True. And if words 3 the any a combination of of these words. Should I use "permutations" for this ?

      – The person with the question
      Mar 27 at 16:30











    • sorry, I misunderstand at first time, you can see whether the new version is what you need @The person with the question

      – recnac
      Mar 27 at 16:39











    • Thanks. That's what I need.

      – The person with the question
      Mar 27 at 16:44











    • You are welcome, and it costs a lot when words count increase, it will generate K! permutation. I will update a more efficient way maybe later. @The person with the question

      – recnac
      Mar 27 at 16:49
















    This is a good start. It should be pointed out exactly which characters a searched word is allowed to contain with this code. It also needs to be generalised to find the search words in any order (but still together).

    – Alex Hall
    Mar 27 at 16:28






    This is a good start. It should be pointed out exactly which characters a searched word is allowed to contain with this code. It also needs to be generalised to find the search words in any order (but still together).

    – Alex Hall
    Mar 27 at 16:28














    Okay, but I also need the "world hello" answer to be True. And if words 3 the any a combination of of these words. Should I use "permutations" for this ?

    – The person with the question
    Mar 27 at 16:30





    Okay, but I also need the "world hello" answer to be True. And if words 3 the any a combination of of these words. Should I use "permutations" for this ?

    – The person with the question
    Mar 27 at 16:30













    sorry, I misunderstand at first time, you can see whether the new version is what you need @The person with the question

    – recnac
    Mar 27 at 16:39





    sorry, I misunderstand at first time, you can see whether the new version is what you need @The person with the question

    – recnac
    Mar 27 at 16:39













    Thanks. That's what I need.

    – The person with the question
    Mar 27 at 16:44





    Thanks. That's what I need.

    – The person with the question
    Mar 27 at 16:44













    You are welcome, and it costs a lot when words count increase, it will generate K! permutation. I will update a more efficient way maybe later. @The person with the question

    – recnac
    Mar 27 at 16:49





    You are welcome, and it costs a lot when words count increase, it will generate K! permutation. I will update a more efficient way maybe later. @The person with the question

    – recnac
    Mar 27 at 16:49













    1















    The straightforward way is to use a loop. Split your message into individual words, and then check for each of those in the sentence in general.



    word_list = message.split() # this gives you a list of words to find
    word_found = True
    for word in word_list:
    if word not in message2:
    word_found = False

    print(word_found)


    The flag word_found is True iff all words were found in the sentence. There are many ways to make this shorter and faster, especially using the all operator, and providing the word list as an in-line expression.



    word_found = all(word in message2 for word in message.split())


    Now, if you need to restrict your "found" property to matching exact words, you'll need more preprocessing. The above code is too forgiving of substrings, such as finding "Are you OK ?" in the sentence "your joke is only barely funny". For the more restrictive case, you should break message2 into words, strip those words of punctuation, drop them to lower-case (to make matching easier), and then look for each word (from message) in the list of words from message2.



    Can you take it from there?






    share|improve this answer





























      1















      The straightforward way is to use a loop. Split your message into individual words, and then check for each of those in the sentence in general.



      word_list = message.split() # this gives you a list of words to find
      word_found = True
      for word in word_list:
      if word not in message2:
      word_found = False

      print(word_found)


      The flag word_found is True iff all words were found in the sentence. There are many ways to make this shorter and faster, especially using the all operator, and providing the word list as an in-line expression.



      word_found = all(word in message2 for word in message.split())


      Now, if you need to restrict your "found" property to matching exact words, you'll need more preprocessing. The above code is too forgiving of substrings, such as finding "Are you OK ?" in the sentence "your joke is only barely funny". For the more restrictive case, you should break message2 into words, strip those words of punctuation, drop them to lower-case (to make matching easier), and then look for each word (from message) in the list of words from message2.



      Can you take it from there?






      share|improve this answer



























        1














        1










        1









        The straightforward way is to use a loop. Split your message into individual words, and then check for each of those in the sentence in general.



        word_list = message.split() # this gives you a list of words to find
        word_found = True
        for word in word_list:
        if word not in message2:
        word_found = False

        print(word_found)


        The flag word_found is True iff all words were found in the sentence. There are many ways to make this shorter and faster, especially using the all operator, and providing the word list as an in-line expression.



        word_found = all(word in message2 for word in message.split())


        Now, if you need to restrict your "found" property to matching exact words, you'll need more preprocessing. The above code is too forgiving of substrings, such as finding "Are you OK ?" in the sentence "your joke is only barely funny". For the more restrictive case, you should break message2 into words, strip those words of punctuation, drop them to lower-case (to make matching easier), and then look for each word (from message) in the list of words from message2.



        Can you take it from there?






        share|improve this answer













        The straightforward way is to use a loop. Split your message into individual words, and then check for each of those in the sentence in general.



        word_list = message.split() # this gives you a list of words to find
        word_found = True
        for word in word_list:
        if word not in message2:
        word_found = False

        print(word_found)


        The flag word_found is True iff all words were found in the sentence. There are many ways to make this shorter and faster, especially using the all operator, and providing the word list as an in-line expression.



        word_found = all(word in message2 for word in message.split())


        Now, if you need to restrict your "found" property to matching exact words, you'll need more preprocessing. The above code is too forgiving of substrings, such as finding "Are you OK ?" in the sentence "your joke is only barely funny". For the more restrictive case, you should break message2 into words, strip those words of punctuation, drop them to lower-case (to make matching easier), and then look for each word (from message) in the list of words from message2.



        Can you take it from there?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 16:28









        PrunePrune

        51.3k14 gold badges39 silver badges61 bronze badges




        51.3k14 gold badges39 silver badges61 bronze badges
























            0















            I don't know if that what you really need but this worked you can tested



            message= 'hello world'
            message2= ' hello beautiful world'
            if 'hello' in message and 'world' in message :
            print('yes')
            else :
            print('no')
            if 'hello' in message2 and 'world' in message2 :
            print('yes')


            out put :
            yes
            yes






            share|improve this answer

























            • Words must be consistently mooted. On the option " hello beautiful world "the result should be"No".

              – The person with the question
              Mar 27 at 16:22















            0















            I don't know if that what you really need but this worked you can tested



            message= 'hello world'
            message2= ' hello beautiful world'
            if 'hello' in message and 'world' in message :
            print('yes')
            else :
            print('no')
            if 'hello' in message2 and 'world' in message2 :
            print('yes')


            out put :
            yes
            yes






            share|improve this answer

























            • Words must be consistently mooted. On the option " hello beautiful world "the result should be"No".

              – The person with the question
              Mar 27 at 16:22













            0














            0










            0









            I don't know if that what you really need but this worked you can tested



            message= 'hello world'
            message2= ' hello beautiful world'
            if 'hello' in message and 'world' in message :
            print('yes')
            else :
            print('no')
            if 'hello' in message2 and 'world' in message2 :
            print('yes')


            out put :
            yes
            yes






            share|improve this answer













            I don't know if that what you really need but this worked you can tested



            message= 'hello world'
            message2= ' hello beautiful world'
            if 'hello' in message and 'world' in message :
            print('yes')
            else :
            print('no')
            if 'hello' in message2 and 'world' in message2 :
            print('yes')


            out put :
            yes
            yes







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 27 at 16:14









            salahsalah

            639 bronze badges




            639 bronze badges















            • Words must be consistently mooted. On the option " hello beautiful world "the result should be"No".

              – The person with the question
              Mar 27 at 16:22

















            • Words must be consistently mooted. On the option " hello beautiful world "the result should be"No".

              – The person with the question
              Mar 27 at 16:22
















            Words must be consistently mooted. On the option " hello beautiful world "the result should be"No".

            – The person with the question
            Mar 27 at 16:22





            Words must be consistently mooted. On the option " hello beautiful world "the result should be"No".

            – The person with the question
            Mar 27 at 16:22

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55381690%2fcheck-for-words-in-a-sentence%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript