怎样用Python和TextBlob库实现简单的情感极性评分

访客 自然语言处理 1

本文目录导读:

  1. 安装TextBlob
  2. 基本用法示例
  3. 实用函数封装
  4. 高级功能示例
  5. 实际应用示例

我来介绍如何使用TextBlob库实现简单的情感极性评分。

安装TextBlob

pip install textblob
python -m textblob.download_corpora

基本用法示例

from textblob import TextBlob
# 1. 基础情感分析
text = "I love this product! It's amazing and wonderful."
blob = TextBlob(text)
# 情感极性:-1(负面) 到 1(正面)
print(f"极性: {blob.sentiment.polarity}")
print(f"主观性: {blob.sentiment.subjectivity}")
# 2. 分析多个文本
texts = [
    "This is terrible, I hate it so much.",
    "The weather is okay, not great but not bad.",
    "Absolutely fantastic! Best experience ever!",
    "It's a neutral statement without strong emotions.",
    "This product broke after one day. Very disappointed."
]
for text in texts:
    blob = TextBlob(text)
    polarity = blob.sentiment.polarity
    subjectivity = blob.sentiment.subjectivity
    # 分类情感
    if polarity > 0.2:
        sentiment = "正面"
    elif polarity < -0.2:
        sentiment = "负面"
    else:
        sentiment = "中性"
    print(f"文本: {text}")
    print(f"情感: {sentiment} (极性: {polarity:.3f}, 主观性: {subjectivity:.3f})")
    print("-" * 50)

实用函数封装

def analyze_sentiment(text):
    """
    分析文本情感并返回详细结果
    """
    blob = TextBlob(text)
    polarity = blob.sentiment.polarity
    subjectivity = blob.sentiment.subjectivity
    # 情感分类
    if polarity > 0.2:
        sentiment_label = "正面"
        emoji = "😊"
    elif polarity < -0.2:
        sentiment_label = "负面"
        emoji = "😞"
    else:
        sentiment_label = "中性"
        emoji = "😐"
    # 主观性分类
    if subjectivity > 0.5:
        subjectivity_label = "主观"
    else:
        subjectivity_label = "客观"
    return {
        "text": text,
        "polarity": polarity,
        "subjectivity": subjectivity,
        "sentiment": sentiment_label,
        "emoji": emoji,
        "subjectivity_type": subjectivity_label
    }
def batch_sentiment_analysis(texts):
    """
    批量分析多个文本的情感
    """
    results = []
    for text in texts:
        result = analyze_sentiment(text)
        results.append(result)
    return results
def get_sentiment_score(text):
    """
    获取标准化情感评分 (0-10)
    """
    blob = TextBlob(text)
    polarity = blob.sentiment.polarity
    # 将 -1到1 映射到 0到10
    score = (polarity + 1) * 5
    return round(score, 1)
# 使用示例
if __name__ == "__main__":
    # 单条文本分析
    review = "The customer service was excellent and very helpful!"
    result = analyze_sentiment(review)
    print(f"文本: {result['text']}")
    print(f"情感: {result['emoji']} {result['sentiment']}")
    print(f"极性评分: {result['polarity']:.3f}")
    print(f"主观性: {result['subjectivity']:.3f} ({result['subjectivity_type']})")
    print(f"标准化评分 (0-10): {get_sentiment_score(review)}")
    print("\n" + "="*50)
    # 批量分析
    reviews = [
        "Excellent product! Highly recommended.",
        "Not worth the money, very disappointed.",
        "Average quality, nothing special.",
        "Amazing! Better than expected.",
        "Poor customer support and slow delivery."
    ]
    results = batch_sentiment_analysis(reviews)
    print("批量分析结果:")
    print(f"{'文本':<50} {'情感':<8} {'极性':<8} {'评分':<8}")
    print("-" * 80)
    for r in results:
        score = get_sentiment_score(r['text'])
        print(f"{r['text'][:48]:<50} {r['emoji'] + ' ' + r['sentiment']:<8} {r['polarity']:<8.3f} {score:<8}")

高级功能示例

