中文分词及词频统计

问题背景

最近在一个比赛里划水,需要完成一些词频统计的工作。

以及实验室任务还需要做一个短语挖掘和分词的任务,所以针对此来做一些实践。

很多东西不记住下次用就不会了,所以还是小小的记录一下,方便之后拿出来用。

基本工具

结巴分词

stanford nlp

import jieba
import os
import codecs
from collections import Counter


def get_words(txt):
    seg_list = jieba.cut(txt)
    c = Counter()
    for x in seg_list:
        if len(x) > 1 and x != '\r\n':
            c[x] += 1
    print('常用词频度统计结果')
    for (k, v) in c.most_common(50):
        print('%s%s %s  %d' % ('  '*(5-len(k)), k, '*'*int(v/2), v))


if __name__ == '__main__':
    with codecs.open('财经.txt', 'r', encoding='utf-8') as f:
        txt = f.read()
    get_words(txt)

参考资料

中文分词方法和软件工具汇总笔记:  https://zhuanlan.zhihu.com/p/86322679

自然语言处理NLP知识和产品的笔记:   https://zhuanlan.zhihu.com/p/85940466

猜你喜欢

转载自blog.csdn.net/caicai0001000/article/details/110133398
今日推荐