Quanteda

编程入门 行业动态 更新时间:2024-10-15 08:22:46
本文介绍了Quanteda - 提取已识别的字典单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试从 Quanteda dfm 中提取已识别的字典单词,但一直无法找到解决方案.

I am trying to extract the identified dictionary words from a Quanteda dfm, but have been unable to find a solution.

有人对此有解决方案吗?

Does someone have a solution for this?

样本输入:

dict <- dictionary(list(season = c("spring", "summer", "fall", "winter")))
dfm  <- dfm("summer is great", dictionary  = dict)

输出:

 > dfm
 Document-feature matrix of: 1 document, 1 feature.
 1 x 1 sparse Matrix of class "dfmSparse"

   features
docs    season
text1      1

我现在知道在句子中确定了一个季节性字典词,但我也想知道它是哪个词.

I now know that a seasonality dict word has been identified in the sentence, but I would also like to know which word it was.

最好以表格格式提取:

docs    dict     dictWord
text1   season   summer

推荐答案

您可以使用 keptFeatures 参数创建第二个 dfm,然后 cbind() 将其添加到首先,字典dfm.

You can create a second dfm using the keptFeatures argument, and then cbind() it to the first, dictionaried dfm.

dict <- dictionary(list(season = c("spring", "summer", "fall", "winter")))
txt <- "summer is great"
season_dfm  <- dfm(txt, dictionary  = dict, verbose = FALSE)
dict_dfm <- dfm(txt, select = dict, verbose = FALSE)

cbind(season_dfm, dict_dfm)
## Document-feature matrix of: 1 document, 2 features.
## 1 x 2 sparse Matrix of class "dfmSparse"
##       season summer
## text1      1      1

如果您希望输出为表格,则为:

If you want the output as a table, it would be:

dict_df <- as.data.frame(combined_dfm)
names(dict_df)[2] <- "dictWord"
dict_df
##       season dictWord
## text1      1        1

但这只适用于每个文本只有一个字典值的情况——否则 dict_dfm 将有多个特征列.

but that only works if you have a single dictionary value per text -- otherwise the dict_dfm will have multiple feature columns.

这篇关于Quanteda - 提取已识别的字典单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 19:49:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1398896.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Quanteda

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!