コード例 #1
0
        public void Add_ItemExists_Throws()
        {
            Trie<string, char, int> trie = new Trie<string, char, int>();

            trie.Add("key", 5);
            trie.Add("key", 6);
        }
コード例 #2
0
ファイル: TrieTests.cs プロジェクト: kpol/trie
        public void AddWithSameKey()
        {
            var trie = new Trie<bool>();

            trie.Add("a", false);
            trie.Add("a", true);
        }
コード例 #3
0
ファイル: GeneralTrieTests.cs プロジェクト: 8thbit/Trie
        public void Twice()
        {
            var trie = new Trie<string, string>();
            trie.Add("123", "hello");
            trie.Add("123", "bye");

            var get = trie.Values("123");
            Assert.Equal("bye", get.First());
            Assert.Equal("hello", get.Last());
        }
コード例 #4
0
ファイル: TrieTests.cs プロジェクト: kpol/trie
        public void Clear()
        {
            var trie = new Trie<bool>();

            trie.Add("ABC", false);
            trie.Add("AB", false);
            trie.Add("ADE", false);
            trie.Add("ABCDE", false);

            trie.Clear();

            Assert.AreEqual(0, trie.Count);
        }
コード例 #5
0
		public void Trie_AddInstructions()
		{
            X86InstructionComparer cmp = new X86InstructionComparer(Normalize.Nothing);
            var trie = new Trie<MachineInstruction>(cmp);
			IntelInstruction inst = CreatePush(Registers.bp);
			
			trie.Add(new [] { inst });
			Assert.AreEqual(trie.Count, 1);

			trie.Add(new [] {
				CreatePush(Registers.bp),
				CreateMov(Registers.bp, Registers.sp) });
			Assert.AreEqual(trie.Count, 3);
		}
