鱼C论坛

 找回密码
 立即注册
查看: 1955|回复: 1

[已解决]Python问题求助

[复制链接]
发表于 2020-11-19 20:40:37 | 显示全部楼层 |阅读模式
30鱼币
1, 读取bookcomments的内容(文档在附件中)

2  对读取内容处理(去除标点,处理重复的评论)(使用正则表达式相关方法进行处理)

3 完成分词操作

4 生成词云
最佳答案
2020-11-19 20:40:38
是这样吗
  1. import re
  2. import jieba
  3. import wordcloud

  4. f = open('bookComments.txt')  #读取
  5. textlist=[]
  6. reg = "[^0-9A-Za-z\u4e00-\u9fa5]" #标点
  7. for eachline in f.readlines():
  8.     eachline = re.sub(reg,'',eachline) #去除标点
  9.     textlist.append(eachline)
  10. textlist=list(set(textlist)) #去重

  11. text = ''.join(textlist)
  12. w = wordcloud.WordCloud(width=1000,\
  13.     font_path="msyh.ttc",height=700)

  14. w.generate(" ".join(jieba.lcut(text)))
  15. w.to_file("词云.png")
复制代码

bookComments.rar

16.94 KB, 下载次数: 2

里面是bookcomments文档

最佳答案

查看完整内容

是这样吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-11-19 20:40:38 | 显示全部楼层    本楼为最佳答案   
是这样吗
  1. import re
  2. import jieba
  3. import wordcloud

  4. f = open('bookComments.txt')  #读取
  5. textlist=[]
  6. reg = "[^0-9A-Za-z\u4e00-\u9fa5]" #标点
  7. for eachline in f.readlines():
  8.     eachline = re.sub(reg,'',eachline) #去除标点
  9.     textlist.append(eachline)
  10. textlist=list(set(textlist)) #去重

  11. text = ''.join(textlist)
  12. w = wordcloud.WordCloud(width=1000,\
  13.     font_path="msyh.ttc",height=700)

  14. w.generate(" ".join(jieba.lcut(text)))
  15. w.to_file("词云.png")
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 01:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表