资源简介 2021—2022学年人教中图版必修一2.4 算法与程序综合应用之Python语言中jieba库的应用一、jieba库基本介绍 (1)、jieba库概述 jieba是优秀的中文分词第三方库 - 中文文本需要通过分词获得单个的词语 - jieba是优秀的中文分词第三方库,需要额外安装 - jieba库提供三种分词模式,最简单只需掌握一个函数 (2)、jieba分词的原理 Jieba分词依靠中文词库 - 利用一个中文词库,确定汉字之间的关联概率 - 汉字间概率大的组成词组,形成分词结果 - 除了分词,用户还可以添加自定义的词组二、jieba库使用说明 (1)、jieba分词的三种模式 精确模式、全模式、搜索引擎模式 - 精确模式:把文本精确的切分开,不存在冗余单词 - 全模式:把文本中所有可能的词语都扫描出来,有冗余 - 搜索引擎模式:在精确模式基础上,对长词再次切分 (2)、jieba库常用函数 三、jieba应用实例 四、利用jieba库统计三国演义中任务的出场次数import jiebatxt = open("D:\\三国演义.txt", "r", encoding='utf-8').read()words = jieba.lcut(txt) # 使用精确模式对文本进行分词counts = {} # 通过键值对的形式存储词语及其出现的次数for word in words:if len(word) == 1: # 单个词语不计算在内continueelse:counts[word] = counts.get(word, 0) + 1 # 遍历所有词语,每出现一次其对应的值加 1items = list(counts.items())#将键值对转换成列表items.sort(key=lambda x: x[1], reverse=True) # 根据词语出现的次数进行从大到小排序for i in range(15):word, count = items[i]print("{0:<5}{1:>5}".format(word, count))统计了次数对多前十五个名词,曹操不愧是一代枭雄,第一名当之无愧,但是我们会发现得到的数据还是需要进一步处理,比如一些无用的词语,一些重复意思的词语。五、去停用词的jieba分词停用词表:https:///goto456/stopwordsimport jieba# 创建停用词列表def stopwordslist():stopwords = [line.strip() for line in open('stop_words.txt', encoding='UTF-8').readlines()]return stopwords# 对句子进行中文分词def seg_depart(sentence):# 对文档中的每一行进行中文分词print("正在分词")sentence_depart = jieba.cut(sentence.strip())# 创建一个停用词列表stopwords = stopwordslist()# 输出结果为outstroutstr = ''# 去停用词for word in sentence_depart:if word not in stopwords:if word != '\t':outstr += wordoutstr += " "return outstr# 给出文档路径filename = "Init.txt"outfilename = "out.txt"inputs = open(filename, 'rb')outputs = open(outfilename, 'w')# 将输出结果写入ou.txt中for line in inputs:line_seg = seg_depart(line)outputs.write(line_seg + '\n')print("-------------------正在分词和去停用词-----------")outputs.close()inputs.close()print("删除停用词和分词成功!!!") 展开更多...... 收起↑ 资源预览