csv<- read.table("body1.txt",header=F)
csv <-t(csv)
library(tm)
txt<-Corpus(VectorSource(csv))
txt<-tm_map(txt,removeNumbers)#去除數(shù)字
txt<-tm_map(txt,stripWhitespace)#去除多余空格
txt<-tm_map(txt,removePunctuation)#去除標(biāo)點(diǎn)符號
txt<-tm_map(txt,removeWords,stopwords("english"))#將英文中的停詞刪掉:例如把that at 等英文介詞去掉。
txt<-tm_map(txt,PlainTextDocument)#去掉空文件
Sys.setenv(JAVA_HOME='C:/ProgramFiles/Java/jdk1.6.0_43/jre')
library(Rwordseg)
txt<-segmentCN(as.character(txt))#將語料庫中的中文時(shí)行分詞
txt<-Corpus(VectorSource(txt))
cnword<-read.table(file.choose(),header=F)
cnword<-as.vector(cnword[1:dim(cnword)[1],])#需要為向量格式
dtm<-DocumentTermMatrix(txt,control=list(dictionary=cnword,removePunctuation=TRUE,stopwords=TRUE,wordLengths = c(2, Inf)))
dtm2= removeSparseTerms(dtm, sparse=0.99)
d<- dist(dtm2, method = "euclidean")
fit<- hclust(d, method="ward.D")
plot(fit)