Esempio n. 1
0
        public static void Main(string[] args)
        {
            // Trie implementation from https://github.com/rmandvikar/csharp-trie
            Console.WriteLine("The program read all words from 'text.txt',");
            Console.WriteLine("adds them in a Trie and prnt the first 1000 words");
            Console.WriteLine("along with the occurrences of each word");
            Console.WriteLine("Press any key to begin..");
            Console.ReadKey();

            ITrie trie   = TrieFactory.CreateTrie();
            var   reader = new StreamReader("../../text.txt");

            ReadWordsFromText(reader, trie);

            var counter = 0;

            foreach (var currentWord in trie.GetWords())
            {
                counter++;

                if (counter == 1000)
                {
                    break;
                }

                var wordCounter = trie.WordCount(currentWord);
                Console.WriteLine("{0} - {1}", currentWord, wordCounter);
            }
        }
Esempio n. 2
0
        public void CreateDataSet()
        {
            var start = DateTime.UtcNow;

            _testInitializeTime = DateTime.UtcNow - start;
            Utils.CheckFileExists(DataFile);
            var file = new FileInfo(DataFile);

            try
            {
                var array = new byte[file.Length];
                using (var stream = file.OpenRead())
                {
                    stream.Read(array, 0, (int)file.Length);
                }
                _provider = TrieFactory.Create(array);
            }
            catch (OutOfMemoryException)
            {
                Assert.Inconclusive(
                    "Not enough memory to perform memory test on data file '{0}' of size '{1}'MB",
                    file.Name,
                    file.Length / (1024 * 1024));
            }
            _testInitializeTime = DateTime.UtcNow - start;
        }
Esempio n. 3
0
        public static void Main()
        {
            Console.WriteLine("Generating a very big mothereffing lorem text. Please stand by...");
            RandomDataGenerator.GenerateLorem(LoremPath);

            var trie     = TrieFactory.CreateTrie();
            var wordsSet = new List <string>(1000);

            Console.WriteLine("Reading words from file...");
            var words = File.ReadAllLines(LoremPath).SelectMany(x => x.Split(new[] { ' ', ',', '.', '!', '?' }, StringSplitOptions.RemoveEmptyEntries));

            Console.WriteLine("Saving words...");
            foreach (var word in words)
            {
                trie.AddWord(word);

                if (!wordsSet.Contains(word) && wordsSet.Count < 999)
                {
                    wordsSet.Add(word);
                }
            }

            var sb = new StringBuilder();

            Console.WriteLine("Counting words...");
            foreach (var word in wordsSet)
            {
                sb.AppendLine(string.Format("{0} occured {1} times in the text.", word, trie.WordCount(word)));
            }

            Console.WriteLine(sb.ToString());
        }
Esempio n. 4
0
 public void AddWord_EmptyString01()
 {
     trie = TrieFactory.CreateTrie();
     Assert.AreEqual(0, trie.GetWords().Count);
     trie.AddWord("");
     Assert.AreNotEqual(0, trie.GetWords().Count);
 }
Esempio n. 5
0
        static void Setup()
        {
            /*Setup dictionary*/
            _dictionary = TrieFactory.CreateTrie();
            StreamReader reader = new StreamReader("..\\..\\..\\SampleText.txt");

            string[] words = reader.ReadToEnd().Split(' ', '.', ',', '?', '!', ':', ';');
            foreach (var word in words)
            {
                _dictionary.AddWord(word.ToLower());
            }
            /*end dictionary*/

            /*setup sample sentence*/
            _sentence = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
            /*end sample sentence*/

            /*setup random encryption*/
            _encryption = Service.GetRandomEncryption();
            /*end random encryption*/

            /*encrypt sentence*/
            _encryptedSentence = Service.Encrypt(_sentence, _encryption);
            /*end ecnryption*/
        }
Esempio n. 6
0
        internal static void Init()
        {
            DataProviderSettings providerSettings = ConfigurationManager.GetSection("dictionaryService")
                                                    as DataProviderSettings;

            var providers = providerSettings.Instances;

            foreach (DataProviderSetting setting in providers)
            {
                Stopwatch timer = new Stopwatch();
                timer.Start();
                IDataProvider dataProvider = null;
                try
                {
                    dataProvider = (IDataProvider)Activator.CreateInstance(Type.GetType(setting.ProviderType, true), new object[] { setting });
                    TrieFactory.LoadFromDataProvider(dataProvider);
                }
                catch (FileNotFoundException e)
                {
                    logger.Error("Invalid provider setting: " + setting.ProviderType);
                }catch (TypeLoadException e)
                {
                    logger.Error("Invalid provider setting: " + setting.ProviderType);
                }
                timer.Stop();
                logger.InfoFormat("{0}: {1:0.00} seconds", setting.Name, timer.ElapsedMilliseconds / 1000.0);
            }
        }
        public void CreateDataSet()
        {
            var start = DateTime.UtcNow;

            Utils.CheckFileExists(DataFile);
            _provider           = TrieFactory.Create(DataFile);
            _testInitializeTime = DateTime.UtcNow - start;
        }
Esempio n. 8
0
        public void InsertIntoTrie()
        {
            Trie trie = TrieFactory.Create();
            Tuple <string, int> tuple = new Tuple <string, int>("bogus", 99);

            trie.Insert(tuple.Item1, tuple.Item2);
            Assert.AreEqual(trie.Count(), 1);
            Assert.AreEqual(trie.Get(tuple.Item1), tuple.Item2);
        }
        public static void Main()
        {
            var trie = TrieFactory.CreateTrie();

            var words       = GenerateRandomWords(1000000);
            var uniqueWords = new HashSet <string>(words);

            AddWordsToTrie(words, trie);
            GetCountOfAllUniqueWords(uniqueWords, trie);
        }
Esempio n. 10
0
        public void TestLoadTrieTreeFromPanguDict()
        {
            var mockPanguProviderSetting = new Mock <IDataProviderSetting>();

            mockPanguProviderSetting.Setup(s => s.Uri).Returns(DictLoadTest.UnitTestProjectFolder + @"\Data\panguDict.dct");
            mockPanguProviderSetting.Setup(s => s.ProviderType).Returns("PanguDictProvider");

            var          panguProvider = new PanguDictProvider(mockPanguProviderSetting.Object);
            TrieTree     tt            = TrieFactory.LoadFromDataProvider(panguProvider);
            TrieTreeNode ttn           = tt.GetNode("测试");

            Assert.IsNotNull(ttn);
            Assert.AreEqual(POSType.D_N, (POSType)ttn.POSValue);
        }
Esempio n. 11
0
        public void Test13()
        {
            var trie = TrieFactory.CreateTrie();

            trie.AddWord("");
            trie.AddWord("");
            Assert.AreEqual(1, trie.GetWords().Count);
            trie.RemoveWord("");
            Assert.AreEqual(0, trie.GetWords().Count);
            trie.AddWord("");
            Assert.AreEqual(1, trie.GetWords().Count);
            trie.RemoveWord("");
            Assert.AreEqual(0, trie.GetWords().Count);
        }
Esempio n. 12
0
        private ITrie LowerCaseWords()
        {
            ITrie trie = TrieFactory.CreateTrie();

            string[] strings =
            {
                "this", "test", "the", "temp", "token", "take", "thump"
            };

            foreach (string s in strings)
            {
                trie.AddWord(s);
            }
            return(trie);
        }
Esempio n. 13
0
        private ITrie UpperCaseWords()
        {
            ITrie trie = TrieFactory.CreateTrie();

            string[] strings =
            {
                "THIS", "TEST", "THE", "TEMP", "TOKEN", "TAKE", "THUMP"
            };

            foreach (string s in strings)
            {
                trie.AddWord(s);
            }
            return(trie);
        }
Esempio n. 14
0
        private ITrie Digits()
        {
            ITrie trie = TrieFactory.CreateTrie();

            string[] strings =
            {
                "123", "1", "23"
            };

            foreach (string s in strings)
            {
                trie.AddWord(s);
            }
            return(trie);
        }
Esempio n. 15
0
        public void TestLoadTrieTreeFromMongoDB()
        {
            var mockProviderSetting = new Mock <IDataProviderSetting>();

            mockProviderSetting.Setup(s => s.Uri).Returns("mongodb://localhost:28001");
            mockProviderSetting.Setup(s => s.ProviderType).Returns("MongoDBDataProvider");
            mockProviderSetting.Setup(s => s.DBName).Returns("nameResearch");
            mockProviderSetting.Setup(s => s.CollectionName).Returns("placeNames");
            var      dataProvider = new MongoDBDataProvider(mockProviderSetting.Object);
            TrieTree tt           = TrieFactory.LoadFromDataProvider(dataProvider);

            Assert.IsNotNull(tt);
            Assert.IsNotNull(tt.Root);
            Assert.IsTrue(tt.Root.Children.Count > 100);
        }
Esempio n. 16
0
        private ITrie Words()
        {
            ITrie trie = TrieFactory.CreateTrie();

            string[] strings =
            {
                "this", "test", "the", "TEMP", "TOKEN", "TAKE", "THUMP"
            };

            foreach (string s in strings)
            {
                trie.AddWord(s);
            }
            return(trie);
        }
        public static void Main(string[] args)
        {
            var words = ReadAllWords();
            var trie  = TrieFactory.GetTrie();

            foreach (var word in words)
            {
                trie.AddWord(word);
            }

            var toSearch = new string[] { "curtain", "you", "well", "lighted" };

            foreach (var word in toSearch)
            {
                Console.WriteLine("{0} - {1}", word, trie.WordCount(word));
            }
        }
Esempio n. 18
0
    static void Main()
    {
        var date  = DateTime.Now;
        var words = GetWords();

        Console.WriteLine(DateTime.Now - date);

        var trie = TrieFactory.GetTrie();

        foreach (var word in words)
        {
            trie.AddWord(word);
        }

        var searched = new[] { "buddhism", "wikipedia", "the", "of", "factors" };

        Console.WriteLine(string.Join(" | ", searched.Select(word => string.Format("{0} {1}", word, trie.WordCount(word)))));
    }
Esempio n. 19
0
        public static ITrie BuildTree()
        {
            var wordTrie = TrieFactory.GetTrie();

            using (StreamReader textReader = new StreamReader(FileLocation))
            {
                string line = textReader.ReadLine();

                while (line != null)
                {
                    var words = line.Split(Separators, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var word in words)
                    {
                        wordTrie.AddWord(word);
                    }

                    line = textReader.ReadLine();
                }
            }

            return(wordTrie);
        }
Esempio n. 20
0
        public static void Main()
        {
            ITrie trie = TrieFactory.CreateTrie();

            using (var reader = new StreamReader(@"..\..\Files\text.txt"))
            {
                while (!reader.EndOfStream)
                {
                    reader
                    .ReadLine()
                    .Split(' ', '.', ',', '?', '!', ':')
                    .ToList()
                    .ForEach(word =>
                    {
                        trie.AddWord(word);
                    });
                }
            }

            var countOfLorem = trie.WordCount("lorem");

            Console.WriteLine("Lorem -> {0} times", countOfLorem);
        }
Esempio n. 21
0
        public void TraverseTriePreOrder()
        {
            Trie trie = TrieFactory.Create();
            List <Tuple <string, int> > tuples = new List <Tuple <string, int> >()
            {
                new Tuple <string, int>("bogus", 99),
                new Tuple <string, int>("aack", 88),
                new Tuple <string, int>("zebra", 77),
                new Tuple <string, int>("kangaroo", 66),
                new Tuple <string, int>("donkey", 55),
                new Tuple <string, int>("dog", 44)
            };

            foreach (Tuple <string, int> tuple in tuples)
            {
                trie.Insert(tuple.Item1, tuple.Item2);
            }
            Assert.AreEqual(trie.Count(), tuples.Count);

            TestVisitor testVisitor = new TestVisitor();

            trie.Traverse(TraversalType.PreOrder, testVisitor);
            Console.WriteLine(testVisitor.ToString());
        }
Esempio n. 22
0
        static void Main(string[] args)
        {
            var trie = TrieFactory.CreateTrie();
            HashSet <string> words = new HashSet <string>();

            using (StreamReader reader = new StreamReader("../../100MB.txt"))
            {
                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    foreach (var word in line.Split(' '))
                    {
                        words.Add(word);
                        trie.AddWord(word);
                    }
                }
            }

            foreach (var word in words)
            {
                Console.WriteLine("{0} - {1} times", word, trie.WordCount(word));
            }
        }
        public static void Main()
        {
            System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
            timer.Start();
            var words = GetWords();

            timer.Stop();
            Console.WriteLine("Adding the words to a collection: {0}", timer.Elapsed);

            var trie = TrieFactory.GetTrie();

            timer.Start();

            foreach (var word in words)
            {
                trie.AddWord(word);
            }
            timer.Stop();
            Console.WriteLine("Adding words to the trie: " + timer.Elapsed);

            timer.Restart();
            Console.WriteLine("Number of occurrences of \"the\": {0}", trie.WordCount("the"));
            Console.WriteLine("Counting the word \"the\": {0}", timer.Elapsed);
        }
Esempio n. 24
0
 public void GetFromEmptyTrie()
 {
     Trie   trie  = TrieFactory.Create();
     string key   = "bogus";
     int    value = trie.Get(key);
 }
Esempio n. 25
0
 public void CreateDataSet()
 {
     Utils.CheckFileExists(DataFile);
     _provider = TrieFactory.Create(DataFile);
 }
 public void CreateDataSet()
 {
     _memory = new Utils.MemoryMonitor();
     Utils.CheckFileExists(DataFile);
     _provider = TrieFactory.Create(DataFile);
 }