首页 > 解决方案 > 在多个文档中查找多字串

问题描述

为了在文档中查找常用术语或短语,可以使用 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,但我们不知道是哪些词组。我们怎样才能找到它们?

标签: rquanteda

解决方案


在这里,您需要一种检测搭配的方法,幸运的是 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

推荐阅读