【发布时间】:2018-05-16 06:04:28
【问题描述】:
我正在对推特数据执行 kmeans 聚类,为此我正在清理推文并创建一个语料库。后来我找到了dtm并使用了tf-idf理论。
但是我的 dtm 中几乎没有我想删除的空文档,因为 kmeans 无法为空文档运行。
这是我的代码:
removeURL <- function(x) gsub("http[[:alnum:][:punct:]]*", "", x)
replacePunctuation <- function(x)
{
x <- tolower(x)
x <- gsub("[.]+[ ]"," ",x)
x <- gsub("[:]+[ ]"," ",x)
x <- gsub("[?]"," ",x)
x <- gsub("[!]"," ",x)
x <- gsub("[;]"," ",x)
x <- gsub("[,]"," ",x)
x <- gsub("[@]"," ",x)
x <- gsub("[???]"," ",x)
x <- gsub("[€]"," ",x)
x
}
myStopwords <- c(stopwords('english'), "rt")
#preprocessing
tweet_corpus <- Corpus(VectorSource(tweet_raw$text))
tweet_corpus_clean <- tweet_corpus %>%
tm_map(content_transformer(tolower)) %>%
tm_map(removeNumbers) %>%
tm_map(removeWords,myStopwords) %>%
tm_map(content_transformer(replacePunctuation)) %>%
tm_map(stripWhitespace)%>%
tm_map(content_transformer(removeURL))
dtm <- DocumentTermMatrix(tweet_corpus_clean )
#tf-idf
mat4 <- weightTfIdf(dtm) #when i run this, i get 2 docs that are empty
mat4 <- as.matrix(mat4)
【问题讨论】:
-
你能发一个 tweet_raw$text 的样本吗?
-
dtm不是已经为空了吗?
标签: r cluster-analysis k-means corpus