from textblob import TextBlob
import re
class SentimentAnalyzer:
    """情感分析器类"""
    def __init__(self):
        self.language = 'en'
    def analyze_sentence_by_sentence(self, text):
        """逐句分析情感"""
        blob = TextBlob(text)
        sentences = blob.sentences
        results = []
        for sentence in sentences:
            sentiment = sentence.sentiment
            results.append({
                'sentence': str(sentence),
                'polarity': sentiment.polarity,
                'subjectivity': sentiment.subjectivity
            })
        return results
    def get_detailed_analysis(self, text):
        """详细分析文本"""
        blob = TextBlob(text)
        return {
            'words': blob.words,
            'word_count': len(blob.words),
            'noun_phrases': blob.noun_phrases,
            'sentences_count': len(blob.sentences),
            'overall_sentiment': blob.sentiment
        }
    def sentiment_trend(self, text):
        """分析情感变化趋势"""
        sentences = self.analyze_sentence_by_句子(text)
        trend = []
        for i, sentence in enumerate(sentences):
            trend.append({
                'position': i + 1,
                'polarity': sentence['polarity']
            })
        # 计算趋势方向
        if len(trend) > 1:
            first_polarity = trend[0]['polarity']
            last_polarity = trend[-1]['polarity']
            direction = last_polarity - first_polarity
            if direction > 0.3:
                trend_direction = "积极趋势 📈"
            elif direction < -0.3:
                trend_direction = "消极趋势 📉"
            else:
                trend_direction = "平稳趋势 ➡"
        else:
            trend_direction = "单句,无趋势"
        return {
            'trend_points': trend,
            'direction': trend_direction
        }
# 使用高级功能
analyzer = SentimentAnalyzer()
# 逐句分析
long_text = """I was excited to try this product. 
The packaging was nice but the quality was disappointing. 
However, customer service solved my problems quickly."""
print("逐句分析:")
result = analyzer.analyze_sentence_by_句子(long_text)
for r in result:
    print(f"  句子: {r['sentence']}")
    print(f"  极性: {r['polarity']:.3f}")
    print()
# 情感趋势
print("情感趋势:")
trend = analyzer.sentiment_trend(long_text)
print(f"  趋势方向: {trend['direction']}")
for point in trend['trend_points']:
    print(f"  句子 {point['position']}: 极性 {point['polarity']:.3f}")

实际应用示例

# 产品评论分析
def analyze_product_reviews(reviews_file=None, reviews_list=None):
    """分析产品评论"""
    if reviews_list:
        reviews = reviews_list
    else:
        # 示例评论
        reviews = [
            "Best purchase ever! Works perfectly.",
            "Good but could be better.",
            "Total waste of money.",
            "Decent product for the price.",
            "I'm very satisfied with this purchase."
        ]
    # 分析所有评论
    total_polarity = 0
    positive = negative = neutral = 0
    for review in reviews:
        blob = TextBlob(review)
        polarity = blob.sentiment.polarity
        total_polarity += polarity
        if polarity > 0.2:
            positive += 1
        elif polarity < -0.2:
            negative += 1
        else:
            neutral += 1
    # 计算总体评分
    avg_polarity = total_polarity / len(reviews)
    print("=" * 40)
    print("产品评论分析报告")
    print("=" * 40)
    print(f"总评论数: {len(reviews)}")
    print(f"好评: {positive} ({positive/len(reviews)*100:.1f}%)")
    print(f"中评: {neutral} ({neutral/len(reviews)*100:.1f}%)")
    print(f"差评: {negative} ({negative/len(reviews)*100:.1f}%)")
    print(f"平均情感极性: {avg_polarity:.3f}")
    if avg_polarity > 0.2:
        print("总体评价: 积极 👍")
    elif avg_polarity < -0.2:
        print("总体评价: 消极 👎")
    else:
        print("总体评价: 中性 👌")
# 运行分析
analyze_product_reviews()

TextBlob的优点是简单易用,适合快速实现情感分析功能,不过需要注意的是,它只支持英文,且准确率有限,对于复杂语境的理解可能不够准确,如果需要更好的中文支持或更高准确率,可以考虑使用更专业的NLP库。

标签: 情感极性 TextBlob

抱歉,评论功能暂时关闭!