/// <summary> /// Add a particular word to node /// </summary> /// <param name="node">Node to which the new nodes to apped as child</param> /// <param name="word">Word to be converted in the form of trie nodes</param> private void AddWord(TrieNode node, string word, Dictionary <int, int> dicDocuFrequcy) { for (int _index = 0, len = word.Length; _index < len; _index++) { string _characterToAdd = word[_index].ToString(); TrieNode _childNode = node.GetChild(_characterToAdd); if (_childNode == null) { _wordIndex++; _childNode = TrieFactory.CreateTrieNode(_characterToAdd); _childNode.WordIndex = _wordIndex; _childNode.DocReferences = dicDocuFrequcy; node.SetChild(_childNode); } node = _childNode; } node.WordCount++; }
/// <summary> /// Creates the trie from flattend body contents. Stores the frequency of the word in the particular document by it's id /// </summary> /// <param name="webResponses"></param> public void CreateTrie(Dictionary <int, string> webResponses) { try { if (_trie == null) { _trie = TrieFactory.CreateTrie(); } foreach (var _webString in webResponses) { List <string> _lstSorted = _webString.Value.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList(); _lstSorted.Sort(); var numberOfTestcasesWithDuplicates = (from word in _lstSorted where !Constants._lstExclusion.Contains(word.ToLower()) select word.ToLower()) .GroupBy(x => x) .ToDictionary(x => x.First(), x => x.Count()); foreach (var item in numberOfTestcasesWithDuplicates) { var _trieNode = _trie.ContainsWord(item.Key.Trim()); if (_trieNode != null) { _trieNode.DocReferences[_webString.Key] = item.Value; } else { _trie.AddWord(item.Key.Trim(), new Dictionary <int, int>() { { _webString.Key, item.Value } }); } } } } catch (System.Exception ex) { Console.WriteLine($"An exception occurred while creating trie {ex}"); } }