Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System;
  3.  
  4. namespace TextAnalysis
  5. {
  6. static class SentencesParserTask
  7. {
  8. public static List<List<string>> ParseSentences(string text)
  9. {
  10. var sentencesList = new List<List<string>>();
  11.  
  12. List<string> sentences = new List<string>();
  13. FillSentences(sentences,text);
  14.  
  15. for (int i = 0; i < sentences.Count; i++)
  16. {
  17. sentences[i] = sentences[i].Trim();
  18. List<string> words = new List<string>();
  19. DivideByWords(words,sentences[i]);
  20.  
  21. if (words.Count > 0)
  22. sentencesList.Add(words);
  23. }
  24.  
  25. return sentencesList;
  26. }
  27.  
  28. public static void DivideByWords(List<string> words, string sentence)
  29. {
  30. string wordTemp = "";
  31. for (int j = 0; j < sentence.Length; j++)//foreach simbol
  32. {
  33. if (char.IsLetter(sentence[j]) || sentence[j] == '\'')
  34. {
  35. wordTemp += sentence[j];
  36. }
  37. else
  38. {
  39. if (wordTemp != "")
  40. {
  41. words.Add(wordTemp.ToLower());
  42. wordTemp = "";
  43. }
  44. }
  45. }
  46. if (wordTemp != "")
  47. {
  48. words.Add(wordTemp.ToLower());
  49. }
  50. }
  51.  
  52. public static void FillSentences(List<string> sentences, string text)
  53. {
  54. string part = "";
  55. string separators = ".!?;:()";
  56. for (int i = 0; i < text.Length; i++)
  57. {
  58. if (separators.IndexOf(text[i]) == -1)
  59. {
  60. part += text[i];
  61. }
  62. else
  63. {
  64. if (part != "")
  65. {
  66. sentences.Add(part.ToLower());
  67. part = "";
  68. }
  69. }
  70. }
  71. if (part != "")
  72. {
  73. sentences.Add(part.ToLower());
  74. part = "";
  75. }
  76. }
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement