前叙
该文章写作共花费二十分钟,阅读只需要七分钟左右,读完该文章后,你将学会使用少量代码,将中文小说,中文新闻,或者其他任意一段中文文本生成词云图
背景
在进行汉语自然语言处理时候,经常使用的几个方法,分词,清除停用词,以及获取新词,为了方便使用我们将其封装.
这样我们就可以通过一行简单的代码获取清除停用词并和英语一样分词完毕,并以空格分割的汉语字符串,或者还可以获得其他功能.
至于之所以加上这个例子,是因为之前写的任意中文文本生成中文词云代码比较,不宜于使用,而这次发的最基础版本的文件:FontCN_NLPtools.py 其功能完全是适宜于生成词云的例子,这样就可以通过少量代码,获取中文词云
代码结构
结构简介
- - class FontCN_NLPtools()
- - __init__(self, textPath, stopwordsPath):
- - def ReadText(self, NewTextPath=False): # 读取文本,默认为初始化时传入的文本
- - def getNewWordsByNLPIR(self, number): # 自动发现单词,这是其他类调用的方法
- - def getNewWords(self, GetNewWordsNumber=20): # 获取新的单词,添加到self.__newWords,并将其返回
- - def addUserWords(self, NewWordsList): # 向用户词库self.__userWords 中添加新单词
- - def jiebaClearAndCutText(self, isAddWord=False): # 使用结巴清理停用词并返回分词字符串
- - def NLPIRClearText(self, isAddWord=False): # 使用NLPIR清理停用词
- - def NLPIR2016CutText(self): # 使用NLPIR分词
- - def getText(self, isJieba=True, isAddWord=False, GetNewWordsNumber=30): # 直接获取分词完毕并清理停用词后的中文字符串
使用示例:
- import FontCN_NLPtools as fts
-
- text_path = 'txt/lztest.txt' # 设置要分析的文本路径
- stopwords_path = 'stopwords\CNENstopwords.txt' # 停用词词表
-
-
- fontsTools = fts.FontCN_NLPtools(textPath=text_path, stopwordsPath=stopwords_path)
-
- # fontsTools.addUserWords([u'路明非'])
-
- # 便捷的处理完毕的中文字符串
- text = fontsTools.getText(isAddWord=True)
-
-
- # 处理第二段文本
- fontsTools.ReadText(NewTextPath=u'E:\Pythonfiles2.7\NLP\汉语自然语言处理基本组件\txt\dazhuzai17523.txt')
-
- newText = fontsTools.getText(isAddWord=True)
-
-
- # 或者只调用一个方法,或者进行任意组合
- fontsTools.ReadText(NewTextPath=u'E:\Pythonfiles2.7\NLP\汉语自然语言处理基本组件\txt\dazhuzai17523.txt')
-
- cutText = fontsTools.jiebaClearAndCutText()
- cutText2 = fontsTools.NLPIR2016CutText()
-
- # 在之后的版本中,我将会讲NLPIR以及jieba或者其他自然语言处理的类库进行高级封装,这样它将会更加简洁,近期我将会直接将词云生成功能封装到该文件中,并在之后的时间内在该code中陆续地添加其他新的功能,如果你有什么想法可以发送邮件给我:fonttian@gmail.com
下载地址我的NLP源码下载合集
例子,生成中文词云
代码阅读说明
其结构与之前代码差距不大,仍然使用绘梨衣与路明非的图片进行演示.
相较于之前的文章 Python词云 wordcloud 十五分钟入门与进阶不同之处在于
- 我们这次不需要在wordcloud 设置停用词
- 我们将图片保存了下来
wc.to_file('保存路径')
- 对wc进行了更详细的设置
代码如下
- # - * - coding: utf - 8 -*-
- #
- # 作者:田丰(FontTian)
- # 创建时间:'2017/7/28'
- # 邮箱:fonttian@Gmaill.com
- # CSDN:http://blog.csdn.net/fontthrone
- from os import path
- from scipy.misc import imread
- import matplotlib.pyplot as plt
- from wordcloud import WordCloud, ImageColorGenerator
- import sys
-
- reload(sys)
- sys.setdefaultencoding('utf-8')
-
- import FontCN_NLPtools as fts
-
- d = path.dirname(__file__)
-
- text_path = 'txt/lztest.txt' # 设置要分析的文本路径
- stopwords_path = 'stopwords\CNENstopwords.txt' # 停用词词表
-
- fontsTools = fts.FontCN_NLPtools(textPath=text_path, stopwordsPath=stopwords_path)
- fontsTools.addUserWords([u'路明非'])
- text = fontsTools.getText(isAddWord=True)
-
- font_path = 'D:\Fonts\simkai.ttf' # 为worldcloud设置中文字体路径没
- back_coloring_path = "img/lz1.jpg" # 设置背景图片路径
-
- imgname1 = "WordCloudDefautColors.png" # 保存的图片名字1(只按照背景图片形状)
- imgname2 = "WordCloudColorsByImg.png" # 保存的图片名字2(颜色按照背景图片颜色布局生成)
- back_coloring = imread(path.join(d, back_coloring_path)) # 设置背景图片
-
- # 设置词云属性
- wc = WordCloud(font_path=font_path, # 设置字体
- background_color="white", # 背景颜色
- max_words=2000, # 词云显示的最大词数
- mask=back_coloring, # 设置背景图片
- max_font_size=100, # 字体最大值
- random_state=42,
- width=1000, height=860, margin=2, # 设置图片默认的大小,但是如果使用背景图片的话,那么保存的图片大小将会
- )
-
- # 生成词云, 可以用generate输入全部文本(wordcloud对中文分词支持不好,建议启用中文分词),也可以我们计算好词频后使用generate_from_frequencies函数
- wc.generate(text)
- # 从背景图片生成颜色值
- image_colors = ImageColorGenerator(back_coloring)
-
- plt.figure()
- # 以下代码显示图片
- plt.imshow(wc)
- plt.axis("off")
- plt.show()
- # 绘制词云
-
- # 保存图片
- wc.to_file(path.join(d, imgname1))
-
- image_colors = ImageColorGenerator(back_coloring)
-
- plt.imshow(wc.recolor(color_func=image_colors))
- plt.axis("off")
- # 绘制背景图片为颜色的图片
- plt.figure()
- plt.imshow(back_coloring, cmap=plt.cm.gray)
- plt.axis("off")
- plt.show()
- # 保存图片
- wc.to_file(path.join(d, imgname2))