예제 #1
0
        static unsafe void Main(string[] args)
        {
            Console.WriteLine("Emulate opendir()");

            //var buf = new byte[65536];

            //var fd = LibC.open(toNullTerm(Encoding.UTF8.GetBytes("/tmp")), LibC.O_DIRECTORY|LibC.O_RDONLY);
            //Console.WriteLine($"Open fd={fd} errno={LibC.errno}");

            //LibC.fcntl(fd, LibC.F_SETFD, LibC.FD_CLOEXEC);
            //Console.WriteLine($"Fcntl fd={fd} errno={LibC.errno}");

            //var dh = Mono.Unix.Native.Syscall.opendir("/tmp");

            //while (true)
            //{
            //    var rd = Mono.Unix.Native.Syscall.readdir(dh);
            //    if (rd == null) break;
            //    Console.WriteLine($"{rd.d_name}");
            //}

            //fixed (byte* p = buf)
            //{
            //    var count = LibC.read(fd, p, 65536);
            //    Console.WriteLine($"Read {(int) count}");
            //    dumpBytes(buf, (int) count);
            //}

            //var dh = RawDirs.opendir(toNullTerm(Encoding.UTF8.GetBytes("/tmp")));

            //while (true)
            //{
            //    var dir = RawDirs.readdir_wrap(dh);
            //    if (dir == null) break;

            //    var d = dir.Value;

            //    var name = Encoding.UTF8.GetString(d.d_name);
            //    Console.WriteLine($"{name}");
            //}

            //RawDirs.closedir(dh);

            var mt = new StringTrie <string>();

            mt.Add("/a/b/", "ab#");
            mt.Add("/a/c/", "ac#");
            mt.Add("/b/b/", "bb#");

            var a = mt.FindPredecessor("/a/b/c");

            Console.WriteLine($"/a/b/c {a.Value}");

            //a = mt.FindPredecessor("/a/b");
            //Console.WriteLine($"/a/b {a.Value}");
        }
예제 #2
0
        public void Given_a_Trie_with_one_item_When_a_subitem_is_added_Then_it_has_two_item()
        {
            //Given
            var trie = new StringTrie <string>();

            trie.Add("Alpha", "first");
            trie.Add("Aztec", "second");
            //Then
            var items = trie.ToArray();

            AssertEqual(items[0], "Alpha", "first");
            AssertEqual(items[1], "Aztec", "second");
        }
예제 #3
0
        public void Given_a_new_Trie_with_two_items_are_added_When_toArray_Then_the_array_is_filled()
        {
            //Given
            var trie = new StringTrie <string>();

            trie.Add("Beta", "second");
            trie.Add("Alpha", "first");
            //Then
            var items = trie.ToArray();

            AssertEqual(items[0], "Alpha", "first");
            AssertEqual(items[1], "Beta", "second");
        }
예제 #4
0
        public void Given_a_new_Trie_When_two_items_are_added_in_reverse_order_Then_it_has_two_item()
        {
            //Given
            var trie = new StringTrie <string>();

            trie.Add("Beta", "second");
            trie.Add("Alpha", "first");
            //Then
            var items = trie.ToArray();

            AssertEqual(items[0], "Alpha", "first");
            AssertEqual(items[1], "Beta", "second");
        }
 public void Given_a_filled_Trie_When_an_existing_item_is_removed_and_added_Then_its_there_again()
 {
     //when
     _underTest.Remove("Gamma");
     _underTest.Add("Gamma", "there and back again");
     Assert.Equal("there and back again", _underTest["Gamma"]);
 }
예제 #6
0
        public SearchService()
        {
            var lines = File.ReadAllLines("./data/cities500.txt");

            for (int i = 1; i < lines.Length - 1; i++)
            {
                var split = lines[i].Split('\t');

                var place = new Place
                {
                    Id        = int.Parse(split[0]),
                    Name      = split[2],
                    Latitude  = float.Parse(split[4]),
                    Longitude = float.Parse(split[5]),
                };

                _tree.Add(new[] { float.Parse(split[4]), float.Parse(split[5]) }, place);

                // if already contained
                if (_trie.ContainsKey(split[2]))
                {
                    var places = _trie[split[2]];
                    places.Add(place);
                }
                else
                {
                    _trie.Add(split[2], new List <Place> {
                        place
                    });
                }
            }
        }
예제 #7
0
        public void Given_a_Trie_with_two_items_When_a_subitem_is_added_Then_it_has_three_items()
        {
            //Given
            var trie = new StringTrie <string>();

            trie.Add("Alpha", "first");
            trie.Add("B", "second");
            //When
            trie.Add("Beta", "third");

            Helper.WriteLine(trie.ToString());
            //Then
            var items = trie.ToArray();

            AssertEqual(items[0], "Alpha", "first");
            AssertEqual(items[1], "B", "second");
            AssertEqual(items[2], "Beta", "third");
        }
예제 #8
0
        public void Given_a_new_Trie_When_one_item_is_added_Then_it_has_one_item()
        {
            //Given
            var trie = new StringTrie <string>();

            trie.Add("Alpha", "first");
            //Then
            var item = trie.Single();

            AssertEqual(item, "Alpha", "first");
        }
예제 #9
0
        public TrieAtom(string name, string[] strings, bool case_sensitive = false)
        {
            Name          = name;
            Score         = -1.0 * strings.Length / strings.Sum(s => s.Length);
            Description   = name;
            CaseSensitive = case_sensitive;

            trie = new StringTrie <string>();
            foreach (string s in strings)
            {
                trie.Add(CaseSensitive ? s : s.ToUpper(), s);
            }
        }
예제 #10
0
        static void Main(string[] args)
        {
            GetAppSettings settings = new GetAppSettings();

            Console.WriteLine(settings.GetSetting("Test"));

            Spelling spelling = new Spelling();
            string   word     = "";

            word = "PORK";
            Console.WriteLine("{0} => {1}", word, spelling.Correct(word));

            word = "FUU"; // 'correcter' is not in the dictionary file so this doesn't work
            Console.WriteLine("{0} => {1}", word, spelling.Correct(word));

            word = "LAMM";
            Console.WriteLine("{0} => {1}", word, spelling.Correct(word));

            word = "PAINT";
            Console.WriteLine("{0} => {1}", word, spelling.Correct(word));

            word = "ETAE";
            Console.WriteLine("{0} => {1}", word, spelling.Correct(word));

            // A sentence
            string sentence   = "I havve speled thes woord wwrong"; // sees speed instead of spelled (see notes on norvig.com)
            string correction = "";

            foreach (string item in sentence.Split(' '))
            {
                correction += " " + spelling.Correct(item);
            }
            Console.WriteLine("Did you mean:" + correction);

            Enumerable.Range(Convert.ToInt32('A'), 26).Union(Enumerable.Range(Convert.ToInt32('0'), 10)).ToList().
            ForEach(e => Console.WriteLine(Convert.ToChar(e)));

            Console.WriteLine($"{DuoVia.FuzzyStrings.StringExtensions.FuzzyMatch("HONG", "HONG")}");
            Console.WriteLine($"{DuoVia.FuzzyStrings.StringExtensions.FuzzyMatch("HOONG", "HONG")}");
            Console.WriteLine($"{DuoVia.FuzzyStrings.StringExtensions.FuzzyMatch("HOONG", "HSOUNG")}");
            Console.WriteLine($"{DuoVia.FuzzyStrings.StringExtensions.FuzzyMatch("HOONG", "HSIN")}");
            Console.WriteLine($"{DuoVia.FuzzyStrings.StringExtensions.FuzzyMatch("Hello Wor1ld3", "Hello Warl12d3")}");



            Console.WriteLine($"{FuzzyString.ComparisonMetrics.JaccardDistance("Hello World", "Hello World")}");
            Console.WriteLine($"{FuzzyString.ComparisonMetrics.JaccardDistance("Hello World", "Hello Warld")}");
            Console.WriteLine($"{FuzzyString.ComparisonMetrics.JaccardDistance("Hello World", "Hello Warl12d3")}");
            Console.WriteLine($"{FuzzyString.ComparisonMetrics.JaccardDistance("Hello Wor1ld3", "Hello Warl12d3")}");

            Console.WriteLine($"{FuzzyString.ComparisonMetrics.HammingDistance("Hello World", "Hello World")}");
            Console.WriteLine($"{FuzzyString.ComparisonMetrics.HammingDistance("Hello World", "Hello Warld")}");
            Console.WriteLine($"{FuzzyString.ComparisonMetrics.HammingDistance("Hello World", "Hello Warl12d3")}");
            Console.WriteLine($"{FuzzyString.ComparisonMetrics.HammingDistance("Hello Wor1ld3", "Hello Warl12d3")}");

            Console.WriteLine($"{FuzzyString.ComparisonMetrics.SorensenDiceDistance("Hello World", "Hello World")}");
            Console.WriteLine($"{FuzzyString.ComparisonMetrics.SorensenDiceDistance("Hello World", "Hello Warld")}");
            Console.WriteLine($"{FuzzyString.ComparisonMetrics.SorensenDiceDistance("Hello World", "Hello Warl12d3")}");
            Console.WriteLine($"{FuzzyString.ComparisonMetrics.SorensenDiceDistance("Hello Wor1ld3", "Hello Warl12d3")}");

            Console.WriteLine("Hello World".DiceCoefficient("Hello World"));
            Console.WriteLine("Hello World".DiceCoefficient("Hello Warld"));
            Console.WriteLine("Hello World".DiceCoefficient("Heo Warl12d3"));
            Console.WriteLine("Hello World".DiceCoefficient("Hello Warl12d3"));

            StringTrie <string> Trie = new StringTrie <string>();

            Trie.Add("ABC", "ABC");
            Trie.Add("ABCDEF", "ABCDEF");
            Trie.Add("FGHIJK", "FGHIJK");
            Trie.Add("KKK", "KKK");
            Trie.Add("LLLLL", "LLLLL");
            Trie.Add("LLLLLT", "LLLLLT");
            Trie.Add("Tai Po", "Tai Po");
            Trie.Add("Tai Po ABC", "Tai Po ABC");
            Trie.Add("Tai Po ABC DEF", "Tai Po ABC DEF");
            Trie.Add("Tai Po XXX", "Tai Po XXX");
            Trie.Add("Tai Po YYY", "Tai Po YYY");

            TraverseTrie(Trie,
                         n =>
            {
                Console.WriteLine($"Node: {n.Value}");
            },
                         lf =>
            {
                Console.WriteLine($"Leaf: *****");
            });


            var node = Trie.Find("Tai Po A");

            Console.WriteLine(node.IsLeaf + " " + node.HasValue);
            Console.WriteLine("*************************");
            node.Descendants.ToList().ForEach(e =>
            {
                if (e.HasValue)
                {
                    Console.WriteLine(e.Value);
                }
            });
            Console.WriteLine("*************************");
            node = Trie.Find("Tai Po AB");
            Console.WriteLine(node.IsLeaf + " " + node.HasValue);
            node = Trie.Find("Tai Po ABC");
            Console.WriteLine(node.IsLeaf + " " + node.HasValue);


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

            l.Add("");
            l.Add(null);
            l.Add("Hello World");
            foreach (var ll in l)
            {
                if (string.IsNullOrEmpty(ll))
                {
                    continue;
                }
                Console.WriteLine(ll);
            }

            var s = "([([}) []  A [] []   Test  ]]]]]   ";

            Console.WriteLine(s.Replace('(', Convert.ToChar(32)).Replace(')', Convert.ToChar(32)).Replace('[', Convert.ToChar(32)));



            /*
             * FileStream fs = new FileStream(@"C:\Temp\TestAddr.csv", FileMode.Open, FileAccess.Read);
             * var reader = new CsvReader(new StreamReader(fs));
             * reader.ReadHeader();
             * int i = 0;
             * while (reader.Read())
             * {
             *  var addr = reader.GetRecord<Addr>();
             *  Console.WriteLine(addr);
             *  if (i++ >= 10)
             *      break;
             * }
             * var p = System.IO.Path.GetTempPath();
             */

            var r = "A".CompareTo("B");

            var cm = new CodeMappings();


            HashSet <int> hs = new HashSet <int>();

            hs.Add(1);
            hs.Add(2);
            hs.Add(1);
            hs.Add(2);
            hs.Add(3);

            foreach (var x in hs)
            {
                Console.WriteLine(x);
            }

            Console.WriteLine("First Result Set ******************************************************************************");
            var res1 = LCSFinder.GetLCS("A B C D E F G H", "A B C D U V Y K");
            var res2 = LCSFinder.GetLCS("A B C D E F G H", "A I B T C O D L");

            Console.WriteLine(res1);
            Console.WriteLine(res2);

            Console.WriteLine("Second Result Set ******************************************************************************");
            var res3 = LCSFinder.GetLCS("AA BB CC DDD EE FFFF GG H", "AA XXXX BDB YYYYYY CC DD123DD U EE V Y K");
            var res4 = LCSFinder.GetLCS("AA BB CC DDD EE FFFF GG H", "AA I BB T CC OXXXXX DDD L EE");

            Console.WriteLine(res3);
            Console.WriteLine(res4);

            Console.WriteLine("Third Result Set ******************************************************************************");
            var res5 = LCSFinder.GetLCS("AA BB CC DD E F G", "AA BB CC BB II CC KK DD H I K");
            var res6 = LCSFinder.GetLCS("AA BB CC DD E F G", "AA BB CCCC DD II E A F B G");

            Console.WriteLine(res5);
            Console.WriteLine(res6);
            Console.WriteLine("******************************************************************************");

            Console.WriteLine("First Result Set ******************************************************************************");
            var resX = "A B C D E F G H".LongestCommonSubsequence("A B C D U V Y K");
            var resY = "A B C D E F G H".LongestCommonSubsequence("A I B T C O D L");

            Console.WriteLine(resX);
            Console.WriteLine(resY);

            Console.WriteLine("Second Result Set ******************************************************************************");
            var resA = "AA BB CC DDD EE FFFF GG H".LongestCommonSubsequence("AA XXXX BDB YYYYYY CC DD123DD U EE V Y K");
            var resB = "AA BB CC DDD EE FFFF GG H".LongestCommonSubsequence("AA I BB T CC OXXXXX DDD L EE");

            Console.WriteLine(resA);
            Console.WriteLine(resB);

            Console.WriteLine("Third Result Set ******************************************************************************");
            var resC = "AA BB CC DD E F G".LongestCommonSubsequence("AA BB CC BB II CC KK DD H I K");
            var resD = "AA BB CC DD E F G".LongestCommonSubsequence("AA BB CCCC DD II E A F B G");

            Console.WriteLine(resC);
            Console.WriteLine(resD);
            Console.WriteLine("******************************************************************************");


            Console.WriteLine("1 Result Set ******************************************************************************");
            var r1 = "ABCDEFGH".LongestCommonSubsequence("ABCDUVYK");
            var r2 = "ABCDEFGH".LongestCommonSubsequence("AIBTCODL");

            Console.WriteLine(r1);
            Console.WriteLine(r2);

            Console.WriteLine("2 Result Set ******************************************************************************");
            var r3 = "AABBCCDDDEEFFFFGGH".LongestCommonSubsequence("AAXXXXBDBYYYYYYCCDD123DDUEEVYK");
            var r4 = "AABBCCDDDEEFFFFGGH".LongestCommonSubsequence("AAIBBTCCOXXXXXDDDLEE");

            Console.WriteLine(r3);
            Console.WriteLine(r4);

            Console.WriteLine("3 Result Set ******************************************************************************");
            var r5 = "AABBCCDDEFG".LongestCommonSubsequence("AABBCCBBIICCKKDDHIK");
            var r6 = "AABBCCDDEFG".LongestCommonSubsequence("AABBCCCCDDIIEAFBG");

            Console.WriteLine(r5);
            Console.WriteLine(r6);
            Console.WriteLine("******************************************************************************");

            Console.WriteLine("4 Result Set ******************************************************************************");
            var r7 = "SUN FUNG WAI".LongestCommonSubsequence("SAN FUNG WAI");
            var r8 = "SUN FUNG WAI".LongestCommonSubsequence("SAN WAI COURT");

            Console.WriteLine(r7);
            Console.WriteLine(r8);
            Console.WriteLine("******************************************************************************");


            Enumerable.Range(1, 20).ToList().ForEach(e =>
            {
                var rA = ToRoman(e);
                var sA = ConvertRomanToNumber(rA);
                Console.WriteLine($"{rA}->{sA}");
            });

            MainStore ms = new MainStore();

            ms.Serialize(@"C:\Temp\tt.json");



            List <StreetNoExpansion> stl = new List <StreetNoExpansion>()
            {
                new StreetNoExpansion(13, "A", 13, "A"),    //0
                new StreetNoExpansion(13, "L", 14, "L"),    //1
                new StreetNoExpansion(13, "L", 17, "L"),    //2
                new StreetNoExpansion(13, "L", 20, "L"),    //3
                new StreetNoExpansion(13, "L", 14, "M"),    //4
                new StreetNoExpansion(12, "", 12, ""),      //5
                new StreetNoExpansion(12, "A", 12, "A"),    //6
                new StreetNoExpansion(12, "", 19, ""),      //7
                new StreetNoExpansion(12, "", 20, ""),      //8
                new StreetNoExpansion(12, "", 12, ""),      //9
                new StreetNoExpansion(12, "", 13, ""),      //10
                new StreetNoExpansion(12, "A", 12, "B"),    //11
                new StreetNoExpansion(14, "D", 21, "S"),    //12
                new StreetNoExpansion(15, "A", 17, ""),     //13
                new StreetNoExpansion(15, "A", 29, ""),     //14
                new StreetNoExpansion(15, "B", 16, ""),     //15
                new StreetNoExpansion(15, "C", 15, ""),     //16
                new StreetNoExpansion(16, "", 17, "B"),     //17
                new StreetNoExpansion(16, "", 18, "B"),     //18
                new StreetNoExpansion(16, "", 16, "B"),     //19
                new StreetNoExpansion(16, "", 20, "D"),     //20
                new StreetNoExpansion(16, "A", 17, ""),     //21
                new StreetNoExpansion(16, "B", 18, ""),     //22
                new StreetNoExpansion(16, "C", 16, ""),     //23
                new StreetNoExpansion(16, "D", 20, ""),     //24
                new StreetNoExpansion(3, "EA3", 3, "EA3"),  //25
                new StreetNoExpansion(3, "A", 0, ""),       //26
                new StreetNoExpansion(3, "", 0, ""),        //27
            };

            stl.ForEach(e => Console.WriteLine(e));
            var line = "";

            while (!string.IsNullOrEmpty(line = Console.ReadLine()))
            {
                Console.Write(">");
                if (!ConvertStreetNoFromStringToComponents(line, out int from, out string fromA, out int to, out string toA))
                {
                    Console.WriteLine("Conversion Failed");
                    continue;
                }
                int idx = 0;
                stl.ForEach(e =>
                {
                    Console.WriteLine($"{idx++}->{e.MatchStreetNo(from, fromA, to, toA)}");
                });
            }
        }
예제 #11
0
 public void TrieTest1()
 {
     var t = new StringTrie<string>();
     t.Add("arm", "armum");
     t.Add("armat", "armatus");
     t.Add("arment", "armenta");
     var three = t.GetValues("armenta").ToArray();
 }
예제 #12
0
 public void Setup()
 {
     Trie = new StringTrie();
     Trie.Add("INSERT", "DELETE", "UPDATE", "CREATE", "ALTER", "INTO", "DROP")
     .Build();
 }