如何使用 quanteda 查找句子中某些单词出现在某些其他单词之前的实例
How to use quanteda to find instances of appearance of certain words before certain others in a sentence
作为 R 新手,通过使用 quanteda,我试图找到某个单词在句子中的某个位置依次出现在另一个特定单词之前的实例。更具体地说,我正在寻找“投资者”一词位于语料库中“应”一词之前某处的实例,该语料库由摩洛哥和尼日利亚之间缔结的国际条约组成(文本可在此处找到:https://edit.wti.org/app.php/document/show/bde2bcf4-e20b-4d05-a3f1-5b9eb86d3b3b).
问题是有时这两个词之间有多个词。例如,有时写成“investors and investments shall”。我尝试应用此网站上提供的类似解决方案。当我在 () 和 运行 上尝试解决方案时,以下代码:
kwic(corpus_mar_nga, phrase("investors * shall"))
我得到 0 个观察结果,因为这只计算“投资者”和“应”之间只有一个词的情况。
当我遵循 (Is it possible to use `kwic` function to find words near to each other?) 和 运行 上提供的另一个解决方案时,以下代码:
toks <- tokens(corpus_mar_nga)
toks_investors <- tokens_select(toks, "investors", window = 10)
kwic(toks_investors, "shall")
我遇到过“投资者”也出现在“应”之后的情况,这从根本上改变了上下文,因为在那种情况下,句子的主语有所不同。
最后,除了“investors shall”的实例,我还应该得到,例如“Investors, their investment and host state authorities shall”的实例,但我做不到用上面的代码做。
谁能给我解决这个问题的方法?
非常感谢!
好问题。这里有两种方法,一种依赖语料库文本上的正则表达式,第二种使用(如@Kohei_Watanabe在评论中建议的那样)using window for tokens_select()
.
首先,创建一些示例文本。
library("quanteda")
## Package version: 2.1.2
# sample text
txt <- c("The investors and their supporters shall do something.
Shall we tell the investors? Investors shall invest.
Shall someone else do something?")
现在将其改造成句子,因为您的搜索是在句子中进行的。
# reshape to sentences
corp <- txt %>%
corpus() %>%
corpus_reshape(to = "sentences")
方法一使用正则表达式。我们在“investors”之前添加一个边界(\b
),.+
表示“investors”和“shall”之间的一个或多个字符。 (这不会捕获换行符,但 corpus_reshape(x, to = "sentences")
会删除它们。)
# method 1: regular expressions
corp$flag <- stringi::stri_detect_regex(corp, "\binvestors.+shall",
case_insensitive = TRUE
)
print(corpus_subset(corp, flag == TRUE), -1, -1)
## Corpus consisting of 2 documents and 1 docvar.
## text1.1 :
## "The investors and their supporters shall do something."
##
## text1.2 :
## "Investors shall invest."
第二种方法应用 tokens_select()
和非对称 window,kwic()
。首先,我们 select 所有包含“investors”的文档(句子),但丢弃前面的标记并保留后面的所有标记。之后的1000个代币应该足够了。然后,应用 kwic()
我们保留所有上下文词但关注之后的词,根据定义必须在之后,因为第一个词是“投资者”。
# method 2: tokens_select()
toks <- tokens(corp)
tokens_select(toks, "investors", window = c(0, 1000)) %>%
kwic("shall", window = 1000)
##
## [text1.1, 5] investors and their supporters | shall | do something.
## [text1.3, 2] Investors | shall | invest.
选择取决于最适合您的需求。
作为 R 新手,通过使用 quanteda,我试图找到某个单词在句子中的某个位置依次出现在另一个特定单词之前的实例。更具体地说,我正在寻找“投资者”一词位于语料库中“应”一词之前某处的实例,该语料库由摩洛哥和尼日利亚之间缔结的国际条约组成(文本可在此处找到:https://edit.wti.org/app.php/document/show/bde2bcf4-e20b-4d05-a3f1-5b9eb86d3b3b).
问题是有时这两个词之间有多个词。例如,有时写成“investors and investments shall”。我尝试应用此网站上提供的类似解决方案。当我在 (
kwic(corpus_mar_nga, phrase("investors * shall"))
我得到 0 个观察结果,因为这只计算“投资者”和“应”之间只有一个词的情况。
当我遵循 (Is it possible to use `kwic` function to find words near to each other?) 和 运行 上提供的另一个解决方案时,以下代码:
toks <- tokens(corpus_mar_nga)
toks_investors <- tokens_select(toks, "investors", window = 10)
kwic(toks_investors, "shall")
我遇到过“投资者”也出现在“应”之后的情况,这从根本上改变了上下文,因为在那种情况下,句子的主语有所不同。
最后,除了“investors shall”的实例,我还应该得到,例如“Investors, their investment and host state authorities shall”的实例,但我做不到用上面的代码做。
谁能给我解决这个问题的方法?
非常感谢!
好问题。这里有两种方法,一种依赖语料库文本上的正则表达式,第二种使用(如@Kohei_Watanabe在评论中建议的那样)using window for tokens_select()
.
首先,创建一些示例文本。
library("quanteda")
## Package version: 2.1.2
# sample text
txt <- c("The investors and their supporters shall do something.
Shall we tell the investors? Investors shall invest.
Shall someone else do something?")
现在将其改造成句子,因为您的搜索是在句子中进行的。
# reshape to sentences
corp <- txt %>%
corpus() %>%
corpus_reshape(to = "sentences")
方法一使用正则表达式。我们在“investors”之前添加一个边界(\b
),.+
表示“investors”和“shall”之间的一个或多个字符。 (这不会捕获换行符,但 corpus_reshape(x, to = "sentences")
会删除它们。)
# method 1: regular expressions
corp$flag <- stringi::stri_detect_regex(corp, "\binvestors.+shall",
case_insensitive = TRUE
)
print(corpus_subset(corp, flag == TRUE), -1, -1)
## Corpus consisting of 2 documents and 1 docvar.
## text1.1 :
## "The investors and their supporters shall do something."
##
## text1.2 :
## "Investors shall invest."
第二种方法应用 tokens_select()
和非对称 window,kwic()
。首先,我们 select 所有包含“investors”的文档(句子),但丢弃前面的标记并保留后面的所有标记。之后的1000个代币应该足够了。然后,应用 kwic()
我们保留所有上下文词但关注之后的词,根据定义必须在之后,因为第一个词是“投资者”。
# method 2: tokens_select()
toks <- tokens(corp)
tokens_select(toks, "investors", window = c(0, 1000)) %>%
kwic("shall", window = 1000)
##
## [text1.1, 5] investors and their supporters | shall | do something.
## [text1.3, 2] Investors | shall | invest.
选择取决于最适合您的需求。