根据文本文件的内容对语料库进行子集化

Subsetting a corpus based on content of textfile

我正在使用 R 和 tm 包进行一些文本分析。 我正在尝试根据是否在各个文本文件的内容中找到某个表达式来构建语料库的子集。

我创建了一个包含 20 个文本文件的语料库(感谢 lukeA 提供的这个示例):

reut21578 <- system.file("texts", "crude", package = "tm")
corp <- VCorpus(DirSource(reut21578), list(reader = readReut21578XMLasPlain))

我现在只想 select 那些包含字符串 "price reduction" 的文本文件来创建子集语料库。

检查文档的第一个文本文件,我知道至少有一个文本文件包含该字符串:

writeLines(as.character(corp[1]))

我最好怎么做?

这是使用 tm_filter 的一种方法:

library(tm)
reut21578 <- system.file("texts", "crude", package = "tm")
corp <- VCorpus(DirSource(reut21578), list(reader = readReut21578XMLasPlain))

( corp_sub <- tm_filter(corp, function(x) any(grep("price reduction", content(x), fixed=TRUE))) )
# <<VCorpus>>
# Metadata:  corpus specific: 0, document level (indexed): 0
# Content:  documents: 1

cat(content(corp_sub[[1]]))
# Diamond Shamrock Corp said that
# effective today it had cut its contract prices for crude oil by
# 1.50 dlrs a barrel.
#     The reduction brings its posted price for West Texas
# Intermediate to 16.00 dlrs a barrel, the copany said.
#     "The price reduction today was made in the light of falling   # <=====
# oil product prices and a weak crude oil market," a company
# spokeswoman said.
#     Diamond is the latest in a line of U.S. oil companies that
# have cut its contract, or posted, prices over the last two days
# citing weak oil markets.
#  Reuter

我是怎么到那里的?通过查看 packages' vignette,搜索 子集 ,然后查看 tm_filter 的示例(帮助:?tm_filter),其中提到了.也可能值得查看 ?grep 来检查模式匹配的选项。

@lukeA 的解决方案有效。我想给出另一个我更喜欢的解决方案。

    library(tm)

        reut21578 <- system.file("texts", "crude", package = "tm")
        corp <- VCorpus(DirSource(reut21578), list(reader = readReut21578XMLasPlain))

        corpTF <- lapply(corp, function(x) any(grep("price reduction", content(x), fixed=TRUE)))

        for(i in 1:length(corp)) 
          corp[[i]]$meta["mySubset"] <- corpTF[i]

        idx <- meta(corp, tag ="mySubset") == 'TRUE'
        filtered <- corp[idx]

        cat(content(filtered[[1]]))

这个解决方案的优点是使用meta标签,我们可以看到所有带有选择标签mySubset,值'TRUE'[=18的语料库元素=] 对于我们选择的那些,对于其他值 'FALSE'

这是使用 quanteda 包的一种更简单的方法,并且更符合重用已为其他 R 对象定义的现有方法的方法。 quanteda 有一个用于语料库对象的 subset 方法,它的工作方式与 data.frame 的子集方法一样,但在逻辑向量上进行选择,包括在语料库中定义的文档变量。下面,我使用语料库对象的 texts() 方法从语料库中提取文本,并在 grep() 中使用它来搜索你的词对。

require(tm)
data(crude)

require(quanteda)
# corpus constructor recognises tm Corpus objects 
(qcorpus <- corpus(crude))
## Corpus consisting of 20 documents.
# use subset method
(qcorpussub <- corpus_subset(qcorpus, grepl("price\s+reduction", texts(qcorpus))))
## Corpus consisting of 1 document.

# see the context
## kwic(qcorpus, "price reduction")
##                       contextPre         keyword             contextPost
## [127, 45:46] copany said." The [ price reduction ] today was made in the

注意:我 space 将你的正则表达式设置为“\s+”,因为你可以有一些 spaces、制表符或换行符的变体,而不仅仅是一个 space。