// Add a given word to the list if it has not already occured in the list, otherwise increase the count variable of the existing word. private static void AddToSet(ref List <WordInformation> set, WordInformation info, ref WordInformation previousInfo) { bool found = false; WordInformation foundInfo = null; foreach (WordInformation wordInfo in set) { if (wordInfo.Word == info.Word) { foundInfo = wordInfo; wordInfo.Count++; found = true; break; } } if (!found) { foundInfo = info; } if (previousInfo != null) { foundInfo.AddNeighbouringWord(previousInfo.Word, true); previousInfo.AddNeighbouringWord(foundInfo.Word, false); } previousInfo = foundInfo; if (!found) { set.Add(foundInfo); } }
private void calculateButton_Click(object sender, EventArgs e) { // Parse the data into seperate sentences parse(); // Parse the data again into words List <WordInformation> processedWordInformation = TextParser.ParseIntoWords(textToParse.Text); int max = 0; string word = ""; WordInformation commonWordInfo = null; // Find the most common word used in the data foreach (WordInformation info in processedWordInformation) { if (info.Count > max) { max = info.Count; word = info.Word; commonWordInfo = info; } } // Set the results to display on the probability results page commonWordResult.Text = word; commonWordCountResult.Text = max.ToString(); commonWordInfo.GetWordStats(ref max, ref word); previousWordResult.Text = word; previousWordCountResult.Text = max.ToString(); commonWordInfo.GetWordStats(ref max, ref word, WordInformation.WORDTYPE.NEXT); nextWordResult.Text = word; nextWordCountResult.Text = max.ToString(); }
// Split the string into seperate words and return a list containing all relevant word information public static List <WordInformation> ParseIntoWords(string data) { List <String> words = data.Split(' ').ToList(); List <WordInformation> result = new List <WordInformation>(); WordInformation precedingInfo = null; foreach (String word in words) { AddToSet(ref result, new WordInformation(word), ref precedingInfo); } return(result); }