2

我正在使用Rtmqdap. 当我的向量 ( words) 只有几个词时,一切看起来都很好:

library(tm)
library(qdap)
text <- "activat affect affected affecting affects aggravat allow attribut based basis
bc because bosses caus change changed changes changing compel compliance"
text <- Corpus(VectorSource(text))
words <- c("activat", "affect", "affected")

# Using termco to search for the words in the text
apply_as_df(text, termco, match.list=words)

# Results:
#      docs    word.count    activat    affect    affected
# 1   doc 1            20   1(5.00%) 4(20.00%)    1(5.00%)

但是当我的向量 ( words) 有太多单词时,结果会变得乱码和不可读:

words <- c("activat", "affect", "affected", "affecting", "affects", "aggravat", "allow",
           "attribut", "based", "basis", "bc", "because", "bosses", "caus", "change",
           "changed", "changes", "changing", "compel", "compliance")

# Using termco to search for the words in the text
apply_as_df(text, termco, match.list=words)

# Results:
#     docs word.count  activat    affect affected affecting  affects aggravat    allow
# attribut    based    basis       bc  because   bosses      caus    change  changed
#  changes changing   compel compliance
# 1  doc 1         20 1(5.00%) 4(20.00%) 1(5.00%)  1(5.00%) 1(5.00%) 1(5.00%) 1(5.00%)
# 1(5.00%) 1(5.00%) 1(5.00%) 1(5.00%) 1(5.00%) 1(5.00%) 2(10.00%) 3(15.00%) 1(5.00%)
# 1(5.00%) 1(5.00%) 1(5.00%)   1(5.00%)

如何将结果显示在数据框/矩阵中,以便更轻松地阅读它们?


我尝试使用termco2matqdap库),它应该像这样“返回术语计数矩阵”(https://trinker.github.io/qdap/termco.html)(请参见下文),但我得到一个错误:

apply_as_df(text, termco2mat, match.list=words)

# Results:
# Error in qdapfun(text.var = text, ...) : 
#   unused arguments (text.var = text, match.list = c("activat", "affect", "affected",
# "affecting", "affects", "aggravat", "allow", "attribut", "based", "basis", "bc",
# "because", "bosses", "caus", "change", "changed", "changes", "changing", "compel",
# "compliance"))

或者:

termco2mat(apply_as_df(text, termco, match.list=words))

# Results:
# Error in `rownames<-`(`*tmp*`, value = "doc 1") : 
#   attempt to set 'rownames' on an object with no dimensions
4

2 回答 2

0

我不确定您要做什么,但不确定scores counts如何从列表中提取对象。也许你想t转置输出?

apply_as_df(text, termco, match.list=words) %>%
    counts() %>%
    t()

## docs       "doc 1"
## word.count "20"   
## activat    "1"    
## affect     "4"    
## affected   "1"    
## affecting  "1"    
## affects    "1"    
## aggravat   "1"    
## allow      "1"    
## attribut   "1"    
## based      "1"    
## basis      "1"    
## bc         "1"    
## because    "1"    
## bosses     "1"    
## caus       "2"    
## change     "3"    
## changed    "1"    
## changes    "1"    
## changing   "1"    
## compel     "1"    
## compliance "1"    
于 2016-02-04T05:33:12.110 回答
0

这是一个没有 qdap 的解决方案:

library(tm)
text1 <- "activat affect affected affecting affects aggravat allow attribut"
text2 <- "based basis bc because bosses caus change changed changes changing compel compliance"
text <- Corpus(VectorSource(c(text1, text2)))
words <- c("activat", "affect", "affected")

dtm <- DocumentTermMatrix(text)
data.frame(cnt = colSums(as.matrix(dtm[ , words])))

输出

         cnt
activat    1
affect     1
affected   1
于 2016-01-30T13:50:54.880 回答