使用 R 进行文本挖掘来计算单词的频率

我想计算“不确定性”一词的出现,但前提是“经济政策”或“立法”或与政策有关的词出现在同一文本中。现在,我已经用 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]DocumentsRwin-library2.13tmstopwords 
#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)))
stack overflow Text mining using R to count frequency of words
原文答案

答案:

作者头像

您可以将您的术语文档矩阵转换为具有 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