public void BKTree_SearchHamming() { var tree = new BKTree(BKTree.DistanceMetric.Hamming); tree.Add("taxi"); tree.Add("Test2"); tree.Add("Text"); tree.Add("Test"); tree.Add("tttt"); // Hamming distance Test = Test2 (compare the length of the shorter word) var results = tree.Search("Test", 0); var result_d0 = results.Count; Assert.AreEqual(2, result_d0); // te-t max. 1 diff : teSt teSt2 teXt var result_d1 = tree.Search("te-t", 1).Count; Assert.AreEqual(3, result_d1); // test max. 2 diff : test test2 teXt tTTt var result_d2 = tree.Search("test", 2).Count; Assert.AreEqual(4, result_d2); }
public void BKTree_SearchLevenshtein() { var tree = new BKTree(); tree.Add("Test"); tree.Add("Test"); tree.Add("Test2"); tree.Add("taxi"); tree.Add("Text"); tree.Add("tttt"); // test -> test var result_d0 = tree.Search("Test", 0).Count; Assert.AreEqual(1, result_d0); // te-t -> teSt teXt var result_d1 = tree.Search("te-t", 1).Count; Assert.AreEqual(2, result_d1); // test -> test test2 teXt tTTt var result_d2 = tree.Search("test", 2).Count; Assert.AreEqual(4, result_d2); }
public void BKTree_NoDuplicate() { var tree = new BKTree(); tree.Add("AaaA"); tree.Add("TaaT"); tree.Add("TTTT"); tree.Add("Test"); tree.Add("Test"); tree.Add("TEST"); tree.Add("TeSt"); tree.Add("TeST"); tree.Add("Text"); tree.Add("Text"); tree.Add("TEXt"); var result_d0 = tree.Search("Test", 0).Count; Assert.AreEqual(1, result_d0); result_d0 = tree.Search("Text", 0).Count; Assert.AreEqual(1, result_d0); result_d0 = tree.Search("aaaa", 0).Count; Assert.AreEqual(1, result_d0); result_d0 = tree.Search("taat", 0).Count; Assert.AreEqual(1, result_d0); result_d0 = tree.Search("tttt", 0).Count; Assert.AreEqual(1, result_d0); result_d0 = tree.Search("zzzz", 0).Count; Assert.AreEqual(0, result_d0); }
public void BKTree_InsertionOrder() { int iterations = 10; var tree = new BKTree(); string[] array = { "Test", "TeSt", "AaaA", "TaaT", "TTTT", "Text", "TEXt", " ", "--", ":-)" }; for (int i = 1; i <= iterations; i++) { Shuffle(array); Console.WriteLine("BKTree insertion: v{0}", i); foreach (string value in array) { tree.Add(value); Console.WriteLine(value); } Console.WriteLine(" "); var result_d0 = tree.Search("Test", 0).Count; Assert.AreEqual(1, result_d0); result_d0 = tree.Search("Text", 0).Count; Assert.AreEqual(1, result_d0); result_d0 = tree.Search("aaaa", 0).Count; Assert.AreEqual(1, result_d0); result_d0 = tree.Search("taat", 0).Count; Assert.AreEqual(1, result_d0); result_d0 = tree.Search("tttt", 0).Count; Assert.AreEqual(1, result_d0); result_d0 = tree.Search("no match", 0).Count; Assert.AreEqual(0, result_d0); } }
public void BKTreeImage_Search() { var index = new Images.ImageIndex.Index(); var hash_algo = Images.ImageHashAlgorithm.Average; ulong ahash1 = 181; ulong ahash2 = 171; var hash1 = new List <String> { ahash1.ToString(), "1111111111" }; index.Add("Test", hash1); var hash2 = new List <String> { ahash2.ToString(), "1111111110" }; index.Add("Test2", hash2); var hash3 = new List <String> { ahash1.ToString(), "1111111111" }; index.Add("Test3", hash3); var hash4 = new List <String> { ahash1.ToString(), "1111111111" }; index.Add("Test", hash4); var tree = new BKTree(hash_algo, index); var id = index.Id("Test"); tree.Add(id); id = index.Id("Test2"); tree.Add(id); id = index.Id("Test3"); tree.Add(id); // index= test test2 test3 Assert.AreEqual(3, index.FileCount()); Assert.AreEqual(ahash1.ToString(), tree.GetImageHash("Test")); Assert.AreEqual(ahash2.ToString(), tree.GetImageHash("Test2")); Assert.AreEqual(100.0, tree.GetImageSimilarity("Test", "Test")); Assert.AreNotEqual(100.0, tree.GetImageSimilarity("Test", "Test2")); // test -> test test3 var result_d0 = tree.Search("Test", 100).Count; Assert.AreEqual(2, result_d0); // test2 block hash = 171 Assert.AreEqual(ahash2.ToString(), tree.GetImageHash("Test2")); // using Average tree algo Assert.AreEqual("Average", tree.ImageHashAlgo()); }
public void BKTreeImage_NoDuplicate() { var index = new Images.ImageIndex.Index(); var hash_algo = Images.ImageHashAlgorithm.Block; ulong ahash1 = 181; ulong ahash2 = 281; var hash1 = new List <String> { ahash1.ToString(), "1111111111" }; index.Add("Test", hash1); var hash2 = new List <String> { ahash2.ToString(), "1111111111" }; index.Add("Test3", hash2); var hash3 = new List <String> { ahash1.ToString(), "1111110000" }; index.Add("Test2", hash3); var hash4 = new List <String> { ahash2.ToString(), "000000000000000000000000" }; index.Add("dup", hash4); var tree = new BKTree(hash_algo, index); var id = index.Id("Test"); tree.Add(id); tree.Add(id); id = index.Id("Test2"); tree.Add(id); tree.Add(id); id = index.Id("Test3"); tree.Add(id); tree.Add(id); id = index.Id("dup"); tree.Add(id); tree.Add(id); tree.Add(id); // index= test test2 test3 dup Assert.AreEqual(4, index.FileCount()); // test -> test test3 var result_d100 = tree.Search("Test", 100).Count; Assert.AreEqual(2, result_d100); // dup -> dup var result_d100_2 = tree.Search("dup", 100).Count; Assert.AreEqual(1, result_d100_2); // dup block hash Assert.AreEqual("000000000000000000000000", tree.GetImageHash("dup")); // tree algo Assert.AreEqual("Block", tree.ImageHashAlgo()); }
static void Main(string[] args) { Stopwatch stopWatch = new Stopwatch(); BKTree bkTree = new BKTree(Ratio, 8); DBHelper dbHelper = new DBHelper(); TimeSpan timeSpan; List <Match> result; dbHelper.afterProgress += ProgressEvent; Console.WriteLine("Veriler alınıyor..."); dbHelper.LoadFromFile(bkTree); Console.WriteLine("\nHazır!"); dbHelper = null; while (true) { GC.Collect(); try { string input = Console.ReadLine(); stopWatch.Start(); string[] array = input.Split(' '); if (array.Length == 2 && array[0] == "add") { input = ValidString(String.Join("", array).Substring(3)); bkTree.Add(input); bkTree.CheckAndRemoveCache(input); Console.WriteLine("Eklendi: " + input); } else { result = bkTree.Search(ValidString(input)); if (result.Count > 0) { Console.WriteLine("Eşleşmeler: " + String.Join(", ", result.ToArray())); Console.WriteLine("Döngü sayısı: " + bkTree.src.count); } else { Console.WriteLine("Eşleşme bulunamadı."); } } stopWatch.Stop(); timeSpan = stopWatch.Elapsed; Console.WriteLine("Süre: " + String.Format("{0:00}:{1:00}:{2:00}.{3:00}", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10)); } catch (Exception ex) { stopWatch.Stop(); Console.WriteLine("Hata Oluştu: " + ex.Message); } finally { stopWatch.Reset(); } } }
static void Main(string[] args) { string line; List <string> listnames = new List <string>(); List <string> listsurnames = new List <string>(); BKTree bk = new BKTree(); System.IO.StreamReader file1 = new System.IO.StreamReader(@"c:\names.txt"); while ((line = file1.ReadLine()) != null) { listnames.Add(line); } file1.Close(); System.IO.StreamReader file2 = new System.IO.StreamReader(@"c:\surnames.txt"); while ((line = file2.ReadLine()) != null) { listsurnames.Add(line); } file2.Close(); //foreach (string name in listnames){ // foreach (string surname in listsurnames) // { // listall.Add(name + surname); // } //} for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++) { bk.Add(listnames[i] + listsurnames[j]); } } bk.Add("fetullahgülen"); bk.Add("firatkaptan"); Console.WriteLine("Ready!"); while (true) { var l = Console.ReadLine(); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); var x = (l.Length * 15 / 100) + 1; var a = bk.Search(l, x); stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime " + elapsedTime); Console.WriteLine(String.Join(", ", a.ToArray())); } }