2017-10-29 11 views
1

目的:私は、単語normalを含むrのさまざまな文で副節を取り除こうとしています。サブクラスは、開始カンマで区切られ、フルストップまたはコンマで終わるものとして定義されます。私はその節を取り除きたい。特定の単語で副節を削除する方法

入力文

I walked down the hill, which was normal, but I also walked up another hill which was dull. 

I looked at him and although he looked normal, he was not normal. 

I am fine, but he is not normal, and she is fine and she is normal, but I think her brother is not normal. 

所望の出力

I walked down the hill but I also walked up another hill which was dull 

I looked at him and although he looked normal. 

I am fine, and she is fine and she is normal. 

試み

gsub(", .*normal.*?(\\.|,|$)\\R*", "", input_string, perl = T, ignore.case = T) 

電流出力:

I walked down the hill. 
I looked at him and although he looked normal. 
I am fine. 

それが最初のカンマからすべてを削除しかし、多くの副次句がある場合、これは主に意図された出力を与えるものではありません。どうすれば最も近いカンマから「普通」に一致させることができますか?

+1

私は元気ですが、彼は正常ではない、と彼女は結構です、彼女は正常ですが、私は思う」の4つのサブ句があります。彼女の兄弟は普通ではありません "とあなたの希望する出力は2番目と4番目を削除します。これはあなたの説明でうまく説明されていません。削除したいサブクラートを明確にしてください。毎2日?そのような出力は本当に正しいですか? – janos

+0

'gsub("、[^、* * \\ bnormal \\ b [^、] * ,? "、input_string、ignore.case = T)' –

答えて

0

あなたの例とルールは一貫していません(@janosのコメントを参照)。たとえば、最後の例文の最後のサブ句を削除しますが、期間が終了していなくても「兄弟は普通ではないと思います。さておき、次はあなたが始める必要がある

ss <- c(
    "I walked down the hill, which was normal, but I also walked up another hill which was dull", 
    "I looked at him and although he looked normal, he was not normal.", 
    "I am fine, but he is not normal, and she is fine and she is normal, but I think her brother is not normal"); 

lapply(ss, function(x) gsub("\\,[a-zA-Z0-9_ ]+[\\,\\.]{1}", "", x)); 
#[[1]] 
#[1] "I walked down the hill but I also walked up another hill which was dull" 

#[[2]] 
#[1] "I looked at him and although he looked normal" 

#[[3]] 
#[1] "I am fine and she is fine and she is normal, but I think her brother is not normal" 
+0

を試してみて、ピリオドのある文章を入力すると、目的の出力が得られます。 –

関連する問題