← 返回教程目录
第 5 章 · 第 2 节 词频分析

指定关键词:监控自家品牌在评论里的声量

Targeted Word Count: Brand & Competitor Tracking

老板常问的不是出现最多的词,而是我家品牌在评论里出现了多少次?这是词频的精准版 — 给定关键词表,统计每个词在语料里出现的次数。

典型场景:监控小米/华为/苹果在抖音弹幕里的声量变化,看竞品动态。

1. 准备关键词表

keywords = {


    '小米': ['小米', '米家', 'xiaomi'],


    '华为': ['华为', '荣耀', 'huawei'],


    '苹果': ['苹果', 'iphone', 'ios'],


    'OPPO': ['oppo', '欧珀'],


    'vivo': ['vivo', '维沃'],


}

2. 遍历语料统计

import re





def count_brand(text, alias_list):


    text_lower = text.lower()


    count = 0


    for alias in alias_list:


        count += len(re.findall(re.escape(alias.lower()), text_lower))


    return count





result = {brand: 0 for brand in keywords}


for text in texts:


    for brand, alias_list in keywords.items():


        result[brand] += count_brand(text, alias_list)





for brand, count in sorted(result.items(), key=lambda x: -x[1]):


    print(f'{brand:6s} {count:6d}')


# 小米     12483


# 华为      9872


# 苹果      8451


# OPPO     3201


# vivo     2987

3. 多文件批量 + 趋势对比

import os





results_by_date = {}


for filename in sorted(os.listdir('daily_comments')):


    if not filename.endswith('.txt'):


        continue


    date = filename[:10]


    texts_daily = open(f'daily_comments/{filename}', encoding='utf-8').read().splitlines()


    daily = {brand: 0 for brand in keywords}


    for text in texts_daily:


        for brand, alias_list in keywords.items():


            daily[brand] += count_brand(text, alias_list)


    results_by_date[date] = daily





df_trend = pd.DataFrame(results_by_date).T


df_trend.plot(figsize=(14, 6), marker='o')


plt.title('品牌声量趋势(30 天)')


plt.ylabel('提及次数')


plt.grid(alpha=0.3)


plt.savefig('brand_trend.png', dpi=150, bbox_inches='tight')
实战提醒:多品牌监控时,记得去重 — 一条评论里同时出现小米和华为是常态(对比评价),不要简单加和。

这 3 步 = 品牌监控的标准流水线。我们的指定词统计软件把这 3 步全包了,导入关键词表一键出趋势图。

想跳过写代码,直接出 Top-N 词频表 + 词云图?

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

查看淘宝软件 →