jieba库安装管理员身份运行cmd窗口输入命令:pip install jieba jieba库功能介绍特征
分词功能
添加自定义词典开发者可以指定自己自定义的词典,以便包含jieba词库里没有的词。虽然jieba有新词识别能力,但是自行添加新词可以保证更高的正确率 用法
关键词提取
词性标注
案例一、精确模式import jieba list1 = jieba.lcut("中华人民共和国是一个伟大的国家") print(list1) print("精确模式:"+"/".join(list1)) 二、全模式list2 = jieba.lcut("中华人民共和国是一个伟大的国家",cut_all = True) print(list2,end=",") print("全模式:"+"/".join(list2)) 三、搜索引擎模式list3 = jieba.lcut_for_search("中华人民共和国是一个伟大的国家") print(list3) print("搜索引擎模式:"+" ".join(list3)) 四、修改词典import jieba text = "中信建投投资公司了一款游戏,中信也投资了一个游戏公司" 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) 五、词性标注import jieba.posseg as pseg words = pseg.cut("我爱北京天安门") for i in words: print(i.word,i.flag) 六、统计三国演义中人物出场的次数import jieba txt = 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 jieba excludes = {"将军","却说","荆州","二人","不可","不能","如此","如何"} 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)) 文章来源:https://www.cnblogs.com/L-hua/p/15584823.html |
|