在 Python 的句子中打印 "no,not,never" 后面的否定词

Print negated words that follows "no,not,never" in a sentence in Python

如何打印 "no"、"not"、"never" 等之后的所有否定词。句子是

SENTENCE="It was never going to work.I'm not happy"

期望的输出

going,happy (Which follows never and not)

任何帮助!

您可能不需要 ntlk。我会拆分字符串(使用正则表达式根据非字母数字拆分(或者你对 work.I'm 部分有问题),并构建一个列表理解来查找属于 "negative" 的前一个单词单词。

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)

结果:

['going', 'happy']