【发布时间】:2021-11-12 15:50:58
【问题描述】:
我想在字典上执行惊人的 quanteda 的 dfm_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
有人可以帮我解决这个问题吗?提前谢谢了! :)
【问题讨论】: