コード例 #1
0
ファイル: ListTrie.cs プロジェクト: campbellsean/SearchEngine
        private List <string> GetSearchResults(HybridNode node, string searchTerm)
        {
            List <string> results = new List <string>();

            searchTerm = searchTerm.Replace(' ', '_');
            string lowercaseSearch = searchTerm.ToLower();

            // searchTerm = he
            HybridNode resultsNode = node;
            string     level       = "";

            foreach (char c in lowercaseSearch)
            {
                if (resultsNode.useDict && resultsNode.Children.ContainsKey(c))
                {
                    level       = level + c;
                    resultsNode = resultsNode.Children[c];
                }
            }

            while (resultsNode.Suffixs == null)
            {
                var first = resultsNode.Children.First().Value;
                level       = level + first.Value;
                resultsNode = first;
            }

            if (resultsNode != null)
            {
                foreach (string s in resultsNode.Suffixs)
                {
                    string complete = level + s;

                    if (complete.StartsWith(lowercaseSearch)) // add in check for 10 count
                    {
                        results.Add(complete);
                    }
                    if (results.Count >= 10)
                    {
                        break;
                    }
                }
            }

            List <string> resultsNoUnderscore = new List <string>();

            foreach (string s in results)
            {
                string noUnderscore = s.Replace('_', ' ');
                resultsNoUnderscore.Add(noUnderscore);
            }
            return(resultsNoUnderscore);
        }
コード例 #2
0
        // Seems like it might work
        public void Add(string toAdd)
        {
            if (this.useDict)
            {
                if (toAdd.Length >= 1)
                {
                    char   firstChar        = toAdd[0];
                    string withoutFirstChar = toAdd.Substring(1);

                    if (this.Children.ContainsKey(firstChar))
                    {
                        this.Children[firstChar].Add(withoutFirstChar);
                    }
                    else
                    {
                        List <string> toAddSuffix = new List <string>();
                        toAddSuffix.Add(withoutFirstChar);

                        HybridNode newNode = new HybridNode(firstChar, toAddSuffix, new Dictionary <char, HybridNode>());
                        this.Children.Add(firstChar, newNode);
                    }
                }
            }
            else if (this.Suffixs.Count < 50)
            {
                this.Suffixs.Add(toAdd);
            }
            else if (this.Suffixs.Count == 50)
            {
                this.useDict = true;
                // we want to add "john"

                // for the first letter of every word in the suffix we want to add to the dictionary and
                // create a hybrid node
                foreach (string s in this.Suffixs)
                {
                    this.Add(s);
                }
                // finally we add John and use a dictionary this time
                this.Add(toAdd);

                // to save space
                this.Suffixs = null;
            }
        }
コード例 #3
0
ファイル: ListTrie.cs プロジェクト: campbellsean/SearchEngine
 public ListTrie()
 {
     _root = new HybridNode('^', new List <string>(), new Dictionary <char, HybridNode>());
 }