【问题标题】:Manipulate (rename and recombine) features in a dfm (quanteda)在 dfm (quanteda) 中操作(重命名和重新组合)特征
【发布时间】:2017-08-15 06:26:20
【问题描述】:

我想在 dfm 中操作(重命名和组合)功能,如何进行?

原因如下:我想使用与 Quanteda 中实现的 Porter 词干分析器不同的词干提取算法(通过 Python 调用的 kpss 算法)。

示例 三词句子 c("creatief creatieve creatie") 将产生一个具有三个特征(即“creatief”、“creatieve”、“creatie”)的 dfm,它们的词频均为 1。然而,kpss 算法将这些词变成“creatie”。如果我可以将 dfm 中的这三个特征组合成一个名为“creatie”的特征,它的词频为 3,那将非常方便。

非常感谢您的帮助。

(注意。我知道在将 dfm 转换为“简单”矩阵后可以进行此类数据操作,但我想在 dfm 中执行此操作)。

附录 我忽略了 dfm_compress 函数。我快到了......在我压缩了dfm之后,是否也可以应用字典,例如“creati”和“innovati”这两个词都应该算作“creati”这个词类别的出现(参见dfm中的字典函数)? (注意。鉴于 txt 文件的数量庞大,我宁愿不保留原始数据文件)

【问题讨论】:

  • 亲爱的肯,感谢您的回复。我忽略了 dfm_compress 函数。我快到了......在我压缩了dfm之后,是否也可以应用字典,例如“creati”和“innovati”这两个词都应该算作“creati”这个词类别的出现(参见dfm中的字典函数)? (注意。考虑到大量的 txt 文件,我宁愿不保留原始数据文件)。

标签: r quanteda


【解决方案1】:

您可以通过创建一个 dfm 然后对特征进行词干化,然后重新编译 dfm 以组合在词干化后相同的特征。

require(quanteda)
txt <- c("creatief creatieve creatie")

(dfm1 <- dfm(txt))
## Document-feature matrix of: 1 document, 3 features (0% sparse).
## 1 x 3 sparse Matrix of class "dfmSparse"
##        features
## docs    creatief creatieve creatie
##   text1        1         1       1

这是我为您的示例近似的一个步骤,但您可以用您自己对特征字符向量的词干提取操作替换下面的右侧字符串子集函数。

# this approximates what you can do with the Python-based stemmer
# note that here you must use colnames<- since there is no function
# featnames<- (for replacement)
colnames(dfm1) <- stringi::stri_sub(featnames(dfm1), 1, 7)
dfm1
## Document-feature matrix of: 1 document, 3 features (0% sparse).
## 1 x 3 sparse Matrix of class "dfmSparse"
##        features
## docs    creatie creatie creatie
##   text1       1       1       1

然后你可以重新编译dfm来编译计数。

# this combines counts in featnames that are identical
dfm_compress(dfm1)
## Document-feature matrix of: 1 document, 1 feature (0% sparse).
## 1 x 1 sparse Matrix of class "dfmSparse"
##        features
## docs    creatie
##   text1       3

请注意,如果您使用 quanteda 的词干分析器,此步骤可能是 dfm_wordstem():

dfm_wordstem(dfm1)
## Document-feature matrix of: 1 document, 1 feature (0% sparse).
## 1 x 1 sparse Matrix of class "dfmSparse"
##        features
## docs    creati
##   text1      3

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    相关资源
    最近更新 更多