r - 如何在 quanteda 令牌对象中找到令牌的位置?
问题描述
我从纯文本文件创建了一个 quanteda 令牌对象,并选择了我寻求使用的特定单词
tokens_select(truePdfAnnualReports.toks, unlist(strategicKeywords.list), padding = TRUE)
维护在原始文本文件中找到的特定标记序列。我现在希望将标记位置编号(绝对和相对)分配给函数选择的标记。如何为函数选择的令牌分配位置编号?
解决方案
你想要kwic()
,没有tokens_select()
。我使用下面的内置创建了一个可重现的示例答案data_corpus_inaugural
。
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.
toks <- tokens(tail(data_corpus_inaugural, 10))
keywords <- c("nuclear", "security")
# form a data.frame from kwic() results
kw <- kwic(toks, keywords, window = 0) %>%
as.data.frame()
# for illustration
kw[10:14, ]
## docname from to pre keyword post pattern
## 10 1985-Reagan 2385 2385 security security
## 11 1989-Bush 2149 2149 security security
## 12 1997-Clinton 259 259 security security
## 13 1997-Clinton 1660 1660 nuclear nuclear
## 14 2001-Bush 872 872 Security security
现在,为了获得相对位置,我们可以在获得总令牌长度并除以之后做一点dplyr魔术:
doc_lengths <- data.frame(
docname = docnames(toks),
toklength = ntoken(toks)
)
# the answer
answer <- dplyr::left_join(kw, doc_lengths) %>%
dplyr::mutate(
from_relative = from / toklength,
to_relative = to / toklength
)
## Joining, by = "docname"
head(answer)
## docname from to pre keyword post pattern toklength from_relative
## 1 1985-Reagan 2005 2005 security security 2909 0.6892403
## 2 1985-Reagan 2152 2152 security security 2909 0.7397731
## 3 1985-Reagan 2189 2189 nuclear nuclear 2909 0.7524923
## 4 1985-Reagan 2210 2210 nuclear nuclear 2909 0.7597112
## 5 1985-Reagan 2245 2245 nuclear nuclear 2909 0.7717429
## 6 1985-Reagan 2310 2310 security security 2909 0.7940873
## to_relative
## 1 0.6892403
## 2 0.7397731
## 3 0.7524923
## 4 0.7597112
## 5 0.7717429
## 6 0.7940873
推荐阅读
- javascript - 交换css字体时是否会发出js事件?
- mysql - (SQL) unique(set) 返回真还是假?
- r - R中是否存在将连续变量分类为每组相等数量的观察值的函数?
- python - Siamese Network-Train 自己的数据集并判断过拟合
- r - R:将所有组分成两半(dplyr)
- javascript - 无法读取未定义的 hbs 辅助函数的属性
- google-cloud-platform - 作业图太大。数据流问题
- r - ggplot2:为什么没有绘制并排的箱线图?
- javascript - javascript块背后的魔力是什么?
- java - Java:JFrame图像仅在最大化窗口时出现