NLP工具学习(1)Thulac分词工具与WordCloud词云

0 前言

数据集:某比赛数据集

1 下载安装

均使用清华源
thulac

pip install thulac -i https://pypi.tuna.tsinghua.edu.cn/simple

wordcloud

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple wordcloud

2 thunlac分词

user_path
设置用户词典,用户词典中的词会被打上uw标签。词典中每一个词一行,UTF8编码
t2s
默认False, 是否将句子从繁体转化为简体
just_seg
默认False, 时候只进行分词,不进行词性标注
ufilter
默认False, 是否使用过滤器去除一些没有意义的词语,例如“可以”。
model_path
设置模型文件所在文件夹,默认为models /
separator
默认为‘_’, 设置词与词性之间的分隔符

thulac(seg_only=True).cut_f('../analyse/input.txt', '../analyse/output.txt')

3 WordCloud词云

    cloud = WordCloud(font_path='msyh.ttc', background_color='white', width=800, height=800, margin=2, stopwords=stopwords)
    # cloud.generate(text='通过 大力 发展 社区 教育 , 使 我省 全民 终身 学习 的 教育 体系 已 深入人心 。')
    cloud.generate(words_str)
    cloud.to_file('../figure//word_cloud.png')

效果
在这里插入图片描述

4 可能遇到的问题

thulac源码版本与python版本不兼容,报错
解决方法:
修改源码

init.py

        input_f = open(input_file, 'r', encoding='utf8')
        output_f = open(output_file, 'w', encoding='utf8')

GBTangingDecoder.py

        start = time.perf_counter()

5 完整代码

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tqdm
import re
import string
import zhon.hanzi
import cv2
import os
from thulac import thulac
from wordcloud import WordCloud


# 常量
wordcloud_open = True


# 加载数据集
train_dataset = pd.read_csv('../data/train.csv', delimiter='\t', encoding='utf8')
datas = train_dataset['text']
labels = train_dataset['label']
print(labels.value_counts())


# 保存为input.txt文件
with open('../analyse/input.txt', 'w', encoding='utf8') as f:
    for line in datas.values.tolist():
        f.write(line + '\n')


# 显示并保存词云
def show_save_word_cloud(words_str):
    # 加载停用词表
    stopwords = set()
    words = [word.strip() for word in open('../data/stopwords_zh.txt', 'r', encoding='utf8').readlines()]
    stopwords.update(words)

    # 词云
    cloud = WordCloud(font_path='msyh.ttc', background_color='white', width=800, height=800, margin=2, stopwords=stopwords)
    # cloud.generate(text='通过 大力 发展 社区 教育 , 使 我省 全民 终身 学习 的 教育 体系 已 深入人心 。')
    cloud.generate(words_str)
    cloud.to_file('../figure//word_cloud.png')
    img = cv2.imread('../figure/word_cloud.png')
    cv2.imshow('WordCloud', img)
    k = cv2.waitKey(0)
    if k == 27:  # ESC
        cv2.destroyAllWindows()


# thulac分词
if not os.path.exists('../analyse/output.txt'):
    thulac(seg_only=True).cut_f('../analyse/input.txt', '../analyse/output.txt')
else:
    corpus = []
    all_words = ''
    with open('../analyse/output.txt', 'r', encoding='utf8') as f:
        for line in f.readlines():
            corpus.append(line.strip('\n'))
            all_words = all_words + ' ' + line.strip('\n')
    if wordcloud_open:
        show_save_word_cloud(all_words)

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热爱旅行的小李同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值