r - 在多个文档中查找多字串
问题描述
为了在文档中查找常用术语或短语,可以使用 tf.
如果我们知道文本中有一些特定的表达但我们不知道长度或者是否包含任何其他信息,有什么方法可以找到它们?例子:
df <- data.frame(text = c("Introduction Here you see something Related work another info here", "Introduction another text Background work something to now"))
假设这些词是 Introducton、Related work 和 Background work,但我们不知道是哪些词组。我们怎样才能找到它们?
解决方案
在这里,您需要一种检测搭配的方法,幸运的是 quanteda具有textstat_collocations()
. 一旦你检测到这些,你可以将你的令牌组合成一个单一的“令牌”,然后以标准方式获得它们的频率。
您不需要提前知道长度,但需要指定范围。下面,我添加了更多文本,并包含从 2 到 3 的大小范围。这也包含了“犯罪背景调查”,而不会混淆“背景工作”一词中的“背景”一词。(默认情况下,检测不区分大小写。)
library("quanteda")
## Package version: 2.1.0
text <- c(
"Introduction Here you see something Related work another info here",
"Introduction another text Background work something to now",
"Background work is related to related work",
"criminal background checks are useful",
"The law requires criminal background checks"
)
colls <- textstat_collocations(text, size = 2:3)
colls
## collocation count count_nested length lambda z
## 1 criminal background 2 2 2 4.553877 2.5856967
## 2 background checks 2 2 2 4.007333 2.3794386
## 3 related work 2 2 2 2.871680 2.3412833
## 4 background work 2 2 2 2.322388 2.0862256
## 5 criminal background checks 2 0 3 -1.142097 -0.3426584
在这里,我们可以看到正在检测和区分这些短语。现在我们可以使用 tokens_compound 来加入它们:
toks <- tokens(text) %>%
tokens_compound(colls, concatenator = " ")
dfm(toks) %>%
dfm_trim(min_termfreq = 2) %>%
dfm_remove(stopwords("en")) %>%
textstat_frequency()
## feature frequency rank docfreq group
## 1 introduction 2 1 2 all
## 2 something 2 1 2 all
## 3 another 2 1 2 all
## 4 related work 2 1 2 all
## 5 background work 2 1 2 all
## 6 criminal background checks 2 1 2 all
推荐阅读
- c# - 是否有统一方法将数组值向下或向上缩放到给定范围
- angular - angular-jwt 令牌未在我的 http 请求标头中发送
- oracle - 如何在 sql 中解决“遇到以下情况之一时遇到符号“文件结尾”:”
- python - 我们可以创建一个可点击的 python 字符串来将用户重定向到一个 url 吗?
- matlab - 如何获得 FFT 的波数?
- java - 为什么没有显示我的最终 JOptionPane 消息?
- python - urllib.error.HTTPError:HTTP 错误 400:错误请求
- mybatis - 在 MyBatis 中执行更新时有什么方法可以返回主键或实体
- javascript - 节点 --experimental-modules - 错误:找不到模块
- wordpress - 向导 WordPress 主题