dimanche 18 novembre 2018

Python - How to find matching one or multiply words in a sentence

So I am trying to make a small script for myself where I have one or multiply word/s and by that it is supposed to find all matching words in a randomized sentence.

etc:

Sentence1 = "Hello, I am new here and I hope I will be able to help and get helped from Stackoverflow"

Sentence2 = "Is it beautiful weather"

Sentence3 = "I hope it wont be snowing here soon"

Sentence4 = "How is the weather"

Words = ['I+be', 'it+weather']


The output is supposed to say

Is it beautiful weather

I hope it wont be snowing here soon

and the reason why it doesn't print the first one and last one is that it does not contain I and Be and it and weather

So my question is basically how to make every + or any other special characters like keyword1 + keyword2 + n (Can be up from 1 to n words) and compare if those word are in the sentence

So what I tried to code was something like

Sentence = [
    "Hello, I am new here and I hope I will be able to help and get helped from Stackoverflow",
    "Is it beautiful weather", "I hope it wont be snowing here soon",
    "How is the weather"]

Words = ['I', 'it+weather']

for loop_word in Words:
    for loop_setence in Sentence:
        if loop_word in loop_setence:
            print(loop_setence)
            break

However for now it does only print out the first sentence since I changed the Words to I for now.

What I want to do is that words in that contains more than 1 word should be adding with a special character in between etc I+be so whenever there is a I and Be inside a sentence it should print that it found that sentence - Else do not print anything.

Illustration

So my question for you is how can I continue from my point forward with me wish :) ?

Aucun commentaire:

Enregistrer un commentaire