-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentencesParserTask.cs
More file actions
51 lines (48 loc) · 1.51 KB
/
SentencesParserTask.cs
File metadata and controls
51 lines (48 loc) · 1.51 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TextAnalysis
{
static class SentencesParserTask
{
public static List<List<string>> ParseSentences(string text)
{
var sentencesList = new List<List<string>>();
var sentences = text.ToLower().Split(new[] {'.', '!', '?', ';', ':', '(', ')'},
StringSplitOptions.RemoveEmptyEntries);
if (sentences.Length == 0 && text.Length != 0)
{
var words = GetWords(text);
if(words!=null)
sentencesList.Add(words);
}
else
sentencesList.AddRange(sentences.Select(sentence => GetWords(sentence)).Where(words => words != null));
return sentencesList;
}
private static List<string> GetWords(string sentence)
{
var words = new List<string>();
var sb = new StringBuilder();
foreach (var c in sentence)
{
if (char.IsLetter(c) || c == '\'')
{
sb.Append(c);
}
else
{
if (sb.Length > 0)
{
words.Add(sb.ToString());
sb.Clear();
}
}
}
if(sb.Length > 0)
words.Add(sb.ToString());
return words.Count>0 ? words:null;
}
}
}