【问题标题】:Text mining using R to count frequency of words使用 R 进行文本挖掘来计算单词的频率
【发布时间】:2014-01-07 16:41:51
【问题描述】:

我想计算“不确定性”一词的出现次数,但前提是“经济政策”或“立法”或与政策有关的词出现在同一文本中。现在,我已经用 R 编写了一个代码来计算文本中所有单词的频率,但它不能辨别计数的单词是否出现在正确的上下文中。您对如何纠正这个问题有什么建议吗?

library(tm) #load text mining library
setwd('D:/3_MTICorpus') #sets R's working directory to near where my files are
ae.corpus<-Corpus(DirSource("D:/3_MTICorpus"),readerControl=list(reader=readPlain))
summary(ae.corpus) #check what went in
ae.corpus <- tm_map(ae.corpus, tolower)
ae.corpus <- tm_map(ae.corpus, removePunctuation)
ae.corpus <- tm_map(ae.corpus, removeNumbers)
myStopwords <- c(stopwords('english'), "available", "via")
ae.corpus <- tm_map(ae.corpus, removeWords, myStopwords) # this stopword file is at C:\Users\[username]\Documents\R\win-library\2.13\tm\stopwords 
#library(SnowballC)
#ae.corpus <- tm_map(ae.corpus, stemDocument)

ae.tdm <- DocumentTermMatrix(ae.corpus, control = list(minWordLength = 3))
inspect(ae.tdm)
findFreqTerms(ae.tdm, lowfreq=2)
findAssocs(ae.tdm, "economic",.7)
d<- Dictionary (c("economic", "uncertainty", "policy"))
inspect(DocumentTermMatrix(ae.corpus, list(dictionary = d)))

【问题讨论】:

  • 如何定义“相同文本”?一个句子、一段、一本书、一个文件?此外,您可以将“D:/correctdirectory”的一小部分下载到 PasteBin 并使示例具有可重复性,这意味着任何人都可以运行代码并尝试为您找到答案:stackoverflow.com/questions/5963269/…
  • @Freddy 感谢您的提示! “同文”是指同一篇报纸文章,我将通过一篇文章、一段话来区分。

标签: r text-mining tm


【解决方案1】:

您可以将术语文档矩阵转换为具有 0/1 值的矩阵

dtm$v[dtm$v > 0] <- 1

dtm <- as.matrix(dtm)

然后你就可以轻松使用table

table(tdm[which(rownames(tdm)=='uncertainty'),], tdm[which(rownames(tdm)=='economic_policy'),])

应该会产生这样的结果:

     0  1
  0 105  13
  1  7  5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 2016-02-22
    • 2022-01-14
    • 2015-08-04
    • 2013-11-17
    相关资源
    最近更新 更多