Python3进行中文文章分词实现词云图与TOP词频统计

转自:https://blog.csdn.net/MG_ApinG/article/details/87776308

工具:Python 3

一下是代码,实现对docx文件的中文文章分词以及作词云图、TOP词频统计


 
 
  1. import docx
  2. import jieba
  3. from scipy.misc import imread
  4. import numpy as np
  5. import matplotlib
  6. import matplotlib.pyplot as plt
  7. from matplotlib import font_manager
  8. from PIL import Image, ImageDraw, ImageFont
  9. from wordcloud import WordCloud, ImageColorGenerator
  10. versionN = 6 # 版本
  11. filePath01 = r'F://data_temp/wordTest01.docx' # 源文件路径
  12. filePath02 = r'F://data_temp/wordCut-v{0}.txt'.format(versionN) # 分词结果文件保存路径
  13. filePath03 = r'F://data_temp/wordCount-v{0}.txt'.format(versionN) # # 分词词频统计结果文件保存路径
  14. filePath04 = r"F://data_temp/test.jpg" # 词云图背景图片
  15. filePath05 = r'F://data_temp/wordCloud-v{0}.jpg'.format(versionN) # 词云图保存路径
  16. filePath06 = r'F://data_temp/全新硬笔行书简体.ttf' # 字体文件
  17. filePath07 = r'F://data_temp/wordCountBar-v{0}.jpg'.format(versionN) # TOP词频图保存路径
  18. file01 = docx.Document(filePath01)
  19. docText01 = ''
  20. for i in file01.paragraphs:
  21. docText01 = docText01 + i.text
  22. segList = '/'.join(jieba.cut(docText01, cut_all= False)) # cut_all=False 精确分词,分词符号为/
  23. with open(filePath02, 'a', encoding= 'utf-8') as f1: # 保存分词结果
  24. f1.write(segList)
  25. f1.close()
  26. wordList = segList.split( '/')
  27. arr = np.array(wordList)
  28. keyUse = np.unique(arr)
  29. wordDict = {}
  30. for i in keyUse:
  31. mask = (arr == i) # return like this [ True False ... False False True]
  32. arr_new = arr[mask] # get the True index element
  33. v = arr_new.size # 计数 count the size of i
  34. wordDict[i] = v # 赋值 assignment index i of dict
  35. wordDictSorted = sorted(wordDict.items(), key= lambda item: item[ 1], reverse= True) # reverse=True 按value值降序排列
  36. PunctuationS = [ ',', '。', '?', '、', ' ', '“', '”', ':', '(', ')',
  37. '.', '', '', '', '', '', '', '', '', '', '', '', '',
  38. '', '', '', '', '', '', '', '', '', '', '', '', '']
  39. with open(filePath03, 'a', encoding= 'utf-8') as f2:
  40. for i in wordDictSorted:
  41. if i[ 0] not in PunctuationS and len(i[ 0]) > 1:
  42. f2.write( '{0}|{1}\n'.format(i[ 0], i[ 1]))
  43. else:
  44. continue
  45. f2.close()
  46. color_mask = imread(filePath04) # 读取背景图片,注意路径
  47. wc = WordCloud(
  48. scale= 6, # 越大分辨率越高
  49. font_path= "simkai.ttf", # 设置字体,不指定就会出现乱码,注意字体路径
  50. #font_path=path.join(d,'simsun.ttc'),
  51. background_color= 'white', # 设置背景色
  52. mask=color_mask, # 词云形状
  53. max_words= 2000, # 允许最大词汇
  54. max_font_size= 60 # 最大号字体
  55. )
  56. wc.generate(segList) # 产生词云
  57. image_colors = ImageColorGenerator(color_mask) # 从背景图片生成颜色值
  58. wc.to_file(filePath05) # 保存图片
  59. plt.figure() # 修复不显示图片的bug
  60. plt.imshow(wc.recolor(color_func=image_colors), interpolation= "bilinear") # 实现词云图片按照图片颜色取色
  61. plt.axis( "off") # 关闭坐标轴
  62. plt.show()
  63. # 画出词频统计条形图,用渐变颜色显示,选取前N个词频
  64. fig, ax = matplotlib.pyplot.subplots() # fig:matplotlib.figure.Figure对象 ax:Axes(轴)对象或Axes(轴)对象数组
  65. myFont = matplotlib.font_manager.FontProperties(fname=filePath06) # 指定一个ttf字体文件作为图表使用的字体
  66. # 默认状态下,matplotlb无法在图表中使用中文
  67. words = []
  68. counts = []
  69. topN = 30
  70. wordCount01 = open(filePath03, 'r', encoding= 'utf-8')
  71. for i in wordCount01:
  72. words.append(i.split( '|')[ 0])
  73. counts.append(int(i.split( '|')[ 1].strip( r'\n')))
  74. # 这里是为了实现条状的渐变效果,以该色号为基本色实现渐变效果
  75. colors = [ '#FA8072']
  76. for i in range(len(words[: 30]) - 1):
  77. colors.append( '#FA{0}'.format(int(colors[ -1][ 3:]) - 1))
  78. rectS = ax.barh(np.arange(topN), counts[:topN], align= 'center', color=colors) # 绘制横向条形图
  79. # 修改Y轴的刻度
  80. ax.set_yticks(np.arange(topN)) # 设置刻度值
  81. ax.set_yticklabels(words[:topN], fontproperties=myFont) # 因为已经排序好,所以直接取前三十个即可,用词替换刻度值
  82. ax.invert_yaxis() # 翻转Y坐标轴
  83. ax.set_title( '文章中的高频词汇', fontproperties=myFont, fontsize= 17) # 设置标题
  84. ax.set_xlabel( u"出现次数", fontproperties=myFont) # 设置X轴标题
  85. for rect in rectS:
  86. width = rect.get_width()
  87. ax.text( 1.03 * width, rect.get_y() + rect.get_height()/ 2., '%d' % int(width), ha= 'center', va= 'center')
  88. plt.rcParams[ 'figure.figsize'] = ( 8.0, 4.0) # 设置figure_size尺寸
  89. plt.rcParams[ 'savefig.dpi'] = 300 # 图片像素
  90. plt.rcParams[ 'figure.dpi'] = 300 # 分辨率
  91. plt.savefig(filePath07)
  92. # 不知道为什么会报错ValueError: setting an array element with a sequence.,但是保存图片成功
  93. plt.show()

以下是部分分词结果:


 
 
  1. 简单/易懂/的/机器/学习/知识/(/一/)/:/人工智能/、/建模/和/机器/学习/ 96/ / /Vency/ /2017.11/./ 28/ /00/: /42/* / /字数/
  2. /1454/ /阅读/ /437/评论/ /0/喜欢/ /4/写/在/前面/:/本文/是/系列/的/第一篇/文章/,/“/简单/易懂/的/机器/学习/知识/”/
  3. 系列/文章/一方面/是/为了/让/更/多/人/了解/、/入门/机器/学习/;/另一方面/,/也/是/为了/让/自己/在/机器/学习/领域/持
  4. 续/学习/下去/。/在/本/系列/中/,/不会/讲/细节/的/算法/和/论证/过程/(/我/暂时/也/不会/•/•/•/)/,/会/讲/一些/简单/
  5. 易懂/的/基础知识/,/并/附以/案例/助于/理解/。/ 1/./人工智能/是/什么/?/在/狭义/上/,/人工智能/(/AI/)/是/指以/Siri/、
  6. /Alexa/等/语音/助手/,/用/语音/代替/界面/交互/的/个人/虚拟/助手/。/在/广义/上/,/人工智能/(/AI/)/是/指/由/人工/制
  7. 造/出/的/智能/机器/,/是/一种/能够/学习/的/计算机程序/,/可/代替/人类/去/解决/需要/人类/智慧/才能/解决/的/问题/。/
  8. 人工智能/包括/自然语言/处理/、/语音/识别/、/图像识别/、/机器/学习/等/,/每/一个/分支/都/很/复杂/和/庞大/。/本文/主
  9. 讲/机器/学习/,/其他/的/大家/可/自行/研究/。/ 2/./什么/是/建模/?/在/我们/深入/了解/之前/,/先说/一下/建模/的/概念/。
  10. /建模/是/指/把/具体/问题/抽象/成为/某/一类/问题/并用/数学模型/表示/,/是/应用/于/工程/、/科学/等/各/方面/的/通用/方
  11. 法/,/是/一种/对/现实/世界/的/抽象/总结/。/(/PS/:/实际/建模/应用/于/社会/各个方面/,/产品/经理/在/从/实际/业务/中
  12. /梳理/出/角色/、/流程/和/实体/也/是/建模/过程/。/如果/眼中/只有/数学/建模/就/过于/狭隘/了/。/)/建模/的/流程/具体/如
  13. 下/:/分析/问题/中/的/各种因素/,/并用/变量/表示/→/分析/变量/之间/的/关系/,/相互依存/或/独立/等/→/根据/实际/问题/
  14. 选用/合适/的/数学/框架/(/典型/的/有/优化/问题/,/配置/问题/等/)/,/并/将/具体/问题/在/此/框架/下/表达/出/某种/公式/
  15. →/选用/合适/的/算法/求解/表达/出/的/公式/→/使用/计算结果/解释/实际/问题/,/并/分析/结果/。/由此可见/,/在/我们/描述

词云图:

TOP词频图:

效果还不错。

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值