-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSentencesParserTask.cs
72 lines (65 loc) · 2.36 KB
/
SentencesParserTask.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace TextAnalysis
{
static class SentencesParserTask
{
public static List<List<string>> ParseSentences(string text)
{
var sentencesList = new List<List<string>>();
var wordList = new List<string>();
var separators = new char[] {'.', '!', '?', ';', ':', '(', ')'};
var builder = new StringBuilder();
for (var i = 0; i < text.Length; i++)
{
if (!char.IsLetter(text[i]))
{
if (text[i] == '\'')
{
builder.Append(text[i]);
continue;
}
if (separators.Contains(text[i]))
{
if (builder.ToString().Length != 0)
{
wordList.Add(builder.ToString().ToLower());
builder.Clear();
}
AddWordListToSentenceList(wordList, sentencesList);
continue;
}
if (i == text.Length - 1)
{
wordList.Add(builder.ToString().ToLower());
AddWordListToSentenceList(wordList, sentencesList);
continue;
}
if (builder.ToString().Length != 0)
{
wordList.Add(builder.ToString().ToLower());
builder.Clear();
}
}
else
{
if (i == text.Length - 1)
{
builder.Append(text[i]);
wordList.Add(builder.ToString().ToLower());
AddWordListToSentenceList(wordList, sentencesList);
}
builder.Append(text[i]);
}
}
return sentencesList;
}
private static void AddWordListToSentenceList(List<string> wordList, List<List<string>> sentencesList)
{
if (wordList.Count != 0)
sentencesList.Add(wordList.Where(word => word.Length != 0).ToList());
wordList.Clear();
}
}
}