Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace TextAnalysis
  6. {
  7.     static class SentencesParserTask
  8.     {
  9.         public static List<List<string>> ParseSentences(string text)
  10.         {
  11.             var sentencesList = new List<List<string>>();
  12.             var sentences = ParseSentence(text);
  13.  
  14.             foreach (var sentence in sentences)
  15.             {
  16.                 if (sentence.Length != 0)
  17.                 {
  18.                     sentencesList.Add(ParseWords(sentence));
  19.                 }
  20.             }
  21.  
  22.             return sentencesList;
  23.         }
  24.  
  25.         private static List<string> ParseWords(string sentence)
  26.         {
  27.             var wordsList = new List<string>();
  28.             var sentenceBuilder = new StringBuilder();
  29.  
  30.             foreach (var symbol in sentence)
  31.             {
  32.                 if (char.IsLetter(symbol) || symbol == '\'')
  33.                 {
  34.                     sentenceBuilder.Append(char.ToLower(symbol));
  35.                 }
  36.                 else
  37.                 {
  38.                     if (sentenceBuilder.Length == 0)
  39.                     {
  40.                         continue;
  41.                     }
  42.  
  43.                     wordsList.Add(sentenceBuilder.ToString());
  44.                     sentenceBuilder.Clear();
  45.                 }
  46.             }
  47.  
  48.             return wordsList;
  49.         }
  50.  
  51.         private static List<string> ParseSentence(string text)
  52.         {
  53.             var sentenceList = new List<string>();
  54.             var sentenceArray = text.Split('.', '!', '?', ';', ':', '(', ')');
  55.  
  56.             foreach (var sentence in sentenceArray)
  57.             {
  58.                 if (!string.IsNullOrEmpty(sentence))
  59.                 {
  60.                     sentenceList.Add(sentence);
  61.                 }
  62.             }
  63.  
  64.             return sentenceList;
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement