绵阳做网站的,安装wordpress软件,网站优化怎么看,网络产品代理加盟jieba库的使用: jieba库是一款优秀的 Python 第三方中文分词库#xff0c;jieba 支持三种分词模式#xff1a;精确模式、全模式和搜索引擎模式#xff0c;下面是三种模式的特点。 精确模式#xff1a;试图将语句最精确的切分#xff0c;不存在冗余数据#xff0c;适合做文…jieba库的使用: jieba库是一款优秀的 Python 第三方中文分词库jieba 支持三种分词模式精确模式、全模式和搜索引擎模式下面是三种模式的特点。 精确模式试图将语句最精确的切分不存在冗余数据适合做文本分析 全模式将语句中所有可能是词的词语都切分出来速度很快但是存在冗余数据 搜索引擎模式在精确模式的基础上对长词再次进行切分. jieba的使用 # -*- coding: utf-8 -*-import jieba seg_str 好好学习天天向上。 print(/.join(jieba.lcut(seg_str))) # 精简模式返回一个列表类型的结果print(/.join(jieba.lcut(seg_str, cut_allTrue))) # 全模式使用 cut_allTrue 指定 print(/.join(jieba.lcut_for_search(seg_str))) # 搜索引擎模式 jieba库对英文单词的统计 # -*- coding: utf-8 -*- def get_text():txt open(1.txt, r, encodingUTF-8).read()txt txt.lower()for ch in !#$%()*,-./:;?[\\]^_‘{|}~:txt txt.replace(ch, ) # 将文本中特殊字符替换为空格return txt file_txt get_text()words file_txt.split() # 对字符串进行分割获得单词列表counts {} for word in words:if len(word) 1:continueelse:counts[word] counts.get(word, 0) 1 items list(counts.items()) items.sort(keylambda x: x[1], reverseTrue) for i in range(5):word, count items[i]print({0:5}-{1:5}.format(word, count)) 词云的制作 完成安装jieba wordcloud matplotlib 1打开taglue官网点击import words把运行的结果copy过来。2选择形状在这里是网上下载的图片进行的导入。3选择字体。4点击Visualize生成图片。 from wordcloud import WordCloud
import matplotlib.pyplot as plt
import jiebadef create_word_cloud(filename):text open(哈姆雷特.txt.format(filename)).read()wordlist jieba.cut(text, cut_allTrue) wl .join(wordlist)wc WordCloud(background_colorblack,max_words2000,font_pathsimsun.ttf,height1200,width1600,max_font_size100,random_state100,)myword wc.generate(wl) plt.imshow(myword)plt.axis(off)plt.show()wc.to_file(img_book.png)if __name__ __main__:create_word_cloud(mytext) 转载于:https://www.cnblogs.com/zhoukun520/p/10649666.html