Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 = SplitToSentences(text.ToLower());
- var result = new List<List<string>>();
- foreach (var senetence in sentencesList)
- {
- var words = SplitToWords(senetence);
- if (words.Count > 0)
- {
- result.Add(SplitToWords(senetence));
- }
- }
- return result;
- }
- private static List<string> SplitToSentences(string text)
- {
- var delimiters = ".!?;:()";
- var result = new List<string>();
- var builder = new StringBuilder();
- foreach (var ch in text)
- {
- if (!delimiters.Contains(ch))
- {
- builder.Append(ch);
- }
- else if (builder.Length != 0)
- {
- result.Add(builder.ToString());
- builder = new StringBuilder();
- }
- }
- if (builder.Length != 0)
- {
- result.Add(builder.ToString());
- }
- return result;
- }
- private static List<string> SplitToWords(string text)
- {
- var result = new List<string>();
- var builder = new StringBuilder();
- foreach (var ch in text)
- {
- if (char.IsLetter(ch) || ch == '\'')
- {
- builder.Append(ch);
- }
- else if (builder.Length != 0)
- {
- result.Add(builder.ToString());
- builder = new StringBuilder();
- }
- }
- if (builder.Length != 0)
- {
- result.Add(builder.ToString());
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement