提问人:Shadi Farzankia 提问时间:8/18/2023 更新时间:8/18/2023 访问量:66
从 NLTK 非索引字中排除否定词
exclude negative words from nltk stopwords
问:
我想从我的句子中删除 nltk 停用词,但具有负面含义的停用词除外,例如:不、不、不能等。换句话说,我想从非索引字列表中排除否定词。我该怎么做?
答:
0赞
Anilosan15
8/18/2023
#1
没有一帆风顺的路,
negative_words = {
'no',
'not',
'none',
'neither',
'never',
'nobody',
'nothing',
'nowhere',
'doesn't',
'isn't',
'wasn't',
'shouldn't',
'won't',
'can't',
'couldn't',
'don't',
'haven't',
'hasn't',
'hadn't',
'aren't',
'weren't',
'wouldn't',
'daren't',
'needn't',
'didn't',
'without',
'against',
'negative',
'deny',
'reject',
'refuse',
'decline',
'unhappy',
'sad',
'miserable',
'hopeless',
'worthless',
'useless',
'futile',
'disagree',
'oppose',
'contrary',
'contradict',
'disapprove',
'dissatisfied',
'objection',
'unsatisfactory',
'unpleasant',
'regret',
'resent',
'lament',
'mourn',
'grieve',
'bemoan',
'despise',
'loathe',
'detract',
'abhor',
'dread',
'fear',
'worry',
'anxiety',
'sorrow',
'gloom',
'melancholy',
'dismay',
'disheartened',
'despair',
'dislike',
'aversion',
'antipathy',
'hate',
'disdain'
}
nltk.download('stopwords')
stop_words = set(stopwords.words('english'))
def remove_stopwords(sentence, stopwords_list):
tokens = nltk.word_tokenize(sentence)
filtered_tokens = [word for word in tokens if word.lower() not in stop_words ]
return ' '.join(filtered_tokens)
我自己写了这样的代码。也许这对你有用。
评论