0

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 !

Chathuri Fernando
  • 950
  • 3
  • 11
  • 22
  • 4
    Welcome to StackOverflow! Please keep in mind that this is not a "code this for me" website. Please show us what you already did and tell us where your problems lie. – Alfe Mar 09 '17 at 12:56

1 Answers1

2

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']
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219