首页 > 解决方案 > quanteda (R) 中的 kwic 不能识别正则表达式模式中的多个单词

问题描述

我正在尝试识别文本中的正则表达式模式,但 kwic() 不能识别长度超过一个单词的正则表达式短语。我尝试使用phrase(),但这也不起作用。

给你举个例子:

mycorpus = corpus(bla$`TEXT` )
foo = kwic(mycorpus, pattern = "\\bno\\b", window = 10, valuetype = "regex" ) #gives 1959 obs. 
foo = kwic(mycorpus, pattern = "\\bno\\b\\s{0,5}\\w+", window = 10, valuetype = "regex" ) #gives 0 obs.
foo = kwic(mycorpus, pattern = "no\\sother", window = 10, valuetype = "regex" ) #gives 0 obs. even though it should find 3 phrases

即使文本中有多种模式需要识别。

谢谢您的帮助!

标签: rregexquanteda

解决方案


那是因为 kwic 搜索令牌,并且令牌不再包含空格。要搜索 quanteda 视为“短语”的标记序列,请将模式包装在phrase(). (另见?phrase。)

library("quanteda")
## Package version: 2.0.0

txt <- "one two three four five"

# no match
kwic(txt, "one\\stwo", valuetype = "regex", window = 1)
## kwic object with 0 rows

# match
kwic(txt, phrase("one two"), valuetype = "regex", window = 1)
##                                 
##  [text1, 1:2]  | one two | three

推荐阅读