服务热线
178 0020 3020
第一题:
library(RMySQL)
killDbConnections<-function(){
all_cons<-dbListConnections(MySQL())
print(all_cons)
for(con in all_cons)
+ dbDisconnect(con)
print(paste(length(all_cons),"connections killed."))
}
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[,4]为title,#chunk[,5]为abstract
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)
第二题:
library(wordcloud2)
wordcloud2(words[0:200,])
很有可能自己第二次的作业没有出全,网总是不好,所以设置成1000就不出图了,所以我设置了200
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','yet','than','well',
'it','not')
words2=words[which(!words$word %in% del_word),]
wordcloud2(words2,shape='star',size=0.5)
暂时参考老大的就只能做这么点,更多的是搬用代码,等毕业论文提交再做一次,一直觉得上次做的结果有问题,这次都有影响。
附件