Exemplo n.º 1
0
        private static void AddWordsForSearchInTrie(TrieNode start, List<string> words)
        {
            stopwatchTest.Start();
            foreach (var item in words)
            {
                start.AddWord(start, item.ToString());
            }

            stopwatchTest.Stop();
            Console.WriteLine("Time to populate the trie: {0}", stopwatchTest.Elapsed);
        }
Exemplo n.º 2
0
        private static void IncrementOccuranceCountTrie(TrieNode start, MatchCollection allWords)
        {
            stopwatchTest.Restart();
            foreach (var word in allWords)
            {
                start.AddOccuranceIfExists(start, word.ToString());
            }

            stopwatchTest.Stop();
            Console.WriteLine("Adding searched words count trie for: {0}", stopwatchTest.Elapsed);
        }
Exemplo n.º 3
0
        public static void Main()
        {
            TrieNode start = new TrieNode();
            Dictionary<string, int> wordsInDictionary = new Dictionary<string, int>();

            string inputText = string.Empty;
            StreamReader inputReader = new StreamReader(@"..\..\text.txt");
            using (inputReader)
            {
                inputText = inputReader.ReadToEnd().ToLower();
            }

            // Regex is a bit slow but its easy to implement.
            var allWords = Regex.Matches(inputText, @"[a-zA-Z]+");
            
            List<string> searchedWords = new List<string>();

            // change number to increase searched words count
            for (int i = 0; i < 50; i++)
            {
                searchedWords.Add(allWords[i].ToString());
            }

            AddWordsForSearchInDictionary(searchedWords, wordsInDictionary);
            AddWordsForSearchInTrie(start, searchedWords);

            IncrementOccuranceCountTrie(start, allWords);
            IncrementOccuranceCountDictionary(wordsInDictionary, allWords);

            Console.WriteLine("Searched words count trie: ");
            foreach (var word in searchedWords)
            {
                Console.WriteLine("{0}: {1}", word, start.CountWords(start, word));
            }

            Console.WriteLine("Searched words count dictionary: ");
            foreach (var item in wordsInDictionary)
            {
                Console.WriteLine("{0}: {1}", item.Key, item.Value);
            }
        }
Exemplo n.º 4
0
        private void AddOccuranceIfExists(TrieNode node, string word, int indexInWord)
        {
            if (word.Length == indexInWord)
            {
                node.Words += 1;
            }
            else
            {
                int nextCharIndex = word[indexInWord] - 'a';
                indexInWord++;
                node.Prefixes += 1;

                if (node.edges[nextCharIndex] == null)
                {
                    return;
                }
                else
                {
                    this.AddOccuranceIfExists(node.edges[nextCharIndex], word, indexInWord);
                }
            }
        }
Exemplo n.º 5
0
        private TrieNode AddWord(TrieNode node, string word, int indexInWord)
        {
            if (word.Length != indexInWord)
            {
                node.Prefixes += 1;

                int index = word[indexInWord] - 'a';
                indexInWord++;

                if (node.edges[index] == null)
                {
                    node.edges[index] = new TrieNode();
                }

                node.edges[index] = this.AddWord(node.edges[index], word, indexInWord);
            }

            return node;
        }
Exemplo n.º 6
0
 public int CountPrefix(TrieNode node, string word)
 {
     return this.CountPrefix(node, word, 0);
 }
Exemplo n.º 7
0
 public int CountWords(TrieNode node, string word)
 {
     return this.CountWords(node, word, 0);
 }
Exemplo n.º 8
0
 public void AddOccuranceIfExists(TrieNode node, string word)
 {
     this.AddOccuranceIfExists(node, word, 0);
 }
Exemplo n.º 9
0
 public TrieNode AddWord(TrieNode node, string word)
 {
     return this.AddWord(node, word, 0);
 }
Exemplo n.º 10
0
        private int CountPrefix(TrieNode node, string word, int indexInWord)
        {
            if (word.Length == indexInWord)
            {
                return node.prefixes;
            }
            else
            {
                int nextCharIndex = word[indexInWord] - 'a';
                indexInWord++;

                if (node.edges[nextCharIndex] == null)
                {
                    return 0;
                }
                else
                {
                    return this.CountPrefix(node.edges[nextCharIndex], word, indexInWord);
                }
            }
        }