2

我正在尝试从字符串创建字母的 dfm。当 dfm 无法选择可以为“/”“-”“”等标点符号创建功能时,我遇到了问题。或者 '。

require(quanteda)
dict = c('a','b','c','d','e','f','/',".",'-',"'")
dict <- quanteda::dictionary(sapply(dict, list))

x<-c("cab","baa", "a/de-d/f","ad")
x<-sapply(x, function(x) strsplit(x,"")[[1]])
x<-sapply(x, function(x) paste(x, collapse = " "))

mat <- dfm(x, dictionary = dict, valuetype = "regex")
mat <- as.matrix(mat)
mat
  1. 对于“a/de-d/f”,我也想捕获字母“/”“-”
  2. 为什么是“。” 充当 rowsum 的特征。如何将其保留为单独的功能?
4

1 回答 1

0

问题(正如@lukeA 在评论中指出的那样)是您valuetype使用了错误的模式匹配。您正在使用正则表达式,其中.代表任何字符,因此这里得到一个总数(您称之为 rowsum)。

让我们先看一下x,它将在空格上被标记化dfm(),这样每个字符就变成了一个标记。

x
#        cab               baa          a/de-d/f                ad 
#    "c a b"           "b a a" "a / d e - d / f"             "a d" 

要首先回答 (2),您将通过“正则表达式”匹配获得以下信息:

dfm(x, dictionary = dict, valuetype = "regex", verbose = FALSE)
## Document-feature matrix of: 4 documents, 10 features.
## 4 x 10 sparse Matrix of class "dfmSparse"
##           features
## docs       a b c d e f / . - '
##   cab      1 1 1 0 0 0 0 3 0 0
##   baa      2 1 0 0 0 0 0 3 0 0
##   a/de-d/f 1 0 0 2 1 1 0 5 0 0
##   ad       1 0 0 1 0 0 0 2 0 0

这很接近,但没有回答(1)。为了解决这个问题,您需要更改默认标记化行为,dfm()以便它不会删除标点符号。

dfm(x, dictionary = dict, valuetype = "fixed", removePunct = FALSE, verbose = FALSE)
## Document-feature matrix of: 4 documents, 10 features.
## 4 x 10 sparse Matrix of class "dfmSparse"
##           features
## docs       a b c d e f / . - '
##   cab      1 1 1 0 0 0 0 0 0 0
##   baa      2 1 0 0 0 0 0 0 0 0
##   a/de-d/f 1 0 0 2 1 1 2 0 1 0
##   ad       1 0 0 1 0 0 0 0 0 0

现在/-正在被计算在内。和仍然作为特征存在.'因为它们是字典键,但每个文档的计数为零。

于 2016-11-20T14:51:35.403 回答