簡體   English   中英

在多個文檔中查找多字串

[英]Find multi-word strings in more than one document

為了在文檔中查找常用術語或短語,可以使用 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,但我們不知道是哪些詞組。 我們怎樣才能找到它們?

在這里,您需要一種檢測搭配的方法,幸運的是quantedatextstat_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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM