public TextResult(Text text) { SentenceCount = text.SentenceCount; WordCount = text.WordCount; List<Sentence> sentences = text.FindSentenceWithMostWords(0); if (sentences.Count > 0) { LongestSentences = sentences[0].SentenceSnippet; for (int i = 1; i < sentences.Count; i++) { LongestSentences += ", " + sentences[i].SentenceSnippet; } } List<string> commonWords = text.FindMostCommonWord(0); if (commonWords.Count > 0) { MostCommonWords = commonWords[0]; for (int i = 1; i < commonWords.Count; i++) { MostCommonWords += ", " + commonWords[i]; } } List<string> longestWords = text.FindLongestWord(2); if (longestWords.Count > 0) { LongestWords = longestWords[0]; for (int i = 1; i < longestWords.Count; i++) { LongestWords += ", " + longestWords[i]; } } }
/// <summary> /// This method takes a Text object and prints data from it to the console /// </summary> /// <param name="text">A Text object</param> void DisplayResults(Text text) { // this method is quite long and verbose. I've obviously done a lot of cutting and pasting here and I was going to come back to clean this up, but unfortunately I've run out of time. Console.WriteLine("Number of sentences: " + text.SentenceCount); Console.WriteLine("Number of words: " + text.WordCount); string longestSentences = "Sentence(s) with the most words: "; List<Sentence> longestSentencesList = text.FindSentenceWithMostWords(0); if (longestSentencesList.Count > 0) { longestSentences += longestSentencesList[0].SentenceSnippet; for (int i = 1; i < longestSentencesList.Count; i++) { longestSentences += ", " + longestSentencesList[0].SentenceSnippet; } } Console.WriteLine(longestSentences); string mostFrequentWord = "Most frequently used word(s): "; List<string> mostFrequentWords = text.FindMostCommonWord(0); if (mostFrequentWords.Count > 0) { mostFrequentWord += mostFrequentWords[0]; for (int i = 1; i < mostFrequentWords.Count; i++) { mostFrequentWord += ", " + mostFrequentWords[i]; } } Console.WriteLine(mostFrequentWord); string longestWords = "3rd longest word(s): "; List<string> longestWords2 = text.FindLongestWord(2); if (longestWords2.Count > 0) { longestWords += longestWords2[0]; for (int i = 1; i < longestWords2.Count; i++) { longestWords += ", " + longestWords2[i]; } } Console.WriteLine(longestWords); AskUserForCmd(); }
void UploadFile() { OpenFileDialog fbd = new OpenFileDialog(); if (fbd.ShowDialog() == DialogResult.OK) { string text; Stream s = fbd.OpenFile(); using (System.IO.StreamReader reader = new System.IO.StreamReader(s)) { text = reader.ReadToEnd(); } s.Close(); try { Text t = new Text(text); DisplayResults(t); } catch (InvalidWordException ex) { Console.WriteLine(ex.WordText + " is not a valid word"); } catch (InvalidSentenceException ex) { Console.WriteLine("\"" + ex.SentenceText + "\" is not a valid sentence"); } } else { Console.WriteLine("File didn't work"); AskUserForCmd(); } }