mcpField
S
sentiment-analyzer

sentiment-analyzer

github.com/dkyazzentwatwa数据与数据库12690 次安装30 周前更新
代码仓库

关于 sentiment-analyzer

sentiment-analyzer 是用于评估文本情感、情绪和主观性的 Python 工具及命令行工具。它支持单条文本和 CSV 批量处理,并可绘制情感分布及时间趋势。

主要功能

  1. 01区分正面、负面和中性情感
  2. 02提供极性和置信度分数
  3. 03检测主观性与情绪
  4. 04批量处理 CSV 数据
  5. 05绘制分布、趋势和词云

适用场景

  1. 01汇总产品评论的情感
  2. 02追踪带日期的社交媒体情绪
  3. 03分类客户反馈

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/dkyazzentwatwa/chatgpt-skills --skill sentiment-analyzer

安装后在 Claude Code 中输入/sentiment-analyzer确认已加载。

附带文件scripts/requirements.txtscripts/sentiment_analyzer.py

详细介绍

情感分析器

分析文本内容的情感,提供详细评分、情绪检测和可视化功能。可处理单条文本、CSV 文件,或跟踪一段时间内的情感趋势。

快速开始

from scripts.sentiment_analyzer import SentimentAnalyzer

# Analyze single text
analyzer = SentimentAnalyzer()
result = analyzer.analyze("I love this product! It's amazing.")
print(f"Sentiment: {result['sentiment']} ({result['score']:.2f})")

# Batch analyze CSV
results = analyzer.analyze_csv("reviews.csv", text_column="review")
analyzer.plot_distribution("sentiment_dist.png")

功能

  • 情感分类:正面、负面、中性,并提供置信度
  • 极性评分:-1.0(负面)至 +1.0(正面)
  • 主观性检测:区分客观与主观内容
  • 情绪检测:喜悦、愤怒、悲伤、恐惧、惊讶
  • 批量处理:分析包含任意文本列的 CSV 文件
  • 趋势分析:跟踪一段时间内的情感变化
  • 可视化:分布图、趋势图、词云

API 参考

初始化
analyzer = SentimentAnalyzer()
单条文本分析
result = analyzer.analyze("This is great!")
# Returns:
# {
#     'text': 'This is great!',
#     'sentiment': 'positive',  # positive, negative, neutral
#     'score': 0.85,            # -1.0 to 1.0
#     'confidence': 0.92,       # 0.0 to 1.0
#     'subjectivity': 0.75,     # 0.0 (objective) to 1.0 (subjective)
#     'emotions': {'joy': 0.8, 'anger': 0.0, ...}
# }
批量分析
# From list
texts = ["Great product!", "Terrible service.", "It's okay."]
results = analyzer.analyze_batch(texts)

# From CSV
results = analyzer.analyze_csv(
    "reviews.csv",
    text_column="review_text",
    output="results.csv"
)
趋势分析
# Analyze sentiment over time
results = analyzer.analyze_csv(
    "posts.csv",
    text_column="content",
    date_column="posted_at"
)
analyzer.plot_trend("sentiment_trend.png")
可视化
# Sentiment distribution
analyzer.plot_distribution("distribution.png")

# Sentiment over time
analyzer.plot_trend("trend.png")

# Word cloud by sentiment
analyzer.plot_wordcloud("positive", "positive_words.png")

CLI 用法

# Analyze single text
python sentiment_analyzer.py --text "I love this product!"

# Analyze file
python sentiment_analyzer.py --input reviews.csv --column review --output results.csv

# With visualization
python sentiment_analyzer.py --input reviews.csv --column text --plot distribution.png

# Trend analysis
python sentiment_analyzer.py --input posts.csv --column content --date posted_at --trend trend.png
CLI 参数
参数描述默认值
--text要分析的单条文本-
--input输入 CSV 文件-
--column文本列名称text
--date用于趋势分析的日期列-
--output输出 CSV 文件-
--plot保存分布图-
--trend保存趋势图-
--format输出格式(json、csv)json

示例

产品评价分析
analyzer = SentimentAnalyzer()
results = analyzer.analyze_csv("amazon_reviews.csv", text_column="review")

# Summary statistics
positive = sum(1 for r in results if r['sentiment'] == 'positive')
negative = sum(1 for r in results if r['sentiment'] == 'negative')
print(f"Positive: {positive}, Negative: {negative}")

# Average sentiment score
avg_score = sum(r['score'] for r in results) / len(results)
print(f"Average sentiment: {avg_score:.2f}")
社交媒体监控
analyzer = SentimentAnalyzer()

# Analyze tweets with timestamps
results = analyzer.analyze_csv(
    "tweets.csv",
    text_column="tweet_text",
    date_column="created_at"
)

# Plot sentiment trend
analyzer.plot_trend("twitter_sentiment.png", title="Brand Sentiment Over Time")
客户反馈分类
analyzer = SentimentAnalyzer()

feedback = [
    "Your support team was incredibly helpful!",
    "The product broke after one day.",
    "Shipping was on time.",
    "I'm extremely disappointed with the quality.",
    "It works as expected, nothing special."
]

for text in feedback:
    result = analyzer.analyze(text)
    print(f"{result['sentiment'].upper():8} ({result['score']:+.2f}): {text[:50]}")

输出格式

JSON 输出
{
  "text": "I love this product!",
  "sentiment": "positive",
  "score": 0.85,
  "confidence": 0.92,
  "subjectivity": 0.75,
  "emotions": {
    "joy": 0.82,
    "anger": 0.02,
    "sadness": 0.01,
    "fear": 0.03,
    "surprise": 0.12
  }
}
CSV 输出
textsentimentscoreconfidencesubjectivity
优质产品!positive0.850.910.80
糟透了……negative-0.720.880.65

依赖项

textblob>=0.17.0
pandas>=2.0.0
matplotlib>=3.7.0

局限性

  • 针对英语进行了优化(其他语言的准确度可能较低)
  • 可能无法准确检测讽刺和反语
  • 可能会遗漏依赖上下文的情感
  • 短文本(少于 5 个词)的置信度较低

评论

支持 Markdown