private void WordFrequencyExplorer() { Console.WriteLine($"Unique word count: {UniqueWords.Count:n0}"); Console.WriteLine($"Total word count: {TotalWordCount:n0}"); Console.WriteLine($"Total verse count: {TotalVerseCount:n0}"); Console.WriteLine(); Console.WriteLine(); while (true) { Console.Write("Enter word: "); var line = Console.ReadLine(); var key = line.ToUpperInvariant().Trim(); if (string.IsNullOrWhiteSpace(key)) { return; } if (!WordFrequencies.ContainsKey(key)) { Console.WriteLine($"{key} was not found in this bible."); continue; } int count = WordFrequencies[key]; Console.WriteLine($"{key} was found ({count:n0}) times in this bible."); Console.WriteLine(); } }
// Methods ------------------ private void IncreaseWordFreq(string word, long frequency = 1) { TotalWordCounter += frequency; if (WordFrequencies.ContainsKey(word)) { WordFrequencies[word] += frequency; } else { WordFrequencies.Add(word, frequency); } }
private void WordLocationExplorer() { while (true) { Console.Write("Enter word: "); var line = Console.ReadLine(); var key = line.ToUpperInvariant().Trim(); if (string.IsNullOrWhiteSpace(key)) { return; } if (!WordFrequencies.ContainsKey(key)) { Console.WriteLine($"{key} was not found in this bible."); continue; } int count = WordFrequencies[key]; Console.WriteLine(); Console.ForegroundColor = ConsoleColor.DarkBlue; Console.WriteLine($"{key} was found ({count:n0}) times in this bible."); Console.ResetColor(); Console.WriteLine(); foreach (var location in WordLocations(key)) { Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine(location.ToString()); Console.ResetColor(); Console.WriteLine(location.Text); Console.WriteLine(); } } }
private void LoadWordFrequencies() { foreach (XmlNode book in BibleXml.DocumentElement.ChildNodes) { var bookName = book.Attributes["n"].InnerText; foreach (XmlNode chapter in book.ChildNodes) { var chapterName = chapter.Attributes["n"].InnerText; foreach (XmlNode verse in chapter.ChildNodes) { TotalVerseCount++; var verseNumber = verse.Attributes["n"].InnerText; string text = verse.InnerText.ToUpperInvariant(); var chars = new List <char>(); for (int i = 0; i < text.Length; i++) { var c = text[i]; if (!char.IsLetter(c) && c != ' ' && c != '-') { continue; } // allow hyphenated words if (i > 0 && i < text.Length - 1 && c == '-') { if (!char.IsLetter(text[i - 1]) || !char.IsLetter(text[i + 1])) { continue; } } chars.Add(text[i]); } text = string.Concat(chars).ToUpperInvariant(); var words = text.Split(' ', StringSplitOptions.RemoveEmptyEntries); foreach (var word in words) { var tempWord = word.Trim().TrimStart('-').TrimEnd('-'); if (string.IsNullOrWhiteSpace(tempWord)) { continue; } TotalWordCount++; if (WordFrequencies.ContainsKey(tempWord)) { WordFrequencies[tempWord]++; } else { WordFrequencies[tempWord] = 1; } UniqueWords.Add(tempWord); } } } } }