我试图编写构建表格的代码,该表格显示语料库中所有单词之间的所有相关性。
我知道我可以在findAssocs
包中使用tm
来查找单个词的所有单词相关性,即findAssocs(dtm, "quick", 0.5)
- 会给我所有与之相关的单词字#"快速"高于0.5,但我不想手动为我的文本中的每个单词做这个。
#Loading a .csv file into R
file_loc <- "C:/temp/TESTER.csv"
x <- read.csv(file_loc, header=FALSE)
require (tm)
corp <- Corpus(DataframeSource(x))
dtm <- DocumentTermMatrix(corp)
#Clean up the text
corp <- tm_map(corp, content_transformer(tolower))
corp <- tm_map(corp, removeNumbers)
corp <- tm_map(corp, removePunctuation)
corp <- tm_map(corp, content_transformer(stripWhitespace))
dtm <- DocumentTermMatrix(corp)
从这里我可以找到单个词的相关词:
findAssocs(dtm, "quick", 0.4)
但我希望找到所有这样的相关性:
quick easy the and
quick 1.00 0.54 0.72 0.92
easy 0.54 1.00 0.98 0.54
the 0.72 0.98 1.00 0.05
and 0.92 0.54 0.05 1.00
有什么建议吗?
&#34; TESTER.csv&#34;数据文件(从单元格A1开始)
[1] I got my question answered very quickly
[2] It was quick and easy to find the information I needed
[3] My question was answered quickly by the people at stack overflow
[4] Because they're good at what they do
[5] They got it dealt with quickly and didn't mess around
[6] The information I needed was there all along
[7] They resolved it quite quickly
答案 0 :(得分:3)
您可以使用as.matrix
和cor
。 findAssocs
的下限为0:
(cor_1 <- findAssocs(dtm, colnames(dtm)[1:2], 0))
# all along
# there 1.00 1.00
# information 0.65 0.65
# needed 0.65 0.65
# the 0.47 0.47
# was 0.47 0.47
cor
可以获得所有与皮尔森相关的信息,以及它的价值:
cor_2 <- cor(as.matrix(dtm))
cor_2[c("there", "information", "needed", "the", "was"), c("all", "along")]
# all along
# there 1.0000000 1.0000000
# information 0.6454972 0.6454972
# needed 0.6454972 0.6454972
# the 0.4714045 0.4714045
# was 0.4714045 0.4714045
上述代码:
x <- readLines(n = 7)
[1] I got my question answered very quickly
[2] It was quick and easy to find the information I needed
[3] My question was answered quickly by the people at stack overflow
[4] Because they're good at what they do
[5] They got it dealt with quickly and didn't mess around
[6] The information I needed was there all along
[7] They resolved it quite quickly
library(tm)
corp <- Corpus(VectorSource(x))
dtm <- DocumentTermMatrix(corp)
corp <- tm_map(corp, content_transformer(tolower))
corp <- tm_map(corp, removeNumbers)
corp <- tm_map(corp, removePunctuation)
corp <- tm_map(corp, content_transformer(stripWhitespace))
dtm <- DocumentTermMatrix(corp)