R2-37-第二阶段第三次作业

洪学志 2018-03-27 23:40:44 阅读: 1321

任务一:

library(DBI)

library(RMySQL)

killDbConnections()


con <- dbConnect(MySQL(),host="localhost",dbname="rdb",user="root",password="")


dbSendQuery(con,'SET NAMES utf8') 


rs <- dbSendQuery(con, "SELECT * FROM article WHERE isdone=1")


 

words = data.frame(word=c(), freq = c())


while (!dbHasCompleted(rs)) {


    chunk <- dbFetch(rs, 10)


    chunk$abstract<-iconv(chunk$abstract,"WINDOWS-1252","UTF-8")


    count=nrow(chunk)


    cnt=1


    while(cnt<=count){


        str = gsub("[[:punct:]]", "", tolower(chunk[cnt,5]))


        temp = as.vector(unlist(strsplit(str, split = " ")))


        temp_len = length(temp)


        cnt2 = 1


        while(cnt2 <= temp_len){


            if(temp[cnt2] %in% words$word){


                words[words$word == temp[cnt2],]$freq = words[words$word == temp[cnt2],]$freq+1


            }


            else{


                words = rbind(words,data.frame(word=c(temp[cnt2]),freq=c(1)))


            }


            cnt2 = cnt2+1


        }


        cnt = cnt +1


    }


}


head(words)

1-1.png

任务二:

library(wordcloud2)

wordcloud2(words[0:1000,])

1-2.png

任务三:

new_words = words[order(words$freq,decreasing=T),]

del_word = c('of','the','and','in','to','a','that','is','for','buy','with','we','are','an','this','these','as','from','which','at','their','have','or','our','its','but','how','be','as','here','on','can','into','data','between','both','also','by')

words2<-words[which(!(words$word %in% del_word)),]

wordcloud2(words2)

1-3.png

 
邀请讨论

附件

{{f.title}} 大小 {{f.file_size}} 下载 {{f.count_download}} 金币 {{f.count_gold}}
{{item.nick_name}} 受邀请回答 {{item.create_time}}
{{item.refer_comment.nick_name}} {{item.refer_comment.create_time}}

附件

{{f.title}} 大小 {{f.file_size}} 下载 {{f.count_download}} 金币 {{f.count_gold}}
切换到完整回复 发送回复
赞({{item.count_zan}}) 踩({{item.count_cai}}) 删除 回复 关闭
科研狗©2015-2024 科研好助手,京ICP备20005780号-1 建议意见

服务热线

178 0020 3020

微信服务号