Python NLPIR2016 与 wordcloud 结合生成中文词云
来源:互联网 发布:淘宝企业店铺怎么操作 编辑:程序博客网 时间:2024/06/04 18:24
前叙
该博文继承之前的文章,进一步介绍NLPIR2016的使用,三个内容(利用NLPIR 的发现新词功能自动提取文本内的新词汇,解决worldcloud中英文混合只显示中文的问题,NLPIR与worldcloud结合生成词云)
写作本博客需要一个小时,阅读需要十二分钟.
使用 NLPIR2016 获取新词
# 之前已经展示过的代码,介绍了NLPIR2016中获取新词的三个方式# 获得新词,第二个参数控制新词的个数,排名按照TF-IDF(term frequency–inverse document frequency)排序# 该功能可以和AddUserWord()方法配合使用,以更好地获取分词效果# strs1 = GetNewWords(text,c_int(10),[c_char_p, c_int, c_bool])# print strs1# 获得新词(从txt文件中),第二个参数控制新词的个数,排名按照TF-IDF(term frequency–inverse document frequency)排序# strs10 = GetFileNewWords(text,c_int(10),[c_char_p, c_int, c_bool])# print strs10# WindowsError: exception: access violation reading 0x0000000000000000# 获得关键词,第二个参数控制新词的个数,排名按照TF-IDF(term frequency–inverse document frequency)排序# strs2= GetKeyWords(text,c_int(10),[c_char_p, c_int, c_bool])# print strs2# 我们将在代码中利用内存文本发现新词的方法封装起来# 第一个参数是要加入而文本,第二个参数是获取的新词个数def getNewWordsByNLPIR(text, number): txt1 = GetNewWords(text, c_int(number), [c_char_p, c_int, c_bool]) txt2 = txt1.split('#') txt3 = [] txt4 = [] txt5 = [] for item2 in txt2: txt3.append(item2.encode('utf-8').split('/')) if txt3 != []: txt4.append(txt3) txt3 = [] for i in txt4: for j in i: if j[0] != [] and j[0] != '': txt5.append(j[0]) return txt5#注意返回的是numpy的列表类型# 然后你就可以通过一个简单的循环将其添加进NLPIR的词典for item in NewWord AddUserWord(item)
worldcloud 无法显示中文的问题
- 谈谈解决时的思考过程
博主在写这篇博客时使用的是python2.7的,结果当NLPIR2016与wordcloud结合是,发现world只显示text中的英文,而不显示中文,而这和之前的结巴是不一样的,并非制定了字体就可以解决的.于是我便将整个程序运行过程中的编码格式都检查了一遍,最后确认每个环节都是utf-8的编码,而最后的输入worldcloud使用的是wordcloud.generate(text.encode(‘utf-8’)),但是结果还是不行.
百般无奈之下,博主先停下先去读读其它书,便拿起一本数据清洗的书读,当我在读到其中一句话python2.x处理文本数据时最好在一开始读取文件的时候就设定好编码时,突然灵光一闪想到,即使使用了encode(‘utf-8’)难道其实内存中的编码格式还是不对?
于是我试着使用codecs将text先存入硬盘,再按照utf-8读取出来,果然好了.但是很明显的代码真的很烂,但是我确实确定了确实是最后输入wordcloud的文本编码有问题,最起码并非真正的可用的utf-8编码(encode(‘utf-8’)并没有发挥作用),于是我试着直接将其替换为python内部使用的unicode对象,结果果然成功了. - 原因与解决方案
# 注意因为NLPIR2016在进行中文分词时会默认转换编码,所以输出的文本需要转化为utf-8的编码# 但是在python2.7中还能使用unicode(strs, encoding='utf8')将其转化为Unicode对象,而encode('utf-8')的是无法生效的.# 注意制定wordcloud的字体,否则中文可能显示乱码
生成词云实例
# - * - coding: utf - 8 -*-## 作者:田丰(FontTian)# 创建时间:'2017/5/23'# 邮箱:fonttian@163.com# CSDN:http://blog.csdn.net/fontthronefrom os import pathfrom nlpir import *from scipy.misc import imreadimport matplotlib.pyplot as pltfrom wordcloud import WordCloud, ImageColorGeneratorfrom ctypes import *import sysreload(sys)sys.setdefaultencoding('utf-8')d = path.dirname(__file__)text_path = 'txt/lztest.txt' # 设置要分析的文本路径stopwords_path = 'stopwords\stopwords1893.txt' # 停用词词表text = open(path.join(d, text_path)).read()txt = seg(text)kw_list = []seg_list = []# 获得新词,第二个参数控制新词的个数,排名按照TF-IDF(term frequency–inverse document frequency排序# 该功能可以和AddUserWord()方法配合使用,以更好地获取分词效果# print strs1# print type(strs1)# 富山雅史/n_new/28.45#白王血裔/n_new/19.83#秘仪咒文/n_new/19.19#风纪委员会主席/n_new/15.30#龙族/n_new/14.48#龙类/n_new/13.79#龙族血裔/n_new/13.78#龙族血统/n_new/13.19#执行部/n_new/12.74#白王/n_new/12.68#炼金/n_new/11.75## <type 'str'>def getNewWordsByNLPIR(text, number): txt1 = GetNewWords(text, c_int(number), [c_char_p, c_int, c_bool]) txt2 = txt1.split('#') txt3 = [] txt4 = [] txt5 = [] for item2 in txt2: txt3.append(item2.encode('utf-8').split('/')) if txt3 != []: txt4.append(txt3) txt3 = [] for i in txt4: for j in i: if j[0] != [] and j[0] != '': txt5.append(j[0]) return txt5strs1 = getNewWordsByNLPIR(text, 10)for i in strs1: print ifor t in txt: seg_list.append(t[0].encode('utf-8'))# 去除停用词def NLPIRclearText(seg_list): mywordlist = [] liststr = "/ ".join(seg_list) f_stop = open(stopwords_path) try: f_stop_text = f_stop.read() f_stop_text = unicode(f_stop_text, 'utf-8') finally: f_stop.close() f_stop_seg_list = f_stop_text.split('\n') for myword in liststr.split('/'): if not (myword.strip() in f_stop_seg_list) and len(myword.strip()) > 1: mywordlist.append(myword) return ''.join(mywordlist)# 去除完停用词的文本strs = NLPIRclearText(seg_list)# print sfont_path = r'C:\Windows\Fonts\simfang.ttf' # 为worldcloud设置中文字体路径没# 设置词云属性wc = WordCloud(font_path=font_path, # 设置字体 background_color="white", # 背景颜色 max_words=200, # 词云显示的最大词数 max_font_size=100, # 字体最大值 random_state=42, width=1000, height=860, margin=2, # 设置图片默认的大小,但是如果使用背景图片的话,那么保存的图片大小将会 )# 因为NLPIR在处理中文时,会自动修改文本编码,所以在使用worldcloud时需要修改文本编码# 否则会造成:无法在wordcloud中显示NLPIR分词后的汉语词语,而只能显示其中的英文wc.generate(unicode(strs, encoding='utf8'))plt.imshow(wc)plt.axis("off")plt.show()wc.to_file('test001.png')
阅读全文
2 0
- Python NLPIR2016 与 wordcloud 结合生成中文词云
- Python wordcloud之中文词云
- Python实现中文词云(wordcloud),根据背景图片生成词云
- python中文词云生成
- wordcloud用来制作中文词云
- NLP01-python的wordcloud实现中文词云小例
- 利用Python将已有TXT文档生成中文词云
- Python +wordcloud 生成词云
- 数据库多对多 一对多 建表 sublimeText 输出不全 wordcloud 中文词云为乱码
- python本地版wordCloud字符云生成
- python-wordcloud模块生成词云
- 微信聊天机器人、Python、中文词云
- 【python入门】制作一个自定义的中文词云
- 用Python做简易的中文词云
- python使用wordcloud生成词云(能识别中文)
- 封装汉语自然语言处理中的常用方法(附代码:生成中文词云)
- 封装汉语自然语言处理中的常用方法(附代码:生成中文词云)
- R中文词云包wordcloud2试用
- Reverse Linked List II
- IDEA maven打成jar包后使用ClassLoader获取不到资源文件
- JDBC连接池
- 墙绘装饰设计公司网站织梦模板免费下载
- float浮动的消除
- Python NLPIR2016 与 wordcloud 结合生成中文词云
- 1044. 火星数字(20)
- 279. Perfect Squares
- 一个简单的银行系统
- transient
- codeforce814B An express train to reveries (有点高中分类讨论的意思)
- [P2678]跳石头
- Android自定义网络请求状态组件
- centos 6.5安装erlang 18.3