r - quanteda::dfm_lookup(): 捕获找到的词
问题描述
我想在字典上执行惊人quanteda
的 'sdfm_lookup()
但也检索匹配项。
考虑以下示例:
dict_ex <- dictionary(list(christmas = c("Christmas", "Santa", "holiday"),
opposition = c("Opposition", "reject", "notincorpus"),
taxglob = "tax*",
taxregex = "tax.+$",
country = c("United_States", "Sweden")))
dfmat_ex <- dfm(tokens(c("My Christmas was ruined by your opposition tax plan.",
"Does the United_States or Sweden have more progressive taxation?")),
remove = stopwords("english"))
dfmat_ex
dfm_lookup(dfmat_ex, dict_ex)
这给了我:
Document-feature matrix of: 2 documents, 5 features (50.00% sparse) and 0 docvars.
features
docs christmas opposition taxglob taxregex country
text1 1 1 1 0 0
text2 0 0 1 0 2
但是,由于每个字典工具也有多个条目,我想知道哪个令牌产生了匹配项。(我真正的字典相当长,所以这个例子可能看起来微不足道,但对于真正的用例,它不是。)
我想达到这样的结果:
Document-feature matrix of: 2 documents, 5 features (50.00% sparse) and 0 docvars.
features
docs christmas christmas.match opposition opposition.match taxglob taxglob.match taxregex taxreg.match country country.match
text1 1 Christmas 1 Opposition 1 tax 0 NA 0 NA
text2 0 NA 0 NA 1 taxation 0 NA 2 United_States, Sweden
有人可以帮我弄这个吗?提前谢谢了!:)
解决方案
这实际上是不可能的,原因有两个。
首先,矩阵(类)对象(dfm 或其他)不能混合元素模式,这里是计数和字符值的混合。使用 data.frame 可以做到这一点,但是您会失去稀疏性的优势,在这里,您将拥有 anx 2*V(其中 V = 特征数)data.frame 尺寸。
其次,“christmas.match”可能有多个特征/标记匹配它,因此字符值需要一个列表,从而进一步限制对象类。
更好的方法是使用kwic()
将标记与字典形成的模式进行匹配。您可以通过将字典提供为 来对键执行此操作pattern()
,或者取消列出字典以获取每个值的匹配项。
library("quanteda")
## Package version: 3.1
## Unicode version: 13.0
## ICU version: 69.1
## Parallel computing: 12 of 12 threads used.
## See https://quanteda.io for tutorials and examples.
dict <- dictionary(list(one = c("a*", "b"), two = c("e", "f")))
toks <- tokens(c(d1 = "a b c d e f g and another"))
# where the dictionary keys are the patterns matched
kwic(toks, dict) %>%
as.data.frame()
## docname from to pre keyword post pattern
## 1 d1 1 1 a b c d e f one
## 2 d1 2 2 a b c d e f g one
## 3 d1 5 5 a b c d e f g and another two
## 4 d1 6 6 a b c d e f g and another two
## 5 d1 8 8 c d e f g and another one
## 6 d1 9 9 d e f g and another one
# where the dictionary values are the patterns matched
kwic(toks, unlist(dict)) %>%
as.data.frame()
## docname from to pre keyword post pattern
## 1 d1 1 1 a b c d e f a*
## 2 d1 2 2 a b c d e f g b
## 3 d1 5 5 a b c d e f g and another e
## 4 d1 6 6 a b c d e f g and another f
## 5 d1 8 8 c d e f g and another a*
## 6 d1 9 9 d e f g and another a*
推荐阅读
- arrays - 插入多个元素后恢复堆属性
- python - python - 如何在Python列表的子列表中查找列表中最小元素的索引?
- java - 为什么在反复使用扫描仪直到用户输入合法值后出现“变量可能尚未初始化”错误?
- rest - Apache Ignite Rest API 调用。(关于 Apache Ignite SDK 的任何想法?)
- c++ - 如何在帕斯卡三角形中积分一行?
- javascript - 如何匹配球体上特定点的纹理
- python - sqlite 事务未提交
- python - 将 SymPy 表达式与不涉及 x 的复杂因子集成
- amazon-web-services - 关闭账户后是否会因未停止 EC2 实例而收费?
- c++ - 是编译之间的 C 函数符号常量的地址