task3b-词云

作业链接

library(jiebaR)
library(wordcloud2)

#读入数据分隔符是‘\n’,字符编码是‘UTF-8’,what=''表示以字符串类型读入
file <- scan('ciyun.txt',sep='\n',what='',encoding="UTF-8")

txtList = lapply(file, strsplit,"\\s+") #使用空格符号进行分词
txtChar = tolower(unlist(txtList))

txtChar <- txtChar[nchar(txtChar)>2] #去除字符长度小于2的词语

txtChar <- table(txtChar) #统计词频
#grepl类似grep,但是返回逻辑向量,即是否包含pattern
txtChar <- txtChar[!grepl('^[0-9-]+$',names(txtChar),perl = TRUE)] #去除纯数字
txtChar <- txtChar[!grepl('^and|the$',names(txtChar),perl = TRUE)] #delete and the
txtChar <- sort(txtChar, decreasing = TRUE)[1:100] #降序排序,并提取出现次数最多的前100个词语
data=data.frame(txtChar)
data = dplyr::filter(data,Freq>=2)#保留频率>=2的单词

#.pdf替换成空白
data$txtChar = sub('\\.pdf','',data$txtChar)

#绘制词云图
wordcloud2(data, size = 1,shape = 'star')

词云

参考

https://zhuanlan.zhihu.com/p/22601260

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值