You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在VADER情感分析中添加自定义语句与极性?解决COVID-19相关推文双/三词组情感分类偏差问题

Hey there, I’ve run into similar context-specific hiccups with VADER before—great catch noticing that terms like "positive covid" are being mislabeled as positive sentiment when they clearly don’t carry that connotation in a pandemic context. Let’s break down how to fix this and add your own custom entries.

1. Correcting Misclassified COVID-19 Bigrams/Trigrams

VADER’s default lexicon treats "positive" as a strongly positive term, but in COVID-related text, phrases like "positive covid", "positive cases", "tested positive", and "test positive" are neutral at best, or even negative (since they indicate infection). The fix is to override these specific multi-word phrases with appropriate polarity scores, so VADER prioritizes these context-specific definitions over individual word meanings.

Here’s a concrete code example:

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

# Initialize the sentiment analyzer
analyzer = SentimentIntensityAnalyzer()

# Define custom lexicon entries: phrase -> polarity score (range: -4 to 4)
# Adjust scores based on how you want the phrase to be classified
custom_covid_lexicon = {
    "positive covid": 0.0,          # Neutral, since it's just a status
    "positive cases": -0.3,         # Slightly negative (indicates spread)
    "tested positive": -0.2,        # Mildly negative (personal infection status)
    "test positive": -0.2,
    "positive covid case": -0.4     # Example trigram with stronger negative weight
}

# Merge your custom entries into VADER's existing lexicon
analyzer.lexicon.update(custom_covid_lexicon)

# Test the adjusted analyzer
test_tweets = [
    "Just tested positive for COVID-19, feeling under the weather",
    "Local positive cases have dropped by 20% this week",
    "Got a positive covid test result—time to isolate"
]

for tweet in test_tweets:
    scores = analyzer.polarity_scores(tweet)
    print(f"Tweet: {tweet}")
    print(f"Sentiment breakdown (pos/neu/neg): {scores['pos']:.2f} / {scores['neu']:.2f} / {scores['neg']:.2f}\n")

This will ensure those problematic phrases no longer trigger the green positive column incorrectly.

2. General Method for Adding Custom Statements & Polarity Labels

If you want to add any other custom terms (single words, bigrams, trigrams) with specific polarity labels, the process is straightforward:

  • Create a dictionary where each key is your target phrase (exact string match, including spaces for multi-word terms), and the value is a polarity score from -4.0 (extremely negative) to 4.0 (extremely positive). Use 0.0 for neutral terms.
  • Use analyzer.lexicon.update(custom_dict) to merge your entries into VADER’s existing lexicon.

Quick Tips:

  • For trigrams or longer phrases, add them as full strings (e.g., "tested positive for covid") to ensure VADER recognizes the full context.
  • Tweak scores based on how strongly the phrase should influence sentiment—for example, "severe positive covid case" might get a score of -1.0 to reflect stronger negativity.

内容的提问来源于stack exchange,提问作者Buddika weerasinghe

火山引擎 最新活动