如何打印“no”,“not”,“never”之后的所有否定词。这句话是
SENTENCE="It was never going to work.I'm not happy"
期望的输出
going,happy (Which follows never and not)
任何帮助!
答案 0 :(得分:2)
您可能不需要ntlk
。我拆分字符串(使用正则表达式根据非字母表进行拆分(或者你有work.I'm
部分的问题),并构建一个列表解析,查找属于&#34的前一个单词;否定"单词。
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']