How do I print all negated word that follows "no","not", "never" so on. The sentence is
SENTENCE="It was never going to work.I'm not happy"
Desired output
going,happy (Which follows never and not)
Any help !
How do I print all negated word that follows "no","not", "never" so on. The sentence is
SENTENCE="It was never going to work.I'm not happy"
Desired output
going,happy (Which follows never and not)
Any help !
You may not need ntlk
for that. I'd split the string (using regex to split according to non-alphanums (or you have an issue with work.I'm
part), and build a list comprehension which looks for the previous word belonging to the "negative" words.
import re
SENTENCE="It was never going to work.I'm not happy"
all_words = re.split("\W+",SENTENCE)
words = [w for i,w in enumerate(all_words) if i and (all_words[i-1] in ["not","never","no"])]
print(words)
result:
['going', 'happy']