这篇教程python写得很实用,希望能帮到您。 jieba 库是优秀的中文分词第三方库,中文文本需要通过分词获得单个的词语
1、jieba库安装管理员身份运行cmd窗口输入命令:pip install jieba
2、jieba库功能介绍特征: 支持三种分词模式: 精确模式:试图将句子最精确地切开,适合文本分析 全模式:把句子中所有的可以成词的词语都扫描出来, 速度非常快,但是不能解决歧义 搜索引擎模式:在精确模式的基础上,对长词再次切分,提高召回率,适合用于搜索引擎分词
分词功能: jieba.cut 和 jieba.lcut 方法接受两个传入参数: - 第一个参数为需要分词的字符串
- cut_all参数用来控制是否采用全模式
lcut 将返回的对象转化为 list 对象返回
jieba.cut_for_search 和 jieba.lcut_for_search 方法接受一个参数 该方法适合用于搜索引擎构建倒排索引的分词,颗粒度较细 jieba.lcut_for_search 方法返回列表类型
添加自定义词典: 开发者可以指定自己自定义的词典,以便包含jieba词库里没有的词。虽然jieba有新词识别能力,但是自行添加新词可以保证更高的正确率 用法: 使用自定义词典文件: jieba.load_userdict(file_name) # file_name 是自定义词典的路径 使用jieba在程序中动态修改词典: jieba.add_word(new_words) # new_words 是想要添加的新词 jieba.del_word(words) # 删除words 关键词提取: jieba.analyse.extract_tags(sentence,topK) #需要先import jieba.analyse sentence 为待提取的文本 topK 为返回几个TF/IDF权重最大的关键词,默认是20 词性标注: jieba.posseg.POSTokenizer(tokenizer=None) 新建自定义分词器,tokenizer参数可指定内部使用的jieba.Tokenizer 分词 jieba.posseg.dt 为默认词性标注分词器 标注句子分词后每个词的词性,采用和ictclas兼容的标记法
3、案例
3.1、精确模式import jiebalist1 = jieba.lcut("中华人民共和国是一个伟大的国家")print(list1)print("精确模式:"+"/".join(list1))
3.2、全模式list2 = jieba.lcut("中华人民共和国是一个伟大的国家",cut_all = True)print(list2,end=",")print("全模式:"+"/".join(list2))
3.3、搜索引擎模式list3 = jieba.lcut_for_search("中华人民共和国是一个伟大的国家")print(list3)print("搜索引擎模式:"+" ".join(list3))
3.4、修改词典import jiebatext = "中信建投投资公司了一款游戏,中信也投资了一个游戏公司"word = jieba.lcut(text)print(word)# 添加词jieba.add_word("中信建投")jieba.add_word("投资公司")word1 = jieba.lcut(text)print(word1)# 删除词jieba.del_word("中信建投")word2 = jieba.lcut(text)print(word2)
3.5、词性标注import jieba.posseg as psegwords = pseg.cut("我爱北京天安门")for i in words:print(i.word,i.flag)
3.6、统计三国演义中人物出场的次数三演义文本下载: import jiebatxt = open("文件路径", "r", encoding='utf-8').read() # 打开并读取文件words = jieba.lcut(txt) # 使用精确模式对文本进行分词counts = {} # 通过键值对的形式存储词语及其出现的次数for word in words: if len(word) == 1: # 单个词语不计算在内 continue else: counts[word] = counts.get(word, 0) + 1 # 遍历所有词语,每出现一次其对应的值加 1 items = list(counts.items()) #将键值对转换成列表items.sort(key=lambda x: x[1], reverse=True) # 根据词语出现的次数进行从大到小排序 for i in range(15): word, count = items[i] print("{0:<10}{1:>5}".format(word, count)) import jiebaexcludes = {"将军","却说","荆州","二人","不可","不能","如此","如何"}txt = open("三国演义.txt", "r", encoding='utf-8').read()words = jieba.lcut(txt)counts = {}for word in words: if len(word) == 1: continue elif word == "诸葛亮" or word == "孔明曰": rword = "孔明" elif word == "关公" or word == "云长": rword = "关羽" elif word == "玄德" or word == "玄德曰": rword = "刘备" elif word == "孟德" or word == "丞相": rword = "曹操" else: rword = word counts[rword] = counts.get(rword,0) + 1 for i in excludes: del counts[i] items = list(counts.items())items.sort(key=lambda x:x[1], reverse=True) for i in range(10): word, count = items[i] print ("{0:<10}{1:>5}".format(word, count)) 到此这篇关于python 中的jieba分词库的文章就介绍到这了,更多相关python jieba分词库内容请搜索51zixue.net以前的文章或继续浏览下面的相关文章希望大家以后多多支持51zixue.net! python周期任务调度工具Schedule使用详解 Python |