Exemplo n.º 1
0
        static void Main(string[] args)
        {
            long count = 0;
            trie = new Trie();
            dict = new Dictionary<string, bool>();
            foreach (var word in Random.Words(10, 40))
            {
                trie.Add(word);
                dict[word] = true;
                count++;
            }

            Console.Clear();

            // 123 test
            bench("123", 0);
            // test an early find on list
            bench("car", 30);
            // test the last one on list
            bench("caterpillar", 60);

            Console.WriteLine();
            Console.WriteLine(count);
            Console.Read();
        }
Exemplo n.º 2
0
        private static Trie<int> ReadFile(string fileName)
        {
            string text = "";
            try
            {
                Console.Write("Reading file");
                using (StreamReader sr = new StreamReader(fileName))
                {
                    text = sr.ReadToEnd();
                }
            }
            catch (IOException e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            Console.WriteLine(" Done");
            Console.Write("Extracting words");
            words = new List<string>(text.ToLower().Split(new char[] { '"', '\'', '\r', '\n', '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries));
            Trie<int> result = new Trie<int>();
            Console.WriteLine(" Done. {0} words extracted", words.Count());
            for (int i = 0; i < words.Count; i++)
            {
                if (i % (words.Count / 100) == 0)
                {
                    Console.Write("\rBuilding trie {0} percent complete", (i * 100) / words.Count);
                }
                result.Add(words[i], i);
            }

            Console.WriteLine();
            return result;
        }
Exemplo n.º 3
0
 private static void BuildUp(string fileName, Trie<int> trie)
 {
     IEnumerable<WordAndLine> allWordsInFile = GetWordsFromFile(fileName);
     foreach (WordAndLine wordAndLine in allWordsInFile)
     {
         trie.Add(wordAndLine.Word, wordAndLine.Line);
     }
 }
        public dynamic Get(int count)
        {
            while (words.Count < count)
                words.AddRange(Trie.Random.Words(5, 10));

            DateTime sTime;
            var trie = new Trie.Trie();
            var dict = new Dictionary<string, bool>();
            var testWords = new List<string> {"123", "car", "caterpillar" };

            var trieData = new List<List<int>>();
            var dictData = new List<List<int>>();
            int x = 0;
            int jumps = 5000;
            while (x < count)
            {
                foreach (var word in words.Take(jumps).Skip(x))
                {
                    trie.Add(word);
                    dict[word] = true;
                }

                int dictZum = 0;
                int trieZum = 0;
                foreach (var word in testWords)
                {
                    for (int J = 0; J < TESTS; J++)
                    {
                        sTime = DateTime.Now;
                        for (int i = 0; i < LOOPS; i++)
                            trie.Contains(word);
                        trieZum += sTime.Diff();

                        sTime = DateTime.Now;
                        for (int i = 0; i < LOOPS; i++)
                            dict.ContainsKey(word);
                        dictZum += sTime.Diff();
                    }
                }
                x += jumps;
                trieData.Add(new List<int> { x, trieZum / (TESTS * testWords.Count) });
                dictData.Add(new List<int> { x, dictZum / (TESTS * testWords.Count) });
            }

            dynamic chart = new ExpandoObject();
            chart.Label = "TrieBenchmark";
            chart.TrieData = trieData;
            chart.DictData = dictData;

            return Json(chart);
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            int  numberOfInstructions = Convert.ToInt32(Console.ReadLine());
            Trie trie = new Trie();

            for (int index_instruction = 0; index_instruction < numberOfInstructions; index_instruction++)
            {
                string[] tokens_op = Console.ReadLine().Split(' ');
                string   op        = tokens_op[0];
                string   contact   = tokens_op[1];
                switch (op)
                {
                case "add":
                    trie.Add(contact);
                    break;

                case "find":
                    Console.WriteLine(trie.CountAllStartsWith(contact));
                    break;
                }
            }
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            Console.WriteLine("Trie workout!");
            var trie = new Trie();

            trie.Add("hello");
            trie.Add("helloworld");
            trie.Add("hell");
            trie.Add("hilarious");
            trie.Add("hilary");
            trie.Add("hilbert");
            trie.Print();

            Console.WriteLine($"string hell exists in " + trie.LookUp("hell"));
            Console.WriteLine($"string hello exists in " + trie.LookUp("hello"));
            Console.WriteLine($"string hel exists in " + trie.LookUp("hel"));

            var list = trie.AutoComplete("hi");

            Console.WriteLine("AutoComplete for hi are " + string.Join(", ", list));
            var list1 = trie.AutoComplete("he");

            Console.WriteLine("AutoComplete for he are " + string.Join(", ", list1));
        }
Exemplo n.º 7
0
 public static void PopulateTrie(Trie trie)
 {
     trie.Add("hello");
     trie.Add("high");
     trie.Add("green");
 }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            var trie = new Trie <int>();

            trie.Add("привет", 50);
            trie.Add("мир", 100);
            trie.Add("приз", 200);
            trie.Add("мирный", 50);
            trie.Add("подарок", 100);
            trie.Add("проект", 200);
            trie.Add("прапорщик", 100);
            trie.Add("правый", 200);
            trie.Add("год", 50);
            trie.Add("герой", 100);
            trie.Add("красота", 300);
            trie.Add("голубь", 200);
            trie.Add("прокрастиниция", 1000);

            trie.Remove("правый");
            trie.Remove("мир");

            Search(trie, "привет");
            Search(trie, "мир");
            Search(trie, "облако");
            Search(trie, "мирный");
            Search(trie, "прокрастиниция");
            Search(trie, "год");

            Console.ReadLine();
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            var trie = new Trie<int>();
            trie.Add("Hello", 50);
            trie.Add("World", 100);
            trie.Add("Prize", 200);
            trie.Add("Herald", 150);
            trie.Add("Gift", 210);
            trie.Add("Project", 220);
            trie.Add("Ordinary", 230);
            trie.Add("Right", 240);
            trie.Add("Year", 250);
            trie.Add("Hero", 260);
            trie.Add("Beauty", 260);
            trie.Add("pigeon", 270);
            trie.Add("Eye", 280);
            trie.Add("Procrastination", 1000);

            trie.Remove("Right");
            trie.Remove("World");

            Search(trie,"Hello");
            Search(trie, "World");
            Search(trie, "Cloud");
            Search(trie, "Herald");
            Search(trie, "Ordinary");
            Search(trie, "Procrastination");

            Console.ReadKey();

        }
Exemplo n.º 10
0
        public dynamic Get(int count)
        {
            while (words.Count < count)
            {
                words.AddRange(Trie.Random.Words(5, 10));
            }

            DateTime sTime;
            var      trie      = new Trie.Trie();
            var      dict      = new Dictionary <string, bool>();
            var      testWords = new List <string> {
                "123", "car", "caterpillar"
            };

            var trieData = new List <List <int> >();
            var dictData = new List <List <int> >();
            int x        = 0;
            int jumps    = 5000;

            while (x < count)
            {
                foreach (var word in words.Take(jumps).Skip(x))
                {
                    trie.Add(word);
                    dict[word] = true;
                }

                int dictZum = 0;
                int trieZum = 0;
                foreach (var word in testWords)
                {
                    for (int J = 0; J < TESTS; J++)
                    {
                        sTime = DateTime.Now;
                        for (int i = 0; i < LOOPS; i++)
                        {
                            trie.Contains(word);
                        }
                        trieZum += sTime.Diff();

                        sTime = DateTime.Now;
                        for (int i = 0; i < LOOPS; i++)
                        {
                            dict.ContainsKey(word);
                        }
                        dictZum += sTime.Diff();
                    }
                }
                x += jumps;
                trieData.Add(new List <int> {
                    x, trieZum / (TESTS * testWords.Count)
                });
                dictData.Add(new List <int> {
                    x, dictZum / (TESTS * testWords.Count)
                });
            }

            dynamic chart = new ExpandoObject();

            chart.Label    = "TrieBenchmark";
            chart.TrieData = trieData;
            chart.DictData = dictData;

            return(Json(chart));
        }