美文网首页数据分析
数据分析-词云实战

数据分析-词云实战

作者: nonoBoy | 来源:发表于2017-02-16 17:29 被阅读88次

    参考上一篇文章,做好分词,得到分词后的文件:jianshu.segment.txt
    下面开始词云绘制工作:
    第一步:

    #读入数据(特别注意,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.freq2=subset(myfile.freq, myfile.freq$freq.Freq>=2)
    

    第六步:

    #绘制词云
    #设置一个颜色系:
    mycolors<- brewer.pal(8,"Dark2")
    
    #设置字体
    windowsFonts(myFont=windowsFont("华文彩云"))
    
    #画图
    wordcloud(myfile.freq2$word,myfile.freq2$freq.Freq,random.order=FALSE, random.color=FALSE,colors=mycolors,family="myFont")
    

    最终附图(文本内容为简书刘淼的文章合集):

    简书_刘淼文集.png

    相关文章

      网友评论

        本文标题:数据分析-词云实战

        本文链接:https://www.haomeiwen.com/subject/gxehwttx.html