コード例 #6
0
        public void PrefixMatchTest()
        {
            string s = @"
                In computer science, a trie, or prefix tree,
                is an ordered tree data structure that is used to
                store an associative array where the keys are strings.
                Unlike a binary search tree, no node in the tree
                stores the key associated with that node;
                instead, its position in the tree shows what key
                it is associated with. All the descendants
                of any one node have a common prefix of the string
                associated with that node, and the root is associated
                with the empty string. Values are normally not associated
                with every node, only with leaves and some inner nodes
                that happen to correspond to keys of interest.
            ";

            Trie<char, string> wordIndex = new Trie<char, string>(new LettersDomain());
            foreach (string word in s.Replace("\r", "").Replace("\n", " ").Replace("\t", "").Split(' ', ',', ';', '.'))
            {
                if (word.Length > 0)
                {
                    wordIndex.Add(word, word);
                }
            }

            ITrieEnumerator<char, string> matches = wordIndex.PrefixMatch("p");
            while (matches.MoveNext())
            {
                Console.WriteLine(matches.Current.Value);
            }
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: Gobiner/ghost-puzzle
 private static Trie<string> ParseTree(IEnumerable<string> p)
 {
     var trie = new Trie<string>();
     foreach (var word in p)
         trie.Add(word, word);
     return trie;
 }
コード例 #8
0
        public static void Main()
        {
            Trie<int> trie = new Trie<int>();

            using (StreamReader reader = new StreamReader("../../test.txt"))
            {
                while (!reader.EndOfStream)
                {
                    string[] line = reader.ReadLine().Split(new char[] { ' ', '.', ',', '!', '?', ':', ';', '-' }, StringSplitOptions.RemoveEmptyEntries).ToArray();

                    foreach (var word in line)
                    {
                        if (!trie.ContainsKey(word))
                        {
                            trie.Add(word, 1);
                        }
                        else
                        {
                            trie[word]++;
                        }
                    }
                }
            }

            trie.Matcher.Next("eros");
            Console.WriteLine(trie.Matcher.GetExactMatch());
        }
コード例 #9
0
ファイル: Testing.cs プロジェクト: VDGone/TelerikAcademy-2
    public static void Main()
    {
        string inputText;
        StreamReader reader = new StreamReader("test.txt");

        using (reader)
        {
            inputText = reader.ReadToEnd().ToLower();
        }

        var matches = Regex.Matches(inputText, @"[a-zA-Z]+");
        HashSet<string> uniqueWords = new HashSet<string>();
        var trie = new Trie();

        for (int i = 0; i < matches.Count; i++)
        {
            uniqueWords.Add(matches[i].ToString());
            trie.Add(matches[i].ToString());
        }

        foreach (var word in uniqueWords)
        {
            Console.WriteLine("{0} -> {1} times", word, trie.GetWordOccurance(word));
        }
    }
コード例 #10
0
        static void Main(string[] args)
        {
            Stopwatch _s = new Stopwatch();

            _s.Start();

            /* */
            //Trie<byte[]> _trie = new Trie<byte[]>();
            Trie<string> _trie = new Trie<string>();
            for (int i = 0; i < 1000000; i++)
            {
                //byte[] bkey = Encoding.ASCII.GetBytes("root/sub/key" + i);
                //byte[] bvalue = Encoding.ASCII.GetBytes("test_" + i);
                _trie.Add("root/sub/key" + i, "test_" + i);
            }

            //var f=_trie.Find("root/sub/key999090");
            var g = _trie.FindAll("root/sub/key0");
            // SPEED ~ 1700 msec
            //*/

            /* /
            Trie2 _trie2 = new Trie2();
            for (int i = 0; i < 1000000; i++)
            {
                _trie2.AddNodeForWord("root/sub/key" + i);
            }
            //*/

            _s.Stop();

            Console.WriteLine("hj");
            Console.ReadKey();
        }
コード例 #11
0
        private static Trie<int> ParseFile(string filename)
        {
            Trie<int> trie = new Trie<int>();

            Console.WriteLine("Reading file and loading content in trie:");

            using (StreamReader reader = new StreamReader(filename))
            {
                while (!reader.EndOfStream)
                {
                    reader
                        .ReadLine()
                        .Split(' ', '.', ',', '?', '!', ':')
                        .ToList()
                        .ForEach(word =>
                        {
                            if (!trie.ContainsKey(word))
                            {
                                trie.Add(word, 1);
                            }
                            else
                            {
                                trie[word] += 1;
                            }
                        });
                }
            }

            Console.WriteLine("File read and loaded into trie");

            return trie;
        }
コード例 #12
0
ファイル: GeneralTrieTests.cs プロジェクト: 8thbit/Trie
        public void NoKey()
        {
            var trie = new Trie<string, string>();
            trie.Add("123", "hello");

            var get = trie.Values("43").SingleOrDefault();
            Assert.Null(get);
        }
コード例 #13
0
ファイル: GeneralTrieTests.cs プロジェクト: 8thbit/Trie
        public void TrieOnce()
        {
            var trie = new Trie<string, string>();
            trie.Add("123", "hello");

            var get = trie.Values("123").Single();
            Assert.Equal("hello", get);
        }
コード例 #14
0
		public void Trie_ScoreInstructions()
		{
			X86InstructionComparer cmp = new X86InstructionComparer(Normalize.Nothing);
			var trie = new Trie<IntelInstruction>(cmp);
			trie.Add(new [] {
				CreatePush(Registers.bp),
				CreateMov(Registers.bp, Registers.sp) });
			trie.Add(new [] {
				CreatePush(Registers.bp),
				CreateMov(Registers.bp, Registers.sp),
				CreatePush(Registers.si),
				CreatePush(Registers.di) });

			long score = trie.ScoreInstructions(new [] {
				CreatePush(Registers.bp) });
			Assert.AreEqual(2, score);
			score = trie.ScoreInstructions(new [] {
				CreatePush(Registers.bp),
				CreateMov(Registers.bp, Registers.sp) } );
			Assert.AreEqual(4, score);

			// This sequqnce matches one of the trie's strings exactly.
			score = trie.ScoreInstructions(new  [] {
				CreatePush(Registers.bp),
				CreateMov(Registers.bp, Registers.sp),
				CreatePush(Registers.si),
				CreatePush(Registers.di) });
			Assert.AreEqual(6, score);

			// A longer sequence runs 'off' the trie, so it should have the same score
			// as the previous test
			score = trie.ScoreInstructions(new [] {
				CreatePush(Registers.bp),
				CreateMov(Registers.bp, Registers.sp),
				CreatePush(Registers.si),
				CreatePush(Registers.di),
				CreatePush(Registers.bx)});
			Assert.AreEqual(6, score);

			// This doesn't exist in the trie at all, so it should score 0.

			score = trie.ScoreInstructions(new [] {
				CreateMov(Registers.ax, Registers.bx) });
			Assert.AreEqual(0, score);
		}
コード例 #15
0
ファイル: TrieTests.cs プロジェクト: Sverdel/AutoComplete
        public void GetTest_Null()
        {
            Trie dictioanry = new Trie();
            dictioanry.Add("a", 100);

            var result = dictioanry.Get("b");

            Assert.IsNull(result, "result must be null");
        }
コード例 #16
0
ファイル: Load_Specs.cs プロジェクト: phatboyg/InternalsTests
        public void Setup()
        {
            _lastNames = SampleData.LastNames;
            _distinctCount = _lastNames.Distinct().Count();

            _trie = new Trie<string>();
            foreach (string lastName in _lastNames)
                _trie.Add(lastName, lastName);
        }
コード例 #17
0
        public void Add_KVP_NullItemIsAdded()
        {
            Trie<string, char, object> trie = new Trie<string, char, object>();

            trie.Add(new KeyValuePair<string, object>("key", null));

            Assert.AreEqual(1, trie.Count);
            Assert.AreEqual(null, trie["key"]);
        }
コード例 #18
0
        public void Add_KVP_ItemIsAdded()
        {
            Trie<string, char, int> trie = new Trie<string, char, int>();

            trie.Add(new KeyValuePair<string, int>("key", 5));

            Assert.AreEqual(1, trie.Count);
            Assert.AreEqual(5, trie["key"]);
        }
コード例 #19
0
        public void Add_ItemIsAdded()
        {
            Trie<string, char, int> trie = new Trie<string, char, int>();

            trie.Add("key", 5);

            Assert.AreEqual(1, trie.Count);
            Assert.AreEqual(5, trie["key"]);
        }
コード例 #20
0
ファイル: TrieTests.cs プロジェクト: Sverdel/AutoComplete
        public void GetTest_UpperCase()
        {
            Trie dictioanry = new Trie();
            dictioanry.Add("a", 100);

            var result = dictioanry.Get("A");

            Assert.IsNotNull(result, "did not found word");
            Assert.AreEqual(1, result.Count(), "Incorrect words count");
        }
コード例 #21
0
ファイル: GeneralTrieTests.cs プロジェクト: 8thbit/Trie
        public void SaveTrie()
        {
            var trie = new Trie<string, FakeData>();
            trie.Add("123", new FakeData { Id = 1, Name = "123", Description = "D 123" });
            trie.Add("1234", new FakeData { Id = 2, Name = "1234", Description = "D 1234" });
            trie.Add("45", new FakeData { Id = 3, Name = "45", Description = "D 45" });

            var origSave = new MemoryStream();
            trie.Save(origSave);
            origSave.Position = 0;

            var reload = new Trie<string, FakeData>();
            reload.Load(origSave);

            var reloadSave = new MemoryStream();
            reload.Save(reloadSave);

            Assert.Equal(origSave.ToArray(), reloadSave.ToArray());
        }
コード例 #22
0
ファイル: TrieTests.cs プロジェクト: kpol/trie
        public void Contains()
        {
            var trie = new Trie<bool>();

            trie.Add("ABC", false);
            trie.Add("AB", false);
            trie.Add("ADE", true);
            trie.Add("ABCDE", false);

            var t = trie as IDictionary<string, bool>;

            Assert.IsTrue(t.Contains(new KeyValuePair<string, bool>("ABC", false)));
            Assert.IsTrue(t.Contains(new KeyValuePair<string, bool>("AB", false)));
            Assert.IsTrue(t.Contains(new KeyValuePair<string, bool>("ADE", true)));
            Assert.IsTrue(t.Contains(new KeyValuePair<string, bool>("ABCDE", false)));

            Assert.IsFalse(t.Contains(new KeyValuePair<string, bool>("X", false)));
            Assert.IsFalse(t.Contains(new KeyValuePair<string, bool>("ADE", false)));
            Assert.IsFalse(t.Contains(new KeyValuePair<string, bool>("ABCD", false)));
        }
コード例 #23
0
ファイル: TrieTests.cs プロジェクト: Sverdel/AutoComplete
        public void GetTest_Multiple()
        {
            Trie dictioanry = new Trie();

            string[] input = new string[4] { "a", "ab", "abc", "bbc" };
            dictioanry.Add(input[0], 100);
            dictioanry.Add(input[1], 200);
            dictioanry.Add(input[2], 300);
            dictioanry.Add(input[3], 400);

            var result = dictioanry.Get("a").ToList();

            Assert.IsNotNull(result, "did not found word");
            Assert.AreEqual(3, result.Count, "Incorrect words count");

            for (int i = 0; i < result.Count; i++)
            {
                Assert.AreEqual(input[2 - i], result[i], "Incorrect word. " + i);
            }
        }
コード例 #24
0
ファイル: PHONELST.cs プロジェクト: davghouse/SPOJ
    // This uses a modified trie, where the trie's Add method returns a bool indicating
    // whether the string added was a prefix of a string already present, or prefixed
    // by a string already present. In either case, the phone numbers aren't consistent,
    // so we short-circuit and just remember to consume the rest of the input.
    public static bool Solve(int phoneNumberCount, string[] phoneNumbers)
    {
        var trie = new Trie();

        for (int i = 0; i < phoneNumberCount; ++i)
        {
            if (trie.Add(phoneNumbers[i]))
                return false; // Some string prefixed another, so it's not consistent.
        }

        return true; // No string prefixed another, so it's consistent.
    }
コード例 #25
0
ファイル: program.cs プロジェクト: staafl/ta-hw-dsa
    /* 3 Write a program that finds a set of words (e.g. 1000 words)
     * in a large text (e.g. 100 MB text file). Print how many times
     * each word occurs in the text.
     * Hint: you may find a C# trie in Internet.
     * */
    static void Main(string[] args)
    {
        var dict = new Dictionary<string, int>();
        var knownCount = new Dictionary<string, int>
        {
            {"foo", 10*1000},
            {"bar", 20*1000},
            {"quux",30*1000},
            {"frob",40*1000},
            {"asdf",50*1000}
        };
        var trie = new Trie<int>();

        var sw = new Stopwatch();

        sw.Start();

        // obviously, I couldn't zip the 100 MB file
        // use "bin\debug\generator.cs" to generate it if you want

        using (var reader = new StreamReader("text.txt"))
            foreach (var word in Words(reader))
                dict[word] = 1 + dict.GetOrDefault(word, 0);

        sw.Stop();
        /*
        foreach (var kvp in knownCount)
            Debug.Assert(dict[kvp.Key] == kvp.Value);
        */

        Console.WriteLine("Using hashtable: " + sw.Elapsed.TotalMilliseconds);

        sw.Reset();
        sw.Start();

        using (var reader = new StreamReader("text.txt"))
            foreach (var word in Words(reader))
                trie.Add(word, 1 + trie.GetOrDefault(word, 0));

        sw.Stop();

        foreach (var kvp in dict)
            Debug.Assert(trie.Find(kvp.Key) == kvp.Value);

        // the trie would probably do much better compared to a hashtable when used on
        // natural text with large amount of repetition and low average word length
        // it is however extremely space inefficient

        // at any rate, I'd be surprised if this implementation could beat .NET's build-in
        // hashtable

        Console.WriteLine("Using trie: " + sw.Elapsed.TotalMilliseconds);
    }
コード例 #26
0
 private static void Main(string[] args) {
   Trie trie = new Trie();
   trie.Add("hell");
   trie.Add("hello");
   trie.Add("help");
   trie.Add("swag");
   trie.Add("swagger");
   trie.Add("trillswag");
   trie.Add("trillswagyolo");
   foreach(string word in trie.Find("hel", 10)) //We only want 10 maximum results back
   {
     Console.WriteLine(word);
   }
 }
コード例 #27
0
        public static void Main(string[] args)
        {
            Trie<char, int> trie = new Trie<char, int>();

            string wordsFileName = "words.txt";
            List<string> wordsList = new List<string>();
            using (var sr = new StreamReader(wordsFileName))
            {
                do
                {
                    string line = sr.ReadLine();
                    if (!trie.ContainsKey(line))
                    {
                        trie.Add(line, 0);
                    }

                    wordsList.Add(line);
                } 
                while (!sr.EndOfStream);
            }

            string textFileName = "text.txt";
            char[] delims = { ' ', '.', '?', ',', '!' };
            using (var sr = new StreamReader(textFileName))
            {
                string text = sr.ReadToEnd();
                string[] words = text.Split(delims, StringSplitOptions.RemoveEmptyEntries);
                foreach (var word in words)
                {
                    if (trie.ContainsKey(word))
                    {
                        trie[word]++;
                    }
                }
            }

            foreach (var word in wordsList)
            {
                Console.WriteLine("{0} => {1} times", word, trie[word]);
            }
        }
コード例 #28
0
        private void loadVerbDic()
        {
            if (!verbDic.IsEmpty())
            {
                return;
            }

            var sLines = loadData(VERB_FILE_NAME);

            foreach (string sLine in sLines)
            {
                var arr = sLine.Split('\t');
                try
                {
                    verbDic.Add(arr[0].Trim(), new Verb(arr[1].Trim(), arr[2].Trim()));
                }
                catch
                {
                    //log.Warn("Verb " + sLine + " cannot be added. Is it duplicated?");
                }
            }
        }
コード例 #29
0
        public void TestTrie()
        {
            var    trie = new Trie(true);
            string pattern;

            for (int i = 0; i < TriePatterns.Length; i++)
            {
                trie.Add(TriePatterns[i]);
            }

            for (int i = 0; i < TestCases.Length; i++)
            {
                int    index = trie.Search(TestCases[i].ToCharArray(), out pattern);
                string substr;

                Assert.IsTrue(index != -1, "Search failed for {0}", TestCases[i]);

                substr = TestCases[i].Substring(index);

                Assert.IsTrue(substr.StartsWith(pattern, StringComparison.OrdinalIgnoreCase), "Search returned wrong index for {0}", TestCases[i]);
            }
        }
コード例 #30
0
        public void WhenStackDoesNotHaveNodes_ThenItIsEmpty()
        {
            var sanpleText = @"O Canada!
                                Our home and native land!
                                True patriot love in all of us command.
                                With glowing hearts we see thee rise,
                                The True North strong and free!
                                From far and wide,
                                O Canada, we stand on guard for thee.
                                God keep our land glorious and free!
                                O Canada, we stand on guard for thee.
                                O Canada, we stand on guard for thee.";

            var trie = new Trie <string>();

            foreach (string word in sanpleText.Split(' '))
            {
                trie.Add(word, word);
            }

            Assert.IsTrue(trie.Matcher.Next('C'));
            Assert.IsTrue(trie.Matcher.Next('a'));
        }
コード例 #31
0
        public static void Main(string[] args)
        {
            var words = new string[] { "dog", "deer", "deal" };

            var query = Console.ReadLine();

            // calling fast and easy, not using the hint of efficient data structure
            var fastEasyResults = FastAndEasy(query, words);

            Console.WriteLine($"Fast and easy results: \n[{string.Join(",", fastEasyResults)}]");

            // store them in a trie to remove unnecessary storage and for faster searching
            Trie trie = new Trie();

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

            var trieStoreResults = trie.Autocomplete(query);

            Console.WriteLine($"Trie store results: \n[{string.Join(",", trieStoreResults)}]");
        }
コード例 #32
0
        public Form1()
        {
            InitializeComponent();
            T = new Trie();

            if (File.Exists("wordsEn.txt"))
            {
                /*using (StreamReader sr = new StreamReader("wordsEn.txt"))
                 * {
                 *  while (!sr.EndOfStream)
                 *  {
                 *      // Read the stream to a string, and write the string to the console.
                 *      String line = sr.ReadLine();
                 *      T.Add(line);
                 *  }
                 * }*/
                string[] archive = File.ReadAllLines("wordsEn.txt");
                foreach (string s in archive)
                {
                    T.Add(s);
                }
            }
        }
コード例 #33
0
ファイル: IntTrieTest.cs プロジェクト: smorken/generictrie
        public override void AllocateTrie()
        {
            Random rnd = new Random();

            for (int i = 0; i < Count; i++)
            {
                int   Length         = rnd.Next(MinLength, MaxLength);
                int[] RandomIntegers = new int[Length];
                for (int j = 0; j < Length; j++)
                {
                    RandomIntegers[j] = rnd.Next(0, DistinctValues);
                }
                if (!intTrie.ContainsKey(RandomIntegers))
                {
                    Keys.Add(RandomIntegers);
                    intTrie.Add(RandomIntegers, i);
                }
                else
                {
                    collisions++;
                }
            }
        }
コード例 #34
0
    public string[] GetAllHealthCenters(string prefixText, int count)
    {
        Trie hfTrie = (Trie)Context.Cache["HFTrie"];

        if (hfTrie == null)
        {
            String query = @"select ""ID"", ""NAME"" from ""HEALTH_FACILITY"" Where ""IS_ACTIVE"" = true "; // ""NAME"" ilike '" + prefixText.ToLower() + "%'";

            //cmd.Parameters.AddWithValue("@myParameter", "%" + prefixText + "%");

            DataTable dt = DBManager.ExecuteReaderCommand(query, CommandType.Text, null);

            hfTrie = new Trie();
            foreach (DataRow row in dt.Rows)
            {
                hfTrie.Add(row[1].ToString() + "," + row[0].ToString());
            }
            Context.Cache["HFTrie"] = hfTrie;
        }


        //Then return List of string(txtItems) as result
        List <string> txtItems = new List <string>();

        // String dbValues;
        txtItems = hfTrie.GetCompletionList(prefixText, count);
        List <string> list = new List <string>();

        for (int i = 0; i < txtItems.Count; i++)
        {
            int    indx = txtItems[i].IndexOf(",");
            string f    = txtItems[i].Substring(0, indx);
            string l    = txtItems[i].Substring(indx + 1);
            list.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(f, l));
        }
        return(list.ToArray());
    }
コード例 #35
0
        //private string[] loadData(string resourceName)
        //{
        //    //try
        //    //{
        //        return File.ReadAllLines(dataPath + resourceName).Where(q => !string.IsNullOrWhiteSpace(q)).ToArray();
        //    //}
        //    //catch (Exception ex) {
        //    //    System.Console.WriteLine(ex.ToString());
        //    //}

        //   // return null;
        //}

        private void loadVerbDic()
        {
            if (!verbDic.IsEmpty())
            {
                return;
            }

            //string[] sLines = loadData(VERB_FILE_NAME);
            //string sql = string.Empty;
            var ls = new PS_VERB_FAOpration();

            foreach (var item in ls.GetAll())
            {
                //string[] arr = sLine.Split('\t');
                try
                {
                    verbDic.Add(item.Val1.Trim(), new Verb(item.Val2.Trim(), item.Val3.Trim()));
                }
                catch
                {
                    //log.Warn("Verb " + sLine + " cannot be added. Is it duplicated?");
                }
            }
        }
コード例 #36
0
ファイル: Q212.cs プロジェクト: zzclynn/csharp-leet
    public IList <string> FindWords(char[,] board, string[] words)
    {
        Trie trie = new Trie();

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

        int row = board.GetLength(0);
        int col = board.GetLength(1);

        bool[,] visited = new bool[row, col];
        List <string> result = new List <string>();

        for (int r = 0; r < row; r++)
        {
            for (int c = 0; c < col; c++)
            {
                Dfs(board, visited, r, c, "", trie, result);
            }
        }
        return(result.Distinct().ToList());
    }
コード例 #37
0
ファイル: UnitTest1.cs プロジェクト: adimanav/cdk
        public void TestTrie()
        {
            var map = new Trie <string>();

            for (int i = 0; i < numValues; i++)
            {
                var tmp = i + string.Empty;
                map.Add(tmp, tmp);
            }

            for (int i = 0; i < numValues; i++)
            {
                var tmp = i + string.Empty;
                Assert.AreEqual(tmp, map.Get(tmp));
            }

            for (int i = 0; i < numValues; i++)
            {
                var tmp = i + string.Empty;
                map.Remove(tmp);
            }

            Assert.AreEqual(0, map.Count);
        }
コード例 #38
0
    public IList <string> FindWords(char[][] board, string[] words)
    {
        IList <string> res = new List <string>();

        if (board.GetLength(0) == 0 || board[0].Length == 0 || words.Length == 0)
        {
            return(res);
        }

        Trie trie = new Trie();

        foreach (string str in words)
        {
            trie.Add(str);
        }

        HashSet <string> strs = new HashSet <string>();
        int m = board.GetLength(0), n = board[0].Length;

        bool[,] used = new bool[m, n];

        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                FindWords(strs, board, used, i, j, trie, trie.Root(), new StringBuilder());
            }
        }

        foreach (string str in strs)
        {
            res.Add(str);
        }

        return(res);
    }
コード例 #39
0
ファイル: trierec.cs プロジェクト: MilindKhadloya/Milindk
    public void SimpleTrie(Trie trie)
    {
        String [] input = { "champ", "check", "checked", "champion", "cat", "cow", "bat", "battle" };
        foreach (String str in input)
        {
            trie.Add(str);
        }

        foreach (String str in input)
        {
            Search(str, trie.Search(str), true);
        }

        Console.WriteLine();
        String temp = "ch";

        Console.WriteLine("Matching words for {0}", temp);
        trie.GetWords(temp);

        Console.WriteLine();
        temp = "M";
        Console.WriteLine("Matching words for {0}", temp);
        trie.GetWords(temp);
    }
コード例 #40
0
        internal static Trie <char, List <string> > Load(string binaryFilePath, string positionTriePath)
        {
            var ret = new Trie <char, List <string> >();

            using (StreamReader strmReader = File.OpenText(positionTriePath))
                using (var binReader = new BinaryReader(File.OpenRead(binaryFilePath)))
                {
                    string line;
                    int    c = 0;
                    while ((line = strmReader.ReadLine()) != null)
                    {
                        string word     = line.Substring(0, line.IndexOf(" "));
                        long   position = Convert.ToInt64(line.Substring(line.IndexOf(" ") + 1));

                        binReader.BaseStream.Position = position;

                        int howMany = binReader.ReadInt32();

                        var list = new List <string>();
                        for (int i = 0; i < howMany; i++)
                        {
                            list.Add(binReader.ReadString());
                        }

                        list = FileCreator.ReduceNumberOfNgrams(list, new int[] { 0, 1, 350, 245, 105 });

                        ret.Add(word, list);
                        if (++c % 10000 == 0)
                        {
                            Console.WriteLine(c);
                        }
                    }
                }

            return(ret);
        }
コード例 #41
0
        public void TestMultiMatchDifferentStart()
        {
            var words = new string[] { "성기사", "기사" };

            Trie <int> trie = new Trie <int>();

            foreach (string w in words)
            {
                trie.Add(w, w.Length);
            }

            trie.Build();

            var test   = "나는 성기사다 바보야.";
            var founds = trie.Find(test).ToArray();

            Assert.AreEqual(2, founds.Length);

            Assert.AreEqual(5, founds[0].end);
            Assert.AreEqual(3, founds[0].value);

            Assert.AreEqual(5, founds[1].end);
            Assert.AreEqual(2, founds[1].value);
        }
コード例 #42
0
ファイル: TrieTests.cs プロジェクト: przpl/CryptZip
        public void IndexOf_AddsTwoSymbolsElements_Added()
        {
            var trie = new Trie();

            trie.Add(0);
            trie.Add(1);
            trie.Add(2);

            TrieNode zeroNode = trie.FindRootChild(0);

            trie.Add(zeroNode, 0);
            Assert.AreEqual(4, trie.FindChild(zeroNode, 0).Index);

            TrieNode oneNode = trie.FindRootChild(1);

            trie.Add(oneNode, 1);
            Assert.AreEqual(5, trie.FindChild(oneNode, 1).Index);

            TrieNode twoNode = trie.FindRootChild(2);

            trie.Add(twoNode, 2);
            Assert.AreEqual(6, trie.FindChild(twoNode, 2).Index);
        }
コード例 #43
0
ファイル: TrieTest.cs プロジェクト: vishwa89/sbs-app
        public void VerifyTrieCharacterRangeInvalidInput()
        {
            Trie <string> trie = new Trie <string>(k => k.ToLowerInvariant());

            trie.Add("Abcd(");
        }
コード例 #44
0
 public bool Add(IEnumerable <char> key)
 {
     // Value does not matter
     return(trie.Add(key, 0));
 }
コード例 #45
0
ファイル: TrieTests.cs プロジェクト: kpol/trie
        public void KeysValues()
        {
            var trie = new Trie<bool>();

            trie.Add("ABC", false);
            trie.Add("AB", true);
            trie.Add("ADE", false);
            trie.Add("ABCDE", true);

            Assert.IsTrue(new[] { "AB", "ABC", "ABCDE", "ADE" }.SequenceEqual(trie.Keys.OrderBy(s => s)));
            Assert.IsTrue(new[] { false, false, true, true }.SequenceEqual(trie.Values.OrderBy(s => s)));
        }
コード例 #46
0
 public void Add(string key, TValue value)
 {
     m_trie.Add(key);
     m_values[m_trie.GetStringId(key)] = value;
 }
コード例 #47
0
        public void AddEmptyWord()
        {
            Trie<string> Words = new Trie<string>();

            int result = Words.Add("", "");
コード例 #48
0
        public string run(string input)
        {
            input = normalization(input).Trim();

            if (string.IsNullOrEmpty(input))
            {
                return("");
            }

            //Integer or english
            if (Utils.IsEnglish(input) || Utils.IsNumber(input) || input.Length <= 2)
            {
                return(input);
            }

            if (enableCache)
            {
                var stm = cache.ContainsKey(input);
                if (!string.IsNullOrEmpty(stm))
                {
                    return(stm);
                }
            }

            var s = GetMokassarStem(input);

            if (NormalizeValidation(input, false))
            {
                //stemList.add(input/*, "[فرهنگ لغت]"*/);
                if (enableCache)
                {
                    cache.Add(input, input);
                }
                return(input);
            }

            if (!string.IsNullOrEmpty(s))
            {
                //addToLog(s/*, "[جمع مکسر]"*/);
                //stemList.add(s);
                if (enableCache)
                {
                    cache.Add(input, s);
                }
                return(s);
            }

            var stemList  = new List <string>();
            var terminate = PatternMatching(input, stemList);

            if (enableVerb)
            {
                s = getVerb(input);
                if (!string.IsNullOrEmpty(s))
                {
                    stemList.Clear();
                    stemList.Add(s);
                }
            }

            if (stemList.Count == 0)
            {
                if (NormalizeValidation(input, true))
                {
                    //stemList.add(input, "[فرهنگ لغت]");
                    if (enableCache)
                    {
                        cache.Add(input, input); //stemList.get(0));
                    }
                    return(input);               //stemList.get(0);
                }

                stemList.Add(input); //, "");
            }

            if (terminate && stemList.Count > 1)
            {
                return(nounValidation(stemList));
            }

            const int I = 0;

            if (patternCount != 0)
            {
                if (patternCount < 0)
                {
                    stemList.Reverse();
                }
                else
                {
                    stemList.Sort();
                }

                while (I < stemList.Count && stemList.Count > Math.Abs(patternCount))
                {
                    stemList.RemoveAt(I);
                }
                //patternList.remove(I);
            }

            if (enableCache)
            {
                cache.Add(input, stemList[0]);
            }
            return(stemList[0]);
        }
コード例 #49
0
        public void Add_GivenNullString_ThrowsException()
        {
            var trie = new Trie();

            Assert.Throws <NullReferenceException>(() => trie.Add(null));
        }
コード例 #50
0
ファイル: FST.cs プロジェクト: yuyingzhang/Sdl-Community
        /// <summary>
        /// Attempts to make the automaton deterministic. Note that it's never fully deterministic on
        /// a single band, since one symbol may always be eps.
        /// </summary>
        public void MakeDeterministic()
        {
            if (!StateExists(_StartState))
            {
                throw new Exception("No start state");
            }

            // the test is cheaper than going through the prep steps
            if (IsDeterministic())
            {
                return;
            }

            // TODO test other required features, such as reachability, productivity of states and
            //  existance of start/final states

            // TODO eps-free prop should be memoized on FST level to avoid multiple runs of eps elim
            EliminateEpsilonTransitions();
            SortTransitions();

            // starting with the start state, create a new joint state for each state cluster which
            //  can be reached by multiple transitions with the same label. Copy the outgoing
            //  transitions from each prior target state to outgoing transitions from the new cluster
            //  state.

            // schedule each state for processing, but start with the start state
            List <int> processedStates = new List <int>();

            processedStates.Add(_StartState);
            processedStates.AddRange(_States.Where(kvp => kvp.Key != _StartState).Select(s => s.Key));
            int current = 0;


            // need to remember the cluster states and which original states they represent
            Trie <int, int> clusterStates = new Trie <int, int>();

            while (current < processedStates.Count)
            {
                int currentState = processedStates[current];
                ++current;

                State state = _States[currentState];

                if (state.Transitions.Count < 2)
                {
                    continue;
                }

                bool wasModified = false;

                int firstEqualTransition    = 0;
                int previousTransitionCount = state.TransitionCount;
                while (firstEqualTransition < previousTransitionCount)
                {
                    int lastEqualTransition = firstEqualTransition + 1;
                    // NOTE that the transitions are sorted by src state, input, output, trg state
                    while (lastEqualTransition < previousTransitionCount &&
                           state.Transitions[lastEqualTransition].Input.Equals(state.Transitions[firstEqualTransition].Input) &&
                           state.Transitions[lastEqualTransition].Output.Equals(state.Transitions[firstEqualTransition].Output))
                    {
                        ++lastEqualTransition;
                    }

                    if (lastEqualTransition > firstEqualTransition + 1)
                    {
                        // found a cluster of equal (i.e. non-deterministic) labels
                        //  for transitions [first..last[


                        // build the cluster state label
                        List <int> label = new List <int>();
                        for (int p = firstEqualTransition; p < lastEqualTransition; ++p)
                        {
                            // usually multiple same target states are not possible, but
                            //  we need to be sure:
                            System.Diagnostics.Debug.Assert(!label.Contains(state.Transitions[p].Target));
                            label.Add(state.Transitions[p].Target);
                        }
                        label.Sort();

                        // search whether the cluster state alread exists:

                        int clusterStateNumber = -1;
                        int cluster;
                        if (!clusterStates.Contains(label, out cluster))
                        {
                            // cluster state not found. Add new state to automaton
                            clusterStateNumber = AddState();
                            State clusterState = _States[clusterStateNumber];

                            // need to check the new state's transitions later
                            processedStates.Add(clusterStateNumber);

                            clusterStates.Add(label, clusterStateNumber);

                            // it's final if any of its clustered states is final
                            // NOTE we don't need to care about going back to the initial state
                            //  as the only initial state in the DFA will be the original initial
                            //  state
                            clusterState.IsFinal = label.Any(s => IsFinal(s));

                            // copy all outgoing transitions of all the clustered states to the new state
                            foreach (int cs in label)
                            {
                                State cState = _States[cs];
                                foreach (FSTTransition t in cState.Transitions)
                                {
                                    clusterState.AddTransition(t.Target, new Label(t.Input), new Label(t.Output));
                                }
                            }

                            // for later once we determinize the new cluster state
                            clusterState.SortTransitions();
                        }
                        else
                        {
                            clusterStateNumber = cluster;
                        }

                        // now create a new single transition with the ambiguous label and
                        //  let it point to the new cluster state
                        state.AddTransition(clusterStateNumber, new Label(state.Transitions[firstEqualTransition].Input),
                                            new Label(state.Transitions[firstEqualTransition].Output));

                        // and invalidate all prior transitions (set to NULL, we cleanup later)
                        for (int p = firstEqualTransition; p < lastEqualTransition; ++p)
                        {
                            state.Transitions[p] = null;
                        }

                        wasModified = true;
                    }

                    firstEqualTransition = lastEqualTransition;
                }

                if (wasModified)
                {
                    // transition list will contain NULL elements which we have to remove
                    state.Transitions.RemoveAll(t => t == null);
                }
            }

            // the above may leave unproductive and/or unreachable states:
            Clean();
            System.Diagnostics.Debug.Assert(IsDeterministic());
        }
コード例 #51
0
        public void Add_GivenEmptyString_ThrowsException()
        {
            var trie = new Trie();

            Assert.Throws <ArgumentException>(() => trie.Add(""));
        }
コード例 #52
0
        public string BuildTrie(byte trieListSize, int updateTime)
        {
            trie = new Trie(trieListSize);
            // Manually force a GC so we have maximum memory for the trie
            // NOTE that this is pretty slow, but since this function is only called once (and manually) it's no problem
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            var            fullPath       = localResource.RootPath + WIKI_TITLES;
            TrieStatEntity stats          = new TrieStatEntity(0, "");
            const int      MAX_GC_TIME    = 100000;
            int            timeTillUpdate = updateTime;
            int            timeTillGC     = MAX_GC_TIME;
            float          startMemory    = ramPerformance.NextValue();
            Stopwatch      watch          = new Stopwatch();

            watch.Start();
            try
            {
                using (StreamReader reader = new StreamReader(fullPath))
                {
                    while (!reader.EndOfStream)
                    {
                        string title = reader.ReadLine();
                        trie.Add(title);
                        stats.UpdateLastTitle(title);
                        timeTillUpdate--;
                        timeTillGC--;
                        if (timeTillGC <= 0)
                        {
                            GC.Collect();
                            GC.WaitForPendingFinalizers();
                            timeTillGC = MAX_GC_TIME;
                        }
                        if (timeTillUpdate <= 0)
                        {
                            timeTillUpdate = updateTime;
                            if (ramPerformance.NextValue() <= MIN_MB_FOR_TRIE)
                            {
                                // always update on error
                                connection.UpdateTrieStats(stats);
                                return("Trie had to stop building due to memory constraints. Last word added: " + stats.LastTitle);
                            }
                        }
                    }
                }
            }
            catch (IOException)
            {
                return("Couldn't build trie due to IOException. Make sure to download the data file before building the trie!");
            }
            watch.Stop();
            long buildTime = watch.ElapsedMilliseconds;

            watch.Reset();
            watch.Start();
            string[] found = trie.FindWords("test", 5);
            watch.Stop();
            long   findTime = watch.ElapsedTicks;
            string output   = string.Join(",", found);

            connection.UpdateTrieStats(stats);
            float memoryDiff = startMemory - ramPerformance.NextValue();

            return("Built trie in " + buildTime + "ms ||| searching for 5 words starting with 'test': " + output + " in " + findTime + " ticks ||| Ram used: " + memoryDiff);
        }
コード例 #53
0
ファイル: TrieTests.cs プロジェクト: kpol/trie
        public void ContainsKey()
        {
            var trie = new Trie<bool>();

            trie.Add("ABC", false);
            trie.Add("AB", false);
            trie.Add("ADE", false);
            trie.Add("ABCDE", false);

            Assert.IsTrue(trie.ContainsKey("ABC"));
            Assert.IsTrue(trie.ContainsKey("AB"));
            Assert.IsTrue(trie.ContainsKey("ADE"));
            Assert.IsTrue(trie.ContainsKey("ABCDE"));

            Assert.IsFalse(trie.ContainsKey("A"));
            Assert.IsFalse(trie.ContainsKey("AC"));
            Assert.IsFalse(trie.ContainsKey("ABCD"));
        }
コード例 #54
0
        public void AddingAfterRemovingEverything()
        {
            var trie = new Trie();

            var words = new string[]
            {
                "One", "one", "oNe", "two", "hello",
                "test", "here", "there", "?!??!?!?!?",
                "VeryVeryVeryLoooooooooooooooooong",
                ".k[2c3-9024g-u,9weg,ouimwt", "3q2tgwadh",
                "`+rv`+*1v+vt23*1`vt*1v", "!@#)(*^$!%@_",
                "  bum  ", "  bam  ", "  bum  bam  ",
                "1", "12", "123", "1234", "12345", "123456"
            };


            for (int i = 0; i < words.Length; i++)
            {
                if (trie.Contains(words[i]))
                {
                    Assert.Fail();
                }

                trie.Add(words[i]);

                if (!trie.Contains(words[i]))
                {
                    Assert.Fail();
                }
            }

            if (trie.Count != words.Length)
            {
                Assert.Fail();
            }

            for (int i = 0; i < words.Length; i++)
            {
                if (!trie.Remove(words[i]))
                {
                    Assert.Fail();
                }

                if (trie.Contains(words[i]))
                {
                    Assert.Fail();
                }
            }

            if (trie.Count != 0)
            {
                Assert.Fail();
            }

            for (int i = 0; i < words.Length; i++)
            {
                if (trie.Contains(words[i]))
                {
                    Assert.Fail();
                }

                trie.Add(words[i]);

                if (!trie.Contains(words[i]))
                {
                    Assert.Fail();
                }
            }

            Assert.IsTrue(trie.Count == words.Length);
        }
コード例 #55
0
        public void CheckIfSortedAfterAddingAndRemovingSomeWords()
        {
            var trie = new Trie();

            var words = new string[]
            {
                "One", "one", "oNe", "two", "hello",
                "test", "here", "there", "?!??!?!?!?",
                "VeryVeryVeryLoooooooooooooooooong",
                ".k[2c3-9024g-u,9weg,ouimwt", "3q2tgwadh",
                "`+rv`+*1v+vt23*1`vt*1v", "!@#)(*^$!%@_",
                "  bum  ", "  bam  ", "  bum  bam  ",
                "1", "12", "123", "1234", "12345", "123456"
            };


            for (int i = 0; i < words.Length; i++)
            {
                if (trie.Contains(words[i]))
                {
                    Assert.Fail();
                }

                trie.Add(words[i]);

                if (!trie.Contains(words[i]))
                {
                    Assert.Fail();
                }
            }

            if (trie.Count != words.Length)
            {
                Assert.Fail();
            }

            int removedWords = 0;

            for (int i = 0; i < words.Length; i += 2)
            {
                if (trie.Remove(words[i]))
                {
                    removedWords++;
                }
                else
                {
                    Assert.Fail();
                }

                if (trie.Contains(words[i]))
                {
                    Assert.Fail();
                }
            }

            var previousWord = string.Empty;

            foreach (var word in trie)
            {
                if (string.CompareOrdinal(previousWord, word) > 0)
                {
                    Assert.Fail();
                }

                //System.Diagnostics.Trace.WriteLine(word);

                previousWord = word;
            }

            Assert.IsTrue(trie.Count == words.Length - removedWords);
        }
コード例 #56
0
        /// <summary>
        /// 直列化した表を作成
        /// </summary>
        /// <returns></returns>
        public StringDictionary <T> Build()
        {
            var trie          = new Trie();
            var codes_builder = new DoubleArrayCodesBuilder();

            foreach (var key_value in m_key_value)
            {
                trie.Add(key_value.Key);
                codes_builder.Add(key_value.Key);
            }


            var base_array = new BaseCheckArray()
            {
                0, 1
            };
            var check_array = new BaseCheckArray()
            {
                0, 1
            };
            var codes            = codes_builder.Build();
            var state_dictionary = new Dictionary <Guid, int>();

            // 現在のステート
            int?current_state = null;

            foreach (var node in trie.GetEnumerator())
            {
                /// 一番最初なら初期値設定
                if (!current_state.HasValue)
                {
                    state_dictionary[node.Guid] = 1;
                }
                current_state = state_dictionary[node.Guid];



                /// baseの設定
                /// 終端なのでvalueのindexを反転して設定
                if ((node.Value == '\x0000') && node.Children.Count == 0)
                {
                    setToList(ref base_array, current_state.Value, -node.Index.Value);
                    continue;
                }



                /// 設定するbaseの検索 設定
                int  current_base = getFromList(base_array, current_state.Value);
                bool is_colided   = false;
                do
                {
                    is_colided = false;
                    foreach (var child in node.Children)
                    {
                        var code = codes[child.Value];
                        Debug.Assert(0 < code);
                        int next = current_base + code;
                        if (getFromList(check_array, next) != 0)
                        {
                            is_colided    = true;
                            current_base += 1;
                            break;
                        }
                    }
                } while (is_colided);
                setToList(ref base_array, current_state.Value, current_base);

                /// 子の設定
                foreach (var child in node.Children)
                {
                    var code = codes[child.Value];
                    Debug.Assert(0 < code);
                    int next = current_base + code;

                    state_dictionary[child.Guid] = next;
                    if (getFromList(check_array, next) == 0)
                    {
                        // 未使用
                        setToList(ref check_array, next, current_state.Value);
                    }
                    else
                    {
                        // 衝突時処理
                        resolveCollision(current_state.Value, node, state_dictionary, codes, base_array, check_array);
                    }
                }
            }


            return(new StringDictionary <T>(
                       new DoubleArray(base_array, check_array, codes),
                       m_key_value.Values));;
        }
コード例 #57
0
        public ViewModelTrie()
        {
            var tupleTreeAndTime = new DictionaryWords("../../Model/Words.txt").CreateTree();

            Tree      = tupleTreeAndTime.Item1;
            OpHistory = new ObservableCollection <OpHistoryItem>();
            var massiveRussian = new MassiveChars("Russian").GetMassiveChars();
            var massiveEnglish = new MassiveChars("English").GetMassiveChars();

            OpHistory.Add(new OpHistoryItem("Время создания дерева из " + tupleTreeAndTime.Item3 + " слов - " + tupleTreeAndTime.Item2 + " ms."));

            Add = new DelegateCommand(() =>
            {
                var beep = new MediaPlayer();
                beep.Open(new Uri("../../Music/beep.mp3", UriKind.RelativeOrAbsolute));
                beep.Play();
                if (string.IsNullOrWhiteSpace(AddValueF) && !string.IsNullOrWhiteSpace(AddValueS))
                {
                    OpHistory.Add(new OpHistoryItem("Вы не ввели слово на английском, которое хотели добавить!"));
                }
                else if (!string.IsNullOrWhiteSpace(AddValueF) && string.IsNullOrWhiteSpace(AddValueS))
                {
                    OpHistory.Add(new OpHistoryItem("Вы не ввели перевод слова!"));
                }
                else if (string.IsNullOrWhiteSpace(AddValueF) && string.IsNullOrWhiteSpace(AddValueS))
                {
                    OpHistory.Add(new OpHistoryItem("Вы ничего не ввели!"));
                }
                else
                {
                    var spForAdd = new Stopwatch();
                    if (CheckerForEnglishAndRussian(AddValueF.ToCharArray(), AddValueS.ToCharArray()))
                    {
                        long time = 0;
                        if (!Tree.ContainsValue(AddValueF))
                        {
                            spForAdd.Start();
                            Tree.Add(AddValueF, AddValueS);
                            spForAdd.Stop();
                            OpHistory.Add(new OpHistoryItem("Слово \"" + AddValueF + "\" записано в словарь."));
                        }
                        else
                        {
                            spForAdd.Start();
                            Tree.Remove(AddValueF);
                            Tree.Add(AddValueF, AddValueS);
                            spForAdd.Stop();
                            OpHistory.Add(new OpHistoryItem("Слово \"" + AddValueF + "\" перезаписано."));
                        }
                        OpHistory.Add(new OpHistoryItem("Время добавления/переписывания составило - " + spForAdd.ElapsedTicks + " тиков."));
                        RaisePropertyChanged("OpHistory");
                    }
                    else
                    {
                        OpHistory.Add(new OpHistoryItem("Первое слово должно быть Английским, а второе Русским!"));
                    }
                }
            });
            Delete = new DelegateCommand(() =>
            {
                var beep = new MediaPlayer();
                beep.Open(new Uri("../../Music/beep.mp3", UriKind.RelativeOrAbsolute));
                beep.Play();
                if (string.IsNullOrWhiteSpace(DeleteValue))
                {
                    OpHistory.Add(new OpHistoryItem("Вы ничего не ввели!"));
                }
                else
                {
                    if (CheckerForEnglish(DeleteValue.ToCharArray()))
                    {
                        if (Tree.ContainsValue(DeleteValue))
                        {
                            var spForDelete = new Stopwatch();
                            spForDelete.Start();
                            Tree.Remove(DeleteValue);
                            spForDelete.Stop();
                            OpHistory.Add(new OpHistoryItem("Слово \"" + DeleteValue + "\" удалено из словаря."));
                            OpHistory.Add(new OpHistoryItem("Время удаления - " + spForDelete.ElapsedTicks + " тиков"));
                        }
                        else
                        {
                            OpHistory.Add(new OpHistoryItem("Слово \"" + DeleteValue + "\" не найдено в словаре."));
                        }
                        RaisePropertyChanged("OpHistory");
                    }
                    else
                    {
                        OpHistory.Add(new OpHistoryItem("Это не английское слово \"" + DeleteValue + "\"."));
                    }
                }
            });
            Translate = new DelegateCommand(() =>
            {
                var beep = new MediaPlayer();
                beep.Open(new Uri("../../Music/beep.mp3", UriKind.RelativeOrAbsolute));
                beep.Play();
                if (string.IsNullOrWhiteSpace(TranslateValue))
                {
                    OpHistory.Add(new OpHistoryItem("Вы ничего не ввели!"));
                }
                else
                {
                    if (CheckerForEnglish(TranslateValue.ToCharArray()))
                    {
                        if (Tree.ContainsValue(TranslateValue))
                        {
                            var spForTranslate = new Stopwatch();
                            spForTranslate.Start();
                            var russianTranslate = Tree.GetValue(TranslateValue);
                            spForTranslate.Stop();
                            OpHistory.Add(new OpHistoryItem("Перевод слова " + TranslateValue + " - " + russianTranslate));
                            OpHistory.Add(new OpHistoryItem("Время перевода - " + spForTranslate.ElapsedTicks + " тиков."));
                        }
                        else
                        {
                            OpHistory.Add(new OpHistoryItem("Слово \"" + TranslateValue + "\" не найдено в словаре."));
                        }
                        RaisePropertyChanged("OpHistory");
                    }
                    else
                    {
                        OpHistory.Add(new OpHistoryItem("Это не английское слово \"" + TranslateValue + "\"."));
                    }
                }
            });
            Tree.GetValue("apple");
            Tree.Remove("warm");
            Tree.Remove("yourself");
            Tree.Add("Test", "Тест");
        }
コード例 #58
0
        public void TestTrie()
        {
            Trie <char, string> wordIndex = new Trie <char, string>(new LettersDomain());

            wordIndex.Add("a", "a");
            wordIndex.Add("aa", "aa");
            wordIndex.Add("ab", "ab");
            wordIndex.Add("ab", "ab2");
            wordIndex.Add("abc", "abc");
            wordIndex.Add("b", "b");
            wordIndex.Add("ba", "ba");
            wordIndex.Add("ba", "ba2");
            wordIndex.Add("ba", "ba3");
            wordIndex.Add("ba", "ba4");
            wordIndex.Add("c", "c");
            wordIndex.Add("cd", "cd");

            int max;

            //PrintTreeRecursive(wordIndex.root, -1, "");
            //Console.WriteLine(" -------------------------------- ");

            //ITrieEnumerator<char, IList<string>> iterator = wordIndex.All1;

            //Console.WriteLine(" MoveNext: -------------------------------- ");

            //while (iterator.MoveNext())
            //{
            //    Console.WriteLine("{0}: {1}", new string(new List<char>(iterator.Current.Key).ToArray()), iterator.Current.Value[0]);
            //}

            //Console.WriteLine(" MovePrevious: -------------------------------- ");

            //while (iterator.MovePrevious())
            //{
            //    Console.WriteLine("{0}: {1}", new string(new List<char>(iterator.Current.Key).ToArray()), iterator.Current.Value[0]);
            //}

            //Console.WriteLine(" MoveNext: -------------------------------- ");

            //while (iterator.MoveNext())
            //{
            //    Console.WriteLine("{0}: {1}", new string(new List<char>(iterator.Current.Key).ToArray()), iterator.Current.Value[0]);
            //}

            //Console.WriteLine(" MovePrevious: -------------------------------- ");

            //int max = 6;
            //while (--max > 0 && iterator.MovePrevious())
            //{
            //    Console.WriteLine("{0}: {1}", new string(new List<char>(iterator.Current.Key).ToArray()), iterator.Current.Value[0]);
            //}

            //Console.WriteLine(" MoveNext: -------------------------------- ");

            //max = 4;
            //while (--max > 0 && iterator.MoveNext())
            //{
            //    Console.WriteLine("{0}: {1}", new string(new List<char>(iterator.Current.Key).ToArray()), iterator.Current.Value[0]);
            //}

            //Console.WriteLine(" MovePrevious: -------------------------------- ");

            //max = 5;
            //while (--max > 0 && iterator.MovePrevious())
            //{
            //    Console.WriteLine("{0}: {1}", new string(new List<char>(iterator.Current.Key).ToArray()), iterator.Current.Value[0]);
            //}

            Console.WriteLine(" -------------------------------- ");
            Console.WriteLine(" -------------------------------- ");

            ITrieEnumerator <char, string> iterator2 = wordIndex.All;

            while (iterator2.MoveNext())
            {
                Console.WriteLine(iterator2.Current.Value);
            }

            Console.WriteLine(" -------------------------------- ");

            while (iterator2.MovePrevious())
            {
                Console.WriteLine(iterator2.Current.Value);
            }

            Console.WriteLine(" -------------------------------- ");

            while (iterator2.MoveNext())
            {
                Console.WriteLine(iterator2.Current.Value);
            }

            Console.WriteLine(" -------------------------------- ");

            max = 6;
            while (--max > 0 && iterator2.MovePrevious())
            {
                Console.WriteLine(iterator2.Current.Value);
            }

            Console.WriteLine(" MoveNext: -------------------------------- ");

            max = 4;
            while (--max > 0 && iterator2.MoveNext())
            {
                Console.WriteLine(iterator2.Current.Value);
            }

            Console.WriteLine(" MovePrevious: -------------------------------- ");

            max = 5;
            while (--max > 0 && iterator2.MovePrevious())
            {
                Console.WriteLine(iterator2.Current.Value);
            }
        }
コード例 #59
0
        public static void Test()
        {
            var testcase = StringSearchTestCase.New(50, 10, 100000);
            Stopwatch stopWatch = new Stopwatch();

            //searcher
            StringSearcher searcher = new StringSearcher();
            stopWatch.Start();
            testcase.TestDictionary.WithEach(x =>
            {
                searcher.Add(x, x);
            });
            stopWatch.Stop();
            Debug.WriteLine("searcher init elapsed ms {0}", stopWatch.ElapsedMilliseconds);

            stopWatch.Reset();
            stopWatch.Start();
            var searchermatches = searcher.FindMatches(testcase.TestSearchText);
            stopWatch.Stop();
            Debug.WriteLine("searcher elapsed ms {0}", stopWatch.ElapsedMilliseconds);
            Debug.WriteLine(
                string.Format("expected percent {0}.  found {1}", testcase.ExpectedPercentMatch, searchermatches.Count())
                );

            stopWatch.Reset();
            stopWatch.Start();
            //trie
            Trie trie = new Trie();
            testcase.TestDictionary.WithEach(x =>
            {
                trie.Add(x, x);
            });
            stopWatch.Stop();
            Debug.WriteLine("trie init elapsed ms {0}", stopWatch.ElapsedMilliseconds);

            stopWatch.Reset();
            stopWatch.Start();
            var matches = trie.FindMatches(testcase.TestSearchText);
            stopWatch.Stop();
            Debug.WriteLine("forwardonly elapsed ms {0}", stopWatch.ElapsedMilliseconds);
            Debug.WriteLine(
                string.Format("expected percent {0}.  found {1}", testcase.ExpectedPercentMatch, matches.Count())
                );

            stopWatch.Reset();
            stopWatch.Start();
            var matches1b = TrieLogic.FindMatchesUsingForwardOnlyCursor2(trie, testcase.TestSearchText);
            stopWatch.Stop();
            Debug.WriteLine("forwardonly2 elapsed ms {0}", stopWatch.ElapsedMilliseconds);
            Debug.WriteLine(
                string.Format("expected percent {0}.  found {1}", testcase.ExpectedPercentMatch, matches1b.Count())
                );

            stopWatch.Reset();
            stopWatch.Start();
            var matches2 = trie.HasPositionalSearch().FindMatches(testcase.TestSearchText);
            stopWatch.Stop();
            Debug.WriteLine("seekahead elapsed ms {0}", stopWatch.ElapsedMilliseconds);
            Debug.WriteLine(
                string.Format("expected percent {0}.  found {1}", testcase.ExpectedPercentMatch, matches2.Count())
                );

            stopWatch.Reset();
            stopWatch.Start();
            var matches3 = trie.HasPositionalSearch().NonOverlapping().FindMatches(testcase.TestSearchText);
            stopWatch.Stop();
            Debug.WriteLine("seekaheadnonoverlapped elapsed ms {0}", stopWatch.ElapsedMilliseconds);
            Debug.WriteLine(
                string.Format("expected percent {0}.  found {1}", testcase.ExpectedPercentMatch, matches3.Count())
                );

            stopWatch.Reset();
            stopWatch.Start();
            var matches4 = trie.HasPositionalSearch().Paralleling().FindMatches(testcase.TestSearchText);
            stopWatch.Stop();
            Debug.WriteLine("seekaheadparallel elapsed ms {0}", stopWatch.ElapsedMilliseconds);
            Debug.WriteLine(
                string.Format("expected percent {0}.  found {1}", testcase.ExpectedPercentMatch, matches4.Count())
                );

            //stopWatch.Reset();
            //stopWatch.Start();
            //InlineTrie inline_trie = new InlineTrie();
            //stopWatch.Start();
            //testcase.TestDictionary.WithEach(x =>
            //{
            //    inline_trie.Add(x, x);
            //});
            //stopWatch.Stop();
            //Debug.WriteLine("inline init elapsed ms {0}", stopWatch.ElapsedMilliseconds);
            //stopWatch.Reset();
            //stopWatch.Start();
            //var inline_matches = inline_trie.FindMatches(testcase.TestSearchText);
            //stopWatch.Stop();
            //Debug.WriteLine("inline elapsed ms {0}", stopWatch.ElapsedMilliseconds);
            //Debug.WriteLine(
            //    string.Format("expected percent {0}.  found {1}", testcase.ExpectedPercentMatch, inline_matches.Count)
            //    );

            ////trie
            //ITrie<string> gma_trie =  new PatriciaTrie<string>();
            //stopWatch.Start();
            //testcase.TestDictionary.WithEach(x =>
            //{
            //    gma_trie.Add(x, x);
            //});
            //stopWatch.Stop();
            //Debug.WriteLine("init elapsed ms {0}", stopWatch.ElapsedMilliseconds);

            //stopWatch.Reset();
            //stopWatch.Start();
            //var gma_matches = gma_trie.Retrieve(testcase.TestSearchText);
            //stopWatch.Stop();
            //Debug.WriteLine("elapsed ms {0}", stopWatch.ElapsedMilliseconds);
            //Debug.WriteLine(
            //    string.Format("expected percent {0}.  found {1}", testcase.ExpectedPercentMatch, gma_matches.Count())
            //    );
            //eeeksoft 
            stopWatch.Reset();
            stopWatch.Start();
            var eeeksoft_searcher = new Decoratid.Idioms.StringSearch.EeekSoft.StringSearch(testcase.TestDictionary.ToArray());
            stopWatch.Stop();
            Debug.WriteLine("eeeksoft init elapsed ms {0}", stopWatch.ElapsedMilliseconds);

            stopWatch.Reset();
            stopWatch.Start();
            var eeeksoft_matches = eeeksoft_searcher.FindAll(testcase.TestSearchText);
            stopWatch.Stop();
            Debug.WriteLine("eeeksoft elapsed ms {0}", stopWatch.ElapsedMilliseconds);
            Debug.WriteLine(
                string.Format("expected percent {0}.  found {1}", testcase.ExpectedPercentMatch, eeeksoft_matches.Length)
                );

            //pduniho
            //stopWatch.Reset();
            //stopWatch.Start();
            //var pduniho_graph = new Decoratid.Idioms.StringSearch.PDuniho.StateGraph<char, int>();
            //for (int istr = 0; istr < testcase.TestDictionary.Count; istr++)
            //{
            //    pduniho_graph.Add(testcase.TestDictionary[istr], istr);
            //}
            //stopWatch.Stop();
            //Debug.WriteLine("init elapsed ms {0}", stopWatch.ElapsedMilliseconds);

            //stopWatch.Reset();
            //stopWatch.Start();
            //var pduniho_matches = pduniho_graph.RgobjFromCollectionParallel(testcase.TestSearchText);
            //stopWatch.Stop();
            //Debug.WriteLine("elapsed ms {0}", stopWatch.ElapsedMilliseconds);
            //Debug.WriteLine(
            //    string.Format("expected percent {0}.  found {1}", testcase.ExpectedPercentMatch, pduniho_matches.Length)
            //    );

            //pnikiforovs
            stopWatch.Reset();
            stopWatch.Start();
            var pnikiforovs_trie = new Decoratid.Idioms.StringSearch.PNikiforovs.Trie();
            testcase.TestDictionary.WithEach(item =>
            {
                pnikiforovs_trie.Add(item);
            });
            pnikiforovs_trie.Build();
            stopWatch.Stop();
            Debug.WriteLine("pnikiforovs init elapsed ms {0}", stopWatch.ElapsedMilliseconds);

            stopWatch.Reset();
            stopWatch.Start();
            string[] pnikiforovs_matches = pnikiforovs_trie.Find(testcase.TestSearchText).ToArray();
            stopWatch.Stop();
            Debug.WriteLine("pnikiforovs elapsed ms {0}", stopWatch.ElapsedMilliseconds);
            Debug.WriteLine(
                string.Format("expected percent {0}.  found {1}", testcase.ExpectedPercentMatch, pnikiforovs_matches.Length)
                );
        }
コード例 #60
0
 public void Add(EntityInfo entityInfo)
 {
     if (entityInfo.EntitySelector is ITrieValueEntitySelector trieSelector && trieSelector.Position == _position)
     {
         _entityTypes.Add(trieSelector.Key, entityInfo);
     }