user9875321__
user9875321__

Reputation: 357

check if string follows specific list of strings in python regex

I have a list of strings that I call 'text_sentences' of the type

["bla bla bla", "yada yada yada","foo boo, foo"...]

Then I have a list of specific strings (words) I have to use to identify the elements (sentences) in my text_sentences, which I call 'words'

words=["word1", "word2",..]

My aim is to identify the sentences in text_sentences based on words, i.e. if a sentence contain at least one of the words in words, that sentence (element of text_sentences) will be put into a new list, say it 'matched'. If not, put it in a list called 'unmatched'. I can reproduce this with something like

    matched=[]
    unmatched_sent=[]
    for j in range(len(text_sentences)):
        if any(s in text_sentences[j] for s in words):
            matched.append(text_sentences[j])
        else:
            unmatched.append(text_sentences[j])

However: this is only one step of the process I need to perform. In fact, I also have a list of words like negations of the type

negations=["no","not","none"]

Its use is the following: if a sentence in text_sentences contain at least one word in words, then that sentence must be appended to the matched list; if, however, the word from wordsthat is contained in that sentence follow any word from the negationslist, then that sentence must be appended to the list unmatched. If the sentence does not contain any word from words, then it must be appended to unmatched. How can I perform this all at once?

Upvotes: 0

Views: 325

Answers (1)

Ali Hassan
Ali Hassan

Reputation: 966

t = ["bla bla bla", "yada yada yada","foo boo, foo", "yoo no you are not in list"]
words = ["test", "bla", "yoo"]
negation = ["no","not","none"]
unmatched = []
matched = []
for i in words:
    for j in t:
        if i in j:
            matched.append(j)

for l in t:
    if l not in matched and l not in unmatched:
        unmatched.append(l)

for m in negation:
    for k in matched:
        if m in k:
            matched.remove(k)

print(unmatched)
print(matched)

Upvotes: 1

Related Questions