2017-11-21 16 views
-2

ファイル内のテキストを読み込んで作業しようとしています。あなたは文章が行くことができる見ることができるように文章を複数の行にまたがる文字列に分割する

I went to a shop. I bought a pack of sausages 

and some milk. Sadly I forgot about the potatoes. I'm on my way 

to the store 

to buy potatoes. 

:問題は、私は文章に分割する必要があり、それを行うための方法を考えることはできませんが...ここで

は、テキストファイルの例です複数の行にまたがって終了します。私は正規表現を使うべきだと思っていますが、それを行う方法を考えることはできません。

+3

あなたが求めているものを決定することは本当に難しいですが、私が思っているのは、文字列からすべての改行文字を取り除き、必要なすべての句読点でその文字列を分割することです( '.'、'? '、'! ''など) – maccettura

答えて

0

ここでは、文をピリオドで区切られた入力の空でないセクションとして定義します。

はたぶんこれの線に沿って何か:

(?<=^|\.)(.+?)(\.|$) 

キーは.が(代わりに\ nを除く任意の文字の)任意の文字に一致しますので、あなたがRegexOptions.Singlelineオプションを使用する必要があることはおそらくです。より詳細に上記のパターンの

説明:

  1. (?<=^|\.)があなたの入力の開始時であるか、または期間が先行するためにあなたに被マッチを必要とZero-Width Positive Lookbehind Assertionです。マッチした期間自体はマッチの一部ではありません。
  2. (.+?)はあなたの文章の内容です。 +?演算子はlazyと呼ばれ、入力の短い部分として可能な限り一致しようとします。これは、次のパターン部分から次の文章までの期間や次の文章を確実に取得しないようにするために必要です。
  3. (\.|$)は、入力の最後に文の終端記号と一致します。

全作業例:

Regex r = new Regex(@"(?<=^|\.)(.+?)(\.|$)", RegexOptions.Singleline); 
String input = @"I went to a shop. I bought a pack of sausages 
and some milk. Sadly I forgot about the potatoes. I'm on my way 
to the store 
to buy potatoes."; 
foreach (var match in r.Matches(input)) 
{ 
    string sentence = match.ToString(); 
} 
0

私は1つの固形の文字列に別々の行を追加し、文章のカップルにそれを分割しようとしました。

static void Sakiniai (string fv, string skyrikliai) 
    { 
     char[] skyrikliaiSak = { '.', '!', '?' }; 

     string[] lines = File.ReadAllLines(fv, Encoding.GetEncoding(1257)); 
     string naujas = ""; 

     foreach (string line in lines) 
     { 
      naujas += line; 
      naujas += " "; 
     } 

     string[] sakiniai = naujas.Split(skyrikliaiSak); 
     for(int i = 0; i < sakiniai.Length; i++) 
     { 
      Console.WriteLine(sakiniai[i]); 
     } 

    } 

を教えてくださいこれを行うには良い方法があります:

これは私が使用しようとした方法があります。

+0

最初に、' string naujas = File.ReadAllLines(fv、Encoding.GetEncoding(1257))。Join( "");また、あなたはどんな点でより良いと思いますか? – NetMage

0

@maccetturaがコメントしたように、これと似たようなことを試すことができます。

string text = "..."; 
text = text.Replace(System.Environment.NewLine, " ").Replace(" ", " "); 
     var sentences = text.Split(new char[] { '.', '!', '?' }); 
     foreach(string s in sentences) 
     { 
      Console.WriteLine(s); 
     } 
0

文章の長さがわからないので、文章で文章を書いてください。

このような何か:

 char[] periods = {'.', '!', '?'}; // or any other separator you may like 

     string  line  = ""; 
     string  sentence = ""; 

     using (StreamReader reader = new StreamReader ("filename.txt")) 
     { 
      while ((line = reader.ReadLine()) != null) 
      { 
       if (line.IndexOfAny(periods)<0) 
       { 
        sentence += " " + line.Trim(); // increment sentence if there are no periods 

        // do whatever you want with the sentence 
        if (string.IsNullOrEmpty (sentence)) 
         process(sentence); 

        continue; 
       } 

       // I'm using StringSplitOptions.None here so we handle lines ending with a period right 
       string[] sentences = line.Split(periods, StringSplitOptions.None); 

       for (int i = 0; i < sentences.Length; i++) 
       { 
        sentence += " " + line.Trim(); // increment sentence if there are no periods 

        // do whatever you want with the sentence 
        if (string.IsNullOrEmpty(sentence)) 
         process(sentence); 

        // we don't want to clean on the last piece of sentence as it will continue on the next line 
        if (i < sentences.Length - 1) 
        { 
         sentence = ""; // clean for next sentence 
        } 
       } 

      } 

      // this step is only required if you might have the last line sentence ending without a period 
      // do whatever you want with the sentence 
      if (string.IsNullOrEmpty(sentence)) 
       process(sentence); 

(あなたが唯一の小さなファイルを処理していることがわかっている場合は、このすべてを必要としないし、前の提案といいと思いますのでご注意ください)

関連する問題