← 返回教程目录
第 2 章 · 第 1 节 情感分析

关键词词典法:5 行代码起步

Keyword Dictionary: 5 Lines to Start

最古老的情感分析方法 — 数正负词个数。5 行代码就能跑,适合"先看个大概"的场景。

1. 准备正负词典

positive = ["不错", "满意", "好评", "推荐", "喜欢", "棒", "完美"]


negative = ["差", "烂", "垃圾", "失望", "退货", "骗人", "坑"]

2. 核心函数:数正负词

def sentiment_score(text, pos, neg):


    pos_count = sum(1 for w in pos if w in text)


    neg_count = sum(1 for w in neg if w in text)


    if pos_count == 0 and neg_count == 0:


        return 0  # 中性


    return (pos_count - neg_count) / (pos_count + neg_count)

3. 批量分析 100 条评论

import pandas as pd





df = pd.read_csv('comments.csv')


df['score'] = df['text'].apply(lambda t: sentiment_score(t, positive, negative))


df['label'] = df['score'].apply(lambda s: 'pos' if s > 0 else ('neg' if s < 0 else 'neu'))





print(df['label'].value_counts())


# pos    58


# neg    32


# neu    10
优点:不依赖外部库、速度快(10 万条 1 秒跑完)、可解释性强(列出命中的词)。
缺点:准确率只有 60%~70%,新词跟不上,否定句处理不了("不差"会被判成负)。

4. 进阶:处理否定词

def has_negation(text, target, neg_words=["不", "没", "别"]):


    # 检查 target 前 1~2 个字是否有否定词


    idx = text.find(target)


    if idx <= 0:


        return False


    prefix = text[max(0, idx-2):idx]


    return any(n in prefix for n in neg_words)





# 改进版 sentiment_score


def sentiment_score_v2(text, pos, neg):


    pos_count = sum(1 for w in pos if w in text and not has_negation(text, w))


    neg_count = sum(1 for w in neg if w in text and not has_negation(text, w))


    if pos_count + neg_count == 0:


        return 0


    return (pos_count - neg_count) / (pos_count + neg_count)

这样"不差"就判成正了。下一节,我们升级到工业级方案 SnowNLP。

想用更准的算法?试试我们的情感分析软件,双引擎准确率 92%

配套软件内置案例数据 + 模板,导入即出图,无需写代码

查看淘宝软件 →