2017-11-21 11 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. (?<=^|\.)이 입력의 시작이 될 OR 기간에 선행하는 당신에-수-일치를 필요로하는 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

내가 문장의 몇 가지로 분할 한 후 하나의 고체 문자열로 별도의 라인을 추가하고 노력했다.

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); 

(참고 당신은 작은 파일을 처리하는 것을 알고 있다면 당신은이 모든 필요하지 않습니다 그리고 당신은 이전 제안 괜찮을 것)