前叙
该文章写作共花费二十分钟,阅读只需要七分钟左右,读完该文章后,你将学会使用少量代码,将中文小说,中文新闻,或者其他任意一段中文文本生成词云图
背景
在进行汉语自然语言处理时候,经常使用的几个方法,分词,清除停用词,以及获取新词,为了方便使用我们将其封装.
这样我们就可以通过一行简单的代码获取清除停用词并和英语一样分词完毕,并以空格分割的汉语字符串,或者还可以获得其他功能.
至于之所以加上这个例子,是因为之前写的任意中文文本生成中文词云代码比较,不宜于使用,而这次发的最基础版本的文件:FontCN_NLPtools.py 其功能完全是适宜于生成词云的例子,这样就可以通过少量代码,获取中文词云
代码结构
结构简介
- class FontCN_NLPtools()
- __init__(self, textPath, stopwordsPath):
- def ReadText(self, NewTextPath=False):
- def getNewWordsByNLPIR(self, number):
- def getNewWords(self, GetNewWordsNumber=20):
- def addUserWords(self, NewWordsList):
- def jiebaClearAndCutText(self, isAddWord=False):
- def NLPIRClearText(self, isAddWord=False):
- def NLPIR2016CutText(self):
- 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)
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()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
例子,生成中文词云
代码阅读说明
其结构与之前代码差距不大,仍然使用绘梨衣与路明非的图片进行演示.
相较于之前的文章 Python词云 wordcloud 十五分钟入门与进阶不同之处在于
- 我们这次不需要在wordcloud 设置停用词
- 我们将图片保存了下来
wc.to_file('保存路径')
- 对wc进行了更详细的设置
代码如下
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'
back_coloring_path = "img/lz1.jpg"
imgname1 = "WordCloudDefautColors.png"
imgname2 = "WordCloudColorsByImg.png"
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,
)
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))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
效果如下


