一、说明
网上提供的一个例子,做了修改与订正。
二、程序
#调入分词的库
library("rJava")
library("Rwordseg")
#调入绘制词云的库
library("RColorBrewer")
library("wordcloud")
#读入数据(特别注意,read.csv竟然可以读取txt的文本)
myfile<-read.csv(file.choose(),header=FALSE)
#预处理,这步可以将读入的文本转换为可以分词的字符,没有这步不能分词
myfile.res <- myfile[myfile!=" "]
#分词,并将分词结果转换为向量
myfile.words <- unlist(lapply(X = myfile.res,FUN = segmentCN))
#剔除URL等各种不需要的字符,还需要删除什么特殊的字符可以依样画葫芦在下面增加gsub的语句
myfile.words <- gsub(pattern="http:[a-zA-Z\\/\\.0-9]+","",myfile.words)
myfile.words <- gsub("\n","",myfile.words)
myfile.words <- gsub(" ","",myfile.words)
#去掉停用词
data_stw=read.table(file=file.choose(),colClasses="character")
stopwords_CN=c(NULL)
for(i in 1:dim(data_stw)[1]){
stopwords_CN=c(stopwords_CN,data_stw[i,1])
}
for(j in 1:length(stopwords_CN)){
myfile.words <- subset(myfile.words,myfile.words!=stopwords_CN[j])
}
#过滤掉1个字的词
myfile.words <- subset(myfile.words, nchar(as.character(myfile.words))>1)
#统计词频
myfile.freq <- table(unlist(myfile.words))
myfile.freq <- rev(sort(myfile.freq))
#myfile.freq <- data.frame(word=names(myfile.freq),freq=myfile.freq);
#按词频过滤词,过滤掉只出现过一次的词,这里可以根据需要调整过滤的词频数
#特别提示:此处注意myfile.freq$Freq大小写
myfile.freq2=subset(myfile.freq, myfile.freq$Freq>=10)
#绘制词云
#设置一个颜色系:
mycolors <- brewer.pal(8,"Dark2")
#设置字体
windowsFonts(myFont=windowsFont("微软雅黑"))
#画图
wordcloud(myfile.freq2$word,myfile.freq2$Freq,min.freq=10,max.words=Inf,random.order=FALSE,
random.color=FALSE,colors=mycolors,family="myFont")
library("rJava")
library("Rwordseg")
#调入绘制词云的库
library("RColorBrewer")
library("wordcloud")
#读入数据(特别注意,read.csv竟然可以读取txt的文本)
myfile<-read.csv(file.choose(),header=FALSE)
#预处理,这步可以将读入的文本转换为可以分词的字符,没有这步不能分词
myfile.res <- myfile[myfile!=" "]
#分词,并将分词结果转换为向量
myfile.words <- unlist(lapply(X = myfile.res,FUN = segmentCN))
#剔除URL等各种不需要的字符,还需要删除什么特殊的字符可以依样画葫芦在下面增加gsub的语句
myfile.words <- gsub(pattern="http:[a-zA-Z\\/\\.0-9]+","",myfile.words)
myfile.words <- gsub("\n","",myfile.words)
myfile.words <- gsub(" ","",myfile.words)
#去掉停用词
data_stw=read.table(file=file.choose(),colClasses="character")
stopwords_CN=c(NULL)
for(i in 1:dim(data_stw)[1]){
stopwords_CN=c(stopwords_CN,data_stw[i,1])
}
for(j in 1:length(stopwords_CN)){
myfile.words <- subset(myfile.words,myfile.words!=stopwords_CN[j])
}
#过滤掉1个字的词
myfile.words <- subset(myfile.words, nchar(as.character(myfile.words))>1)
#统计词频
myfile.freq <- table(unlist(myfile.words))
myfile.freq <- rev(sort(myfile.freq))
#myfile.freq <- data.frame(word=names(myfile.freq),freq=myfile.freq);
#按词频过滤词,过滤掉只出现过一次的词,这里可以根据需要调整过滤的词频数
#特别提示:此处注意myfile.freq$Freq大小写
myfile.freq2=subset(myfile.freq, myfile.freq$Freq>=10)
#绘制词云
#设置一个颜色系:
mycolors <- brewer.pal(8,"Dark2")
#设置字体
windowsFonts(myFont=windowsFont("微软雅黑"))
#画图
wordcloud(myfile.freq2$word,myfile.freq2$Freq,min.freq=10,max.words=Inf,random.order=FALSE,
random.color=FALSE,colors=mycolors,family="myFont")
三、结果
作者:张子良
出处:http://www.cnblogs.com/hadoopdev
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类:
机器学习
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】