2016-10-31 9 views
0

私は構造化されていないテキストを持っています。私はテキストマイニングタスクのコンセプトを維持するためにいくつかの単語を組み合わせたいと思います。例では、以下の文字列で、 "High_pressure"に "High pressure"を、 "Not_working"に "not_working"を、 "No_air"に "No air"を変更したいと考えています。言葉正規表現を使って単語を結合するR

c('low', 'high', 'no', 'not') 

所望の出力の

サンプルテキスト

c(" High pressure was the main problem in the machine","the system is not working right now","No air in the system") 

一覧

# [1] " High_pressure was the main problem in the machine" 
# [2] "the system is not_working right now"    
# [3] "No_air in the system"  
+0

"遅い" あなたはすべての接頭辞(ハイ、いや、ないなど)との完全なリストを持っているのですか? –

+0

(low、high、no、not) –

+0

単語を組み合わせる代わりにバイグラムとトライグラムを使用する必要があります。 – vagabond

答えて

2

まず、あなたが連結するテキストの入力や修正単語のリストを保存する:

textIn <- 
    c(" High pressure was the main problem in the machine","the system is not working right now","No air in the system") 

prefix <- c("high", "low", "no", "not") 

次に、それらの単語の後ろにスペースを含む正規表現を作成します。私たちが誤って単語を単語の終わりとして取り込まないように、私は\bを使用しています。

gsub(
    paste0("\\b(", paste(prefix, collapse = "|"),") ") 
    , "\\1_", textIn, ignore.case = TRUE 
) 

戻り

[1] " High_pressure was the main problem in the machine" 
[2] "the system is not_working right now"   
[3] "No_air in the system" 
関連する問題