jieba 库是优秀的中文分词第三方库,中文文本需要通过分词获得单个的词语
管理员身份运行cmd窗口输入命令:pip install jieba
lcut 将返回的对象转化为 list 对象返回
该方法适合用于搜索引擎构建倒排索引的分词,颗粒度较细
jieba.lcut_for_search 方法返回列表类型
开发者可以指定自己自定义的词典,以便包含jieba词库里没有的词。虽然jieba有新词识别能力,但是自行添加新词可以保证更高的正确率
sentence 为待提取的文本
topK 为返回几个TF/IDF权重最大的关键词,默认是20
jieba.posseg.dt 为默认词性标注分词器
标注句子分词后每个词的词性,采用和ictclas兼容的标记法
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))