文本分析学习笔记

文本数据的获取

文本数据包括两个部分:评论数据和文章数据,对于评论数据这里推荐两个非常好用额采集器

  • 后羿采集器
  • 八爪鱼采集器
    这两款采集器非常智能,而且采集的效率很高,不用写代码,但是对于比较复杂的数据还是使用python比较好
    提示:采集数据一定要符合网站采集规范哦

词频统计

**工具:**python的jiaba库
**步骤:**过滤通用词;分词;词频统计
**数据简单描述:**评论数据存放在数据框的“内容”这一列中

  1. 获取短评内容

得到的sentences为一个列表,每一个元素为一条评论

sentences = df["发布内容"].dropna().astype(str).values.tolist()
  1. 过滤停用词

加载停用词txt,将文本中的停用词用空格代替

with open("stopwords.txt", "r", encoding="utf8") as f:
    stopwords = [line.replace("\n", "") for line in f.readlines()]
for stopword in stopwords:
    sentence = sentence.replace(stopword, "")
  1. 加载一些自定义词汇,进行分词的操作
ls = []
    jieba.load_userdict("userwords.txt")
    for sentence in tqdm(sentences):
        sentence = filter_stopwords(sentence)
        words = jieba.lcut(sentence)
        ls.extend(words)
  1. 统计词频
word_dict = [(word, freq) for word, freq in Counter(ls).most_common(1000) if len(word) > 1][:1000]
    print(word_dict[:15])

词云图的绘制

 # 绘制词云
 new_textlist =' '.join(ls)   # 将分词组合起来
 pic = imageio.imread("cloud.jpg")  # 读取图片
 color_list = ['#FF0000','#a41a1a'] # 建立颜色数组
 colormap = colors.ListedColormap(color_list)
 wc = WordCloud(background_color='white',# 构造词云的实例对象
             	mask = pic, # 形状
            	min_font_size=2, #字体大小
                max_font_size=60,
                random_state=30,
                font_path="msyh.ttc", # 字体类型
                max_words=1000,
                colormap = colormap,
                collocations=False,
     )
 wc.generate(new_textlist)    #生成词云图
 plt.figure(figsize=(20, 10))    # 画图
 plt.imshow(wc)
 plt.axis("off")
 plt.show()
 wc.to_file(file_name.split('.')[0]+'.png')   #保存图片
    

通过如上步骤就完成了一张简易的词云图的绘制
问题注意:

  1. 停用词和自定义词的txt文件需要自己进行指定
  2. 词云背景图尽量从晚上找一些背景为纯色的图片,这样效果比较好
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值