作者abcggg (小鸡逼逼)
看板R_Language
标题[问题] 关於文字探勘
时间Tue Aug 18 22:40:55 2015
[问题类型]:
程式谘询
[软体熟悉度]:
入门(写过其他程式,只是对语法不熟悉)
[问题叙述]:
大家好,最近刚接触文字探勘
参考了陈嘉葳大大的文章:用R进行中文 text Mining
(
http://goo.gl/3mTrDg)
我也照做了一番,有些地方因为有更新所以有自己修改。
问题1:
在输出TermDocumentMatrix时,出现了以下的样子
不知道怎麽把\n弄掉,原本的文章也没有长这样。
Docs
Terms 1 2 3 4 5
一生\n志愿 0 0 0 0 0
一生\n味道 0 0 0 0 0
一家\n嘴\n罐子 1 0 0 0 0
一连串 0 0 0 0 0
人\n 0 0 0 0 0
人\n人\n人\n强者\n同学 0 0 0 0 0
人\n人\n小学 0 0 0 0 0
人\n人\n事\n课\n精神 0 0 0 0 0
人\n人\n东西 0 0 0 0 0
人\n山\n双手 0 0 0 0 0
问题2:
输出的文字云长这样
http://i.imgur.com/W6Bo2Tk.png
明明程式码一样,不知为何我的却是方的,而且没有很密集。
想知道问题出在哪。
问题3:
由於原文章只锁定名词
d.corpus <- tm_map(d.corpus[1:100], segmentCN, nature = TRUE)
d.corpus <- tm_map(d.corpus, function(sentence) {
noun <- lapply(sentence, function(w) {
w[names(w) == "n"]
})
unlist(noun)
})
若所有词性都想要,该怎麽做呢?
我有试做看看,却在输出tdm时得到error
Error in `[.simple_triplet_matrix`(tdm, 1:10, 1:5) :
subscript out of bounds
[程式范例]:
大概的程式码如下:
d.corpus0 <- Corpus(DirSource('doc'), list(language = NA)) #语料库
d.corpus_clean <- tm_map(d.corpus0, removePunctuation)
d.corpus_clean <- tm_map(d.corpus_clean, removeNumbers)
d.corpus_clean <- tm_map(d.corpus_clean, function(word) {
gsub("[A-Za-z0-9]", "", word)
})
d.corpus_seg <- tm_map(d.corpus_clean[1:100], segmentCN, nature = TRUE)
d.corpus_seg2 <- tm_map(d.corpus_seg, function(sentence) {
noun <- lapply(sentence, function(w) {
w[names(w) == "n"]
})
unlist(noun)
})
#d.corpus_vec <- Corpus(VectorSource(d.corpus_seg)) #无法run
d.corpus_stop <- tm_map(d.corpus_seg2, removeWords, myStopWords)
#建立TermDocumentMatrix(自己修改过)
corpus_clean <- tm_map(d.corpus_stop, PlainTextDocument)
d.corpus_vec <- Corpus(VectorSource(corpus_clean))
tdm <- TermDocumentMatrix(d.corpus_vec, control = list(wordLengths = c(2,
Inf)))
#文字云
m1 <- as.matrix(tdm)
v <- sort(rowSums(m1), decreasing = TRUE)
d <- data.frame(word = names(v), freq = v)
wordcloud(d$word, d$freq, min.freq = 2, random.order = F, ordered.colors = F,
colors = rainbow(length(row.names(m1))))
这个问题已经困扰我好几天,想了很久也找很多资料还是无解,才想说上来请各位高手解
答
小妹第一次在本版发文,若有任何不妥请多多包涵:)
[关键字]:
文字探勘,text mining
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.34.144.35
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1439908857.A.A29.html
真的耶! 不好意思,我也回去试试看旧版本好了…
只是还想知道有没有人也有这个问题然後有解决的~
※ 编辑: abcggg (1.34.144.23), 08/19/2015 02:17:14