2016-03-24 17 views
0

私はテキスト解析を行うためにRとtmパッケージを使用しています。 私は、特定の式が個々のテキストファイルの内容内にあるかどうかに基づいて、コーパスのサブセットを構築しようとしています。テキストファイルの内容に基づいてコーパスをサブセット化する

私は20個のテキストファイル(この例ではあなたにlukeAに感謝)とコーパス作成します。私は今、サブセット・コーパスを作成するには、文字列「低価格化」が含まれているのみテキストファイルを選択したいと思います

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

を。

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

は、どのように私は最高これを行うに行くか:文書の最初のテキストファイルを検査

、私はその文字列を含む少なくとも1つのテキストファイルがあることを知っていますか?ここで

答えて

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を調べる価値があります。

0

@ 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]])) 

メタタグを使用して、このソリューションの利点は、我々はmySubset選択タグを持つすべてのコーパスの要素を参照することができ、値私たちの選択されたもののためにを「TRUE」、および値「false」に

+0

この追加をいただきありがとうございます。私は同意する、それは非常に便利です! – tarti

0

quantedaパッケージを使用した簡単な方法と、他のRオブジェクトに対して既に定義されている既存のメソッドを再利用する方法との一貫性があります。 quantedadata.frameのサブセットメソッドと同様に機能するコーパスオブジェクトに対してはsubsetメソッドを持ちますが、コーパスに定義されたドキュメント変数を含む論理ベクトルを選択します。以下では、コーパスオブジェクトの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 <- 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 

注:あなたは、スペース、タブ、改行だけではなく、単一の空間のいくつかのバリエーションを持っている可能性があるので、私は「\ sの+」を使って正規表現を離れました。

関連する問題