I'm looking to select relevant sentence tokens from a text. The selection has to be made if strings in the list 'keywords' are present and strings in the list 'avoid' are not present in the sentence tokens. However, the sentence tokens containing strings in the list 'avoid' have to be selected if strings in the list 'essentials' are present.
Here's the code I tried running:
#sentences is the list containing sentence tokens
facts = []
for sent in sentences:
for keyword in keywords:
if keyword in sent:
facts.append(sent)
for fact in facts[:]:
for i in avoid[:]:
for essential in essentials[:]:
if i in fact and essential not in fact:
facts.remove(fact)
This however does not include the sentence tokens of the second category i.e. the ones with essential strings along with the ones to be avoided.
What am I doing wrong and is there a better way to do this? Thanks in advance
I tried another way but it messes up the order of the sentence tokens. It goes like this:
for sent in sentences:
for keyword in keywords:
if keyword in sent:
facts.append(sent)
relevant_facts = [fact for fact in facts if not (i in fact for i in avoid)]
for sent in facts:
for essential in essentials:
if essential in sent:
relevant_facts.append(sent)