分词和词云

2022-12-27 04:10:13   第一文档网     [ 字体: ] [ 阅读: ] [ 文档下载 ]
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。下载word有问题请添加QQ:admin处理,感谢您的支持与谅解。点击这里给我发消息

#第一文档网# 导语】以下是®第一文档网的小编为您整理的《分词和词云》,欢迎阅读!
分词

import jieba

jieba.lcut('全国计算机等级考试') #精确模式

import jieba

jieba.lcut('全国计算机等级考试Python科目')

jieba.lcut('全国计算机等级考试Python科目',cut_all=True) #全模式

jieba.lcut_for_search('全国计算机等级考试Python科目')

#搜索引擎模式,先执行精确模式,再对其中长词进一步切分

jieba.add_word('Python科目') #向分词词典中增加新词 jieba.lcut('全国计算机等级考试Python科目')

from wordcloud import WordCloud

txt="Three Chinese astronauts have entered the core module\

of China's permanent space station to embark on their three-month\ mission, becoming the module's first occupants\

and pioneers in one of the nation's grandest space endeavors." wordcloud=WordCloud().generate(txt) wordcloud.to_file('Astronauts.png')

import jieba

from wordcloud import WordCloud

txt='程序设计语言是用于书写计算机程序的语言。\

语言的基础是一组记号和一组规则。根据规则由记号构成的记号串\ 的总体就是语言。在程序设计语言中,这些记号串就是程序。\ 程序设计语言有3个方面的因素,即语法、语义和语用。\

语法表示程序的结构或形式,亦即表示构成语言的各个记号之间的组合规律,\ 但不涉及这些记号的特定含义,也不涉及使用者。语义表示程序的含义,\ 亦即表示按照各种方法所表示的各个记号的特定含义,但不涉及使用者。' words=jieba.lcut(txt)

newtxt=''.join(words) #空格拼接

wordcloud=WordCloud(font_path='msyh.ttc').generate(newtxt) wordcloud.to_file('词云.png')

from wordcloud import WordCloud from imageio import imread

mask=imread(r'C:\Users\Administrator\Desktop\Alice.png') #f=open('Nightingale.txt','r',encoding='UTF-8')

f=open('AliceInWonderland.txt','r',encoding='gb18030') txt=f.read()

wordcloud=WordCloud(background_color='white',\


width=258,\ height=351,\

max_words=200,\ max_font_size=80,\

mask=mask).generate(txt) wordcloud.to_file('AliceInWonderland.png')


本文来源:https://www.dywdw.cn/550b8175862458fb770bf78a6529647d2628344b.html

相关推荐
推荐阅读