Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace TextAnalysis
  5. {
  6.     static class SentencesParserTask
  7.     {
  8.         public static List<string> SplitSentenceIntoWords(string sentence)
  9.         {
  10.             var words = sentence.Split(' ', '^');
  11.             var wordList = new List<String>();
  12.             foreach (string word in words)
  13.             {
  14.                 var clearWord = word;
  15.                 bool wordIsClear = false;
  16.                 for (var i = 0; i < clearWord.Length; i++)
  17.                 {
  18.                     if (!char.IsLetter(clearWord[i]) && clearWord[i] != '\'')
  19.                     {
  20.                         clearWord = clearWord.Remove(i, 1);
  21.                         i--;
  22.                         while (!char.IsLetter(clearWord[0]))
  23.                         {
  24.                             clearWord = clearWord.Remove(0, 1);
  25.                         }
  26.                         wordList.Add(clearWord.ToLower());
  27.                         wordIsClear = true;
  28.                     }
  29.                 }
  30.                 if (!wordIsClear)
  31.                 {
  32.                     wordList.Add(word);
  33.                 }
  34.             }
  35.             return wordList;
  36.         }
  37.         public static List<List<string>> ParseSentences(string text)
  38.         {
  39.             var sentencesList = new List<List<string>>();
  40.             var sentences = text.Split('.', '!', '?', ';', ':', '(', ')');
  41.             foreach (string sentence in sentences)
  42.             {
  43.                 if (!sentence.Equals(""))
  44.                 sentencesList.Add(SplitSentenceIntoWords(sentence));
  45.             }
  46.             return sentencesList;
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement