public SentenceWithWordCounts AnalyzeSentence(string sentence) { string parsedSentence = sentence.Trim().TrimEnd(EndOfSentenceChars); IEnumerable<string> words = parsedSentence.Split(PunctuationChars, StringSplitOptions.RemoveEmptyEntries).Select(w => w.ToLower()); IEnumerable<WordCount> wordCounts = words.GroupBy(w => w).Select(group => new WordCount {Word = group.Key, Count = group.Count()}); SentenceWithWordCounts sentenceWithWordCounts = new SentenceWithWordCounts(sentence, wordCounts); return sentenceWithWordCounts; }
public string FormatOutput(SentenceWithWordCounts sentenceWithWordCount) { StringBuilder stringBuilder = new StringBuilder(string.Format("Input: {0}\r\nOutput:", sentenceWithWordCount.Sentence)); foreach (WordCount wordCount in sentenceWithWordCount.WordCounts) { stringBuilder.AppendFormat("\r\n{0} - {1}", wordCount.Word, wordCount.Count); } string output = stringBuilder.ToString(); return output; }