You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

是否存在与R语言syuzhe包功能类似的Python情感分析库?

Python Equivalents to R's syuzhet Package for NRC Emotion Lexicon Analysis

Great question! Since you're targeting a rule-based, lexicon-driven approach (no machine learning, perfect for unlabeled data) to detect the 8 NRC emotions—anger, anticipation, disgust, fear, joy, sadness, surprise, trust—here are your best Python options:

1. nrclex – The Direct Drop-In Equivalent

This library is built specifically around the NRC Word-Emotion Association Lexicon, mirroring the core functionality of syuzhet's get_nrc_sentiment() function. It counts occurrences of each of the 8 target emotions in your text, no training required.

How to use it:

First install the package:

pip install nrclex

Then implement it in your code:

from nrclex import NRCLex

# Example comment text
comment = "I'm stoked for the new game launch but terrified the servers will crash on day one!"

# Run emotion analysis
emotion_results = NRCLex(comment)

# Extract emotion counts (matches the 8 NRC categories + valence)
emotion_counts = emotion_results.affect_frequencies

# Print readable results
for emotion, count in emotion_counts.items():
    print(f"{emotion}: {count}")

This will output a breakdown of each emotion's presence, just like the R tool you're familiar with.

2. nltk + Custom NRC Lexicon Implementation

If you prefer using a more established library like NLTK, you can manually load the NRC lexicon and build a custom function to calculate emotion counts. This gives you full control over preprocessing steps like tokenization or stopword removal.

Steps & Example Code:

  1. Download the public NRC Emotion Lexicon (available as a CSV/TXT file)
  2. Load it into a Python dictionary mapping words to their associated emotions
  3. Preprocess your text and count emotion matches
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
import pandas as pd

# Download required NLTK resources
nltk.download('punkt')
nltk.download('stopwords')

# Load NRC lexicon (adjust file path to your local copy)
nrc_df = pd.read_csv("nrc_lexicon.csv", names=["word", "emotion", "association"])
emotion_map = {}
for _, row in nrc_df.iterrows():
    if row["association"] == 1:
        if row["word"] not in emotion_map:
            emotion_map[row["word"]] = []
        emotion_map[row["word"]].append(row["emotion"])

# Text preprocessing helper
def clean_text(text):
    tokens = word_tokenize(text.lower())
    stop_words = set(stopwords.words('english'))
    return [token for token in tokens if token.isalpha() and token not in stop_words]

# Custom emotion counting function
def get_nrc_emotions(text):
    tokens = clean_text(text)
    emotion_counts = {
        "anger": 0, "anticipation": 0, "disgust": 0, "fear": 0,
        "joy": 0, "sadness": 0, "surprise": 0, "trust": 0
    }
    for token in tokens:
        if token in emotion_map:
            for emotion in emotion_map[token]:
                if emotion in emotion_counts:
                    emotion_counts[emotion] += 1
    return emotion_counts

# Test with sample text
comment = "This product is amazing! I trust the brand completely, though I was surprised by how fast it shipped."
print(get_nrc_emotions(comment))

3. text2emotion (Partial Alternative)

While not strictly tied to the NRC lexicon, text2emotion is a simple out-of-the-box tool that detects similar core emotions. Note: it doesn't include anticipation, disgust, or trust, so it's only a partial fit for your full 8-emotion requirement.

Quick Usage:

pip install text2emotion
import text2emotion as te

comment = "The customer service was terrible— I'm so angry they ignored my complaint for weeks!"
print(te.get_emotion(comment))

内容的提问来源于stack exchange,提问作者Christian Gonzalez-Martel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:02:12