jieba源码解析:jieba.cut

2 篇文章 1 订阅
2 篇文章 0 订阅

reference:
Python 中文 文本分析 实战:jieba分词+自定义词典补充+停用词词库补充+词频统计:
https://zhuanlan.zhihu.com/p/46922291
示例代码来自:https://zhuanlan.zhihu.com/p/143099147
jieba源码解析:https://www.cnblogs.com/aloiswei/p/11567616.html
停用词表:(中文常用停用词表)https://github.com/goto456/stopwords
jieba中文分词:https://github.com/fxsjy/jieba

jieba分词有三种模式:全模式、精确模式、搜索引擎模式。全模式和精确模式通过jieba.cut实现,搜索引擎模式对应cut_for_search,且三者均可以通过参数HMM决定是否使用新词识别功能。

四.算法思路
基于Trie树结构实现高效的词图扫描,生成句子中汉字所有可能成词情况所构成的有向无环图(DAG)
采用了动态规划查找最大概率路径, 找出基于词频的最大切分组合
对于未登录词,采用了基于汉字成词能力的HMM模型,使用了Viterbi算法

# Python中,读取文件时什么情况时需写上encoding=utf-8,什么时候不用写?
# 主要针对的是Windows系统。所以涉及到Windows系统或者跨系统文件读写最好都加上

#!/user/bin/env python3
# -*- coding: utf-8 -*-

from collections import Counter
import jieba


# jieba.load_userdict('userdict.txt')
# 创建停用词list
# Python中,读取文件时什么情况时需写上encoding=utf-8,什么时候不用写?
# 主要针对的是Windows系统。所以涉及到Windows系统或者跨系统文件读写最好都加上
def stopwordslist(filepath):
    stopwords = [line.strip() for line in open(filepath, 'r', encoding='UTF-8').readlines()]
    return stopwords


# 对句子进行分词
def seg_sentence(sentence):
    sentence_seged = jieba.cut(sentence.strip())
    stopwords = stopwordslist('stopwords.txt')  # 这里加载停用词的路径
    outstr = ''
    for word in sentence_seged:
        if word not in stopwords:
            if word != '\t':
                outstr += word
                outstr += " "
    return outstr


inputs = open('file1.txt', 'r', encoding='UTF-8')  # 加载要处理的文件的路径
outputs = open('file2', 'w')  # 加载处理后的文件路径
for line in inputs:
    line_seg = seg_sentence(line)  # 这里的返回值是字符串
    outputs.write(line_seg)
outputs.close()
inputs.close()
# WordCount
with open('file2', 'r') as fr:  # 读入已经去除停用词的文件
    data = jieba.cut(fr.read()) # 为什么fr已经是分好词的string,还要再分一遍词? fr.read()返回从字符串中读取的字节,所以需要再分一遍词。
data = dict(Counter(data)) # 如不再分一遍词,则结果为:data = {dict:5} {'你': 1, ' ': 3, '今': 1, '天': 1, '吃': 1}

with open('file3', 'w') as fw:  # 读入存储wordcount的文件路径
    for k, v in data.items():
        fw.write('%s,%d\n' % (k, v))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值