-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
928 lines (779 loc) · 45.7 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
//*********************************************************************************************************
//MinHasher - Example minhashing system with Jaccard comparisons.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace LinstaMatch
{
class Program
{
/*whether we are using our tuned LSH-Minhash for candidata pair generation (LinstaMatch) or the regular LSH-Minhash implementation*/
private static bool tuned = true;
public static bool Tuned
{
get { return Program.tuned; }
}
//used when Tuned = false
private static int constantRvalue = 3;
public static int ConstantRvalue
{
get { return Program.constantRvalue; }
}
private static string dataset_main_location = @"..\..\input\";
static void Main(string[] args)
{
Console.WriteLine("Tuned: " + Program.Tuned + "\n");
/*Evaluations mentined in LinstaMatch Paper - start*/
//processNewsCorporaFiles(true,250000);
//processNewsCorporaFiles(true, 5000); //Process a sample of 5000 instances from news-aggregator dataset for candidate-pairs-generation
//processNewsCorporaFiles(false, 5000); //Process All news-aggregator dataset for candidate-pairs-generation
//processAmazonJsonDumpFiles(true, 5000); //Process a sample of 5000 instances from Amazon office products metadata dataset for candidate-pairs-generation
//processAmazonJsonDumpFiles(false, 5000); //Process All Amazon office products metadata dataset for candidate-pairs-generation
//processMashableFiles(true, 5000); //Process a sample of 5000 instances from mashable (online news popularity) dataset for candidate-pairs-generation
//processMashableFiles(false, 5000); //Process All mashable (online news popularity) dataset for candidate-pairs-generation
//processUobmLargeFiles_InstanceMatch(false, 1000); //Process UOBM-Mainbox files used in OAEI 2016 campaign for instance matching
//processSpimbenchFiles_InstanceMatch(false, 1000); //Process Spimbench files used in OAEI 2016 campaign for instance matching
/*Evaluations mentined in LinstaMatch Paper - end*/
/*
* these tests were not mentioned in the paper
//processNumbersTest3(false, 1000);
//MinHasher3TestFunc1();
//JaccardTest1();
//processNumbersTest(false, 1000);
//generatePairsFileForRoleSim();
* */
//For integration to RoleSim
generatePairsFileForRoleSim();
}
static void processNewsCorporaFiles(bool sample = false, int sample_size = 1000)
{
//to try minhash on news corpora file
//string file = @"C:\Users\maydar\Dropbox\Semantic Study\ScabilityPaper\datasets\news aggregator\newsCorpora.csv-clean.txt";
string file = dataset_main_location + @"\news aggregator\newsCorpora.csv-clean.txt";
string pair_output_filename = file + "_minhashpairs.txt";
int numHashFunctions = 130;
double simThreshold = 0.65;
bool exclude_sim_under_threshold = false; //vertex pairs which have estimated similarity under the threshold will be excluded if set
MinHasher2 minHasher = new MinHasher2(numHashFunctions, simThreshold);
Dictionary<int, string[]> wordList;
Console.BufferHeight = Int16.MaxValue - 1; // ***** Alters the BufferHeight *****
Stopwatch sw0 = new Stopwatch();
Stopwatch sw = new Stopwatch();
int[] index_locations = { 0, 1, 2 };
string sep = @"\t";
int limit = -1;
if (sample)
{
limit = sample_size;
Console.WriteLine("Sample size: " + sample_size);
}
SepInputReader<int, string> sepInputReader = new SepInputReader<int, string>(file, index_locations, sep, false, limit);
Dictionary<int, string> groundTruth = sepInputReader.groundTruth;
//Dictionary<int, int[]> docMinhashes = minHasher.createMinhashCollection(flatInputReader.vertexLabelList);
wordList = sepInputReader.wordList;
Console.WriteLine(string.Format("\r\nInstances count: {0}", wordList.Count));
long possiblePairCount = PermutationsAndCombinations.nCr(wordList.Count, 2);
/*if (!sample)
wordList = sepInputReader.wordList;
else
{
wordList = Util.getSampleFromDict(sepInputReader.wordList, sample_size);
}*/
//Now create a MinHasher object to minhash each of the documents created above
//using 300 unique hashing functions.
//MinHasher minHasher = new MinHasher(500, 5);
Console.WriteLine("\r\nGenerating MinHash signatures ... ");
sw0.Restart();
sw.Restart();
Dictionary<int, int[]> docMinhashes = minHasher.createMinhashCollection(wordList);
if (sample)
{
//double avg_diff_perc_from_actual_and_minhash_jaccard = Util.calculateMinHashFunctionsAccuracy(wordList, docMinhashes);
}
sw.Stop();
Console.WriteLine("Generated MinHash signatures in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Restart();
Console.WriteLine("\r\nCreating MinHash buckets ... ");
Dictionary<string, HashSet<int>> m_lshBuckets = minHasher.createBandBuckets(wordList, docMinhashes);
Console.WriteLine("Created MinHash buckets in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
Console.WriteLine("\r\nComplexity with regular jaccard lookup(estimate): " + Math.Pow(wordList.Count, 3) / 5);
/*
sw.Restart();
Console.WriteLine("\r\nListing buckets sizes ... ");
minHasher.listBucketSizes(m_lshBuckets, pair_output_filename);
Console.WriteLine("Listing buckets sizes in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();*/
sw.Restart();
Console.WriteLine("\r\nGenerating vertex pairs using MinHash buckets ... ");
Dictionary<string, Tuple<int, int, double>> pairsDictionary = minHasher.generateVertexPairs(m_lshBuckets, docMinhashes, wordList, exclude_sim_under_threshold, null);
Console.WriteLine("Generated vertex pairs using MinHash buckets in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
sw0.Stop();
Console.WriteLine("\r\nTook total time of: " + sw0.Elapsed.ToString("mm\\:ss\\.ff"));
int foundPairsCount = pairsDictionary.Count;
double prunePercentage = ((double)(possiblePairCount - foundPairsCount) / (double)possiblePairCount) * 100.0;
Cluster<int, string> cls = new Cluster<int, string>(pairsDictionary, groundTruth);
//cls.generateClusers1();
//double precision_from_groundTruth = cls.calculatePrecision_fromGroundTruth();
sw.Restart();
double precision_from_actualSimilarity = cls.calculatePrecision_fromActualSimilarity(wordList, simThreshold);
Console.WriteLine("Calculated precision from found pairs in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
if (sample && sample_size<=10000)
{
sw.Restart();
Console.WriteLine("Calculating recall from actual should be pairs:");
Dictionary<string, Tuple<int, int, double>> actualPairsDictionary = Util.getActualPairsDictionary(wordList, simThreshold);
double recall = Util.calculateRecall<int>(actualPairsDictionary, pairsDictionary);
Console.WriteLine("Calculated recall from the algorithm vs pairwise-comparison in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
/*Dictionary<string, Tuple<int, int, double>> actualMinHashPairsDictionary = Util.getActualPairsDictionary(docMinhashes, simThreshold);
Console.WriteLine("Calculating recall from actual MinHash pairs:");
recall = Util.calculateRecall<int>(actualMinHashPairsDictionary, pairsDictionary);*/
int a = 0;
}
Console.WriteLine(string.Format("\r\nPossible pairs count: {0}", possiblePairCount));
Console.WriteLine(string.Format("\r\nFound pairs count: {0}", foundPairsCount));
Console.WriteLine(string.Format("\r\nPrune percentage: {0}", prunePercentage));
int x = 1;
Console.ReadKey();
}
static void processAmazonJsonDumpFiles(bool sample = false, int sample_size = 1000)
{
Console.WriteLine("Amazon meta data will be made available (for research purposes) on request. Please contact Julian McAuley ([email protected]) to obtain a link.");
//to try minhash on amazon json dump files
string amz_json_file = @"C:\Users\maydar\Documents\Sony Backup\PROJECTS\amazon\review-dumps\test\meta_Office_Products.json.gz";
string pair_output_filename = amz_json_file + "_minhashpairs.txt";
int numHashFunctions = 130;
double simThreshold = 0.65;
bool exclude_sim_under_threshold = false; //vertex pairs which have estimated similarity under the threshold will be excluded if set
//MinHasher minHasher = new MinHasher(numHashFunctions, simThreshold);
MinHasher2 minHasher = new MinHasher2(numHashFunctions, simThreshold);
Dictionary<string, string[]> wordList;
Console.BufferHeight = Int16.MaxValue - 1; // ***** Alters the BufferHeight *****
Stopwatch sw = new Stopwatch();
Stopwatch sw0 = new Stopwatch();
int limit = -1;
if (sample)
{
limit = sample_size;
Console.WriteLine("Sample size: " + sample_size);
}
AmazonJsonInputReader amzInputReader = new AmazonJsonInputReader(amz_json_file, false, limit);
//Dictionary<int, int[]> docMinhashes = minHasher.createMinhashCollection(flatInputReader.vertexLabelList);
/*if (!sample)
wordList = amzInputReader.productWordList;
else
{
wordList = Util.getSampleFromDict(amzInputReader.productWordList, sample_size);
}*/
wordList = amzInputReader.productWordList;
Console.WriteLine(string.Format("\r\nInstances count: {0}", wordList.Count));
long possiblePairCount = PermutationsAndCombinations.nCr(wordList.Count, 2);
Console.WriteLine(" ");
//Now create a MinHasher object to minhash each of the documents created above
//using 300 unique hashing functions.
//MinHasher minHasher = new MinHasher(500, 5);
Console.WriteLine("\r\nGenerating MinHash signatures ... ");
sw0.Restart();
sw.Restart();
//Dictionary<int, int[]> docMinhashes = minHasher.createMinhashCollection(flatInputReader.vertexLabelList);
Dictionary<string, int[]> docMinhashes = minHasher.createMinhashCollection(wordList);
sw.Stop();
Console.WriteLine("Generated MinHash signatures in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Restart();
Console.WriteLine("\r\nCreating MinHash buckets ... ");
Dictionary<string, HashSet<string>> m_lshBuckets = minHasher.createBandBuckets(wordList, docMinhashes);
Console.WriteLine("Created MinHash buckets in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
Console.WriteLine("\r\nComplexity with regular jaccard lookup(estimate): " + Math.Pow(wordList.Count, 3) / 5);
sw.Restart();
Console.WriteLine("\r\nGenerating vertex pairs using MinHash buckets ... ");
Dictionary<string, Tuple<string, string, double>> pairsDictionary =
minHasher.generateVertexPairs(m_lshBuckets, docMinhashes, wordList, exclude_sim_under_threshold, null);
Console.WriteLine("Generated vertex pairs using MinHash buckets in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
sw0.Stop();
Console.WriteLine("\r\nTook total time of: " + sw0.Elapsed.ToString("mm\\:ss\\.ff"));
int foundPairsCount = pairsDictionary.Count;
double prunePercentage = ((double)(possiblePairCount - foundPairsCount) / (double)possiblePairCount) * 100.0;
Console.WriteLine("\r\nBucket pairsDictionary size: " + pairsDictionary.Count);
Cluster<string, string> cls = new Cluster<string, string>(pairsDictionary, null);
//cls.generateClusers1();
//double precision_from_groundTruth = cls.calculatePrecision_fromGroundTruth();
double precision_from_actualSimilarity = cls.calculatePrecision_fromActualSimilarity(wordList, simThreshold);
if (sample && limit <= 50000)
{
sw.Restart();
Console.WriteLine("Calculating recall from actual should be pairs:");
Dictionary<string, Tuple<string, string, double>> actualPairsDictionary = Util.getActualPairsDictionary(wordList, simThreshold);
double recall = Util.calculateRecall<string>(actualPairsDictionary, pairsDictionary);
Console.WriteLine("Calculated recall from the algorithm vs pairwise-comparison in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
int a = 0;
}
Console.WriteLine(string.Format("\r\nPossible pairs count: {0}", possiblePairCount));
Console.WriteLine(string.Format("\r\nFound pairs count: {0}", foundPairsCount));
Console.WriteLine(string.Format("\r\nPrune percentage: {0}", prunePercentage));
Console.ReadKey();
}
static void processMashableFiles(bool sample = false, int sample_size = 1000)
{
//to try minhash on news corpora file
//string file = @"C:\Users\maydar\Dropbox\Semantic Study\ScabilityPaper\datasets\OnlineNewsPopularity\OnlineNewsPopularity2.csv-clean.txt";
string file = dataset_main_location + @"\OnlineNewsPopularity\OnlineNewsPopularity2.csv-clean.txt";
string pair_output_filename = file + "_minhashpairs.txt";
int numHashFunctions = 130;
double simThreshold = 0.65;
bool exclude_sim_under_threshold = false; //vertex pairs which have estimated similarity under the threshold will be excluded if set
MinHasher2 minHasher = new MinHasher2(numHashFunctions, simThreshold);
Dictionary<int, string[]> wordList;
Console.BufferHeight = Int16.MaxValue - 1; // ***** Alters the BufferHeight *****
Stopwatch sw0 = new Stopwatch();
Stopwatch sw = new Stopwatch();
int limit = -1;
if (sample)
{
limit = sample_size;
Console.WriteLine("Sample size: " + sample_size);
}
int[] index_locations = { 0, 1, 2 };
string sep = @"\t";
SepInputReader<int, string> sepInputReader = new SepInputReader<int, string>(file, index_locations, sep,
false, limit);
Dictionary<int, string> groundTruth = sepInputReader.groundTruth;
//Now create a MinHasher object to minhash each of the documents created above
//using 300 unique hashing functions.
//MinHasher minHasher = new MinHasher(500, 5);
Console.WriteLine("\r\nGenerating MinHash signatures ... ");
sw0.Restart();
sw.Restart();
//Dictionary<int, int[]> docMinhashes = minHasher.createMinhashCollection(flatInputReader.vertexLabelList);
/*if (!sample)
wordList = sepInputReader.wordList;
else
{
wordList = Util.getSampleFromDict(sepInputReader.wordList, sample_size);
}*/
wordList = sepInputReader.wordList;
Console.WriteLine(string.Format("\r\nInstances count: {0}", wordList.Count));
long possiblePairCount = PermutationsAndCombinations.nCr(wordList.Count, 2);
Dictionary<int, int[]> docMinhashes = minHasher.createMinhashCollection(wordList);
sw.Stop();
Console.WriteLine("Generated MinHash signatures in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Restart();
Console.WriteLine("\r\nCreating MinHash buckets ... ");
Dictionary<string, HashSet<int>> m_lshBuckets = minHasher.createBandBuckets(wordList, docMinhashes);
Console.WriteLine("Created MinHash buckets in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
Console.WriteLine("\r\nComplexity with regular jaccard lookup(estimate): " + Math.Pow(wordList.Count, 3) / 5);
sw.Restart();
Console.WriteLine("\r\nGenerating vertex pairs using MinHash buckets ... ");
Dictionary<string, Tuple<int, int, double>> pairsDictionary = minHasher.generateVertexPairs(m_lshBuckets, docMinhashes, wordList, exclude_sim_under_threshold, null);
Console.WriteLine("Generated vertex pairs using MinHash buckets in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
sw0.Stop();
Console.WriteLine("\r\nTook total time of: " + sw0.Elapsed.ToString("mm\\:ss\\.ff"));
int foundPairsCount = pairsDictionary.Count;
double prunePercentage = ((double)(possiblePairCount - foundPairsCount) / (double)possiblePairCount) * 100.0;
Console.WriteLine("\r\nBucket pairsDictionary size: " + pairsDictionary.Count);
Cluster<int, string> cls = new Cluster<int, string>(pairsDictionary, groundTruth);
//cls.generateClusers1();
double precision = cls.calculatePrecision_fromGroundTruth();
double precision_from_actualSimilarity = cls.calculatePrecision_fromActualSimilarity(wordList, simThreshold);
if (sample)
{
sw.Restart();
Console.WriteLine("Calculating recall from actual should be pairs:");
Dictionary<string, Tuple<int, int, double>> actualPairsDictionary = Util.getActualPairsDictionary(wordList, simThreshold);
double recall = Util.calculateRecall<int>(actualPairsDictionary, pairsDictionary);
Console.WriteLine("Calculated recall from the algorithm vs pairwise-comparison in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
}
Console.WriteLine(string.Format("\r\nPossible pairs count: {0}", possiblePairCount));
Console.WriteLine(string.Format("\r\nFound pairs count: {0}", foundPairsCount));
Console.WriteLine(string.Format("\r\nPrune percentage: {0}", prunePercentage));
Console.ReadKey();
}
private static void processUobmLargeFiles_InstanceMatch(bool sample = false, int sample_size = 1000)
{
Console.WriteLine("Processing UOBM_large ...");
string file1 = dataset_main_location + @"\IM2016_UOBM_large\Abox1.nt";
string file2 = dataset_main_location + @"\IM2016_UOBM_large\Abox2.nt";
string file_gt =
dataset_main_location + @"\IM2016_UOBM_large\refalign.rdf";
int numHashFunctions = 256;
double simThreshold = 0.5;
//ground truth file
string pair_output_filename = file1 + "_minhashpairs.txt";
string prefix1 = "|first|", prefix2 = "|second|", sep_prefix = "-";
bool exclude_sim_under_threshold = false;
//vertex pairs which have estimated similarity under the threshold will be excluded if set
MinHasher2 minHasher = new MinHasher2(numHashFunctions, simThreshold);
Dictionary<string, string[]> wordList;
Console.BufferHeight = Int16.MaxValue - 1; // ***** Alters the BufferHeight *****
Stopwatch sw = new Stopwatch();
Stopwatch sw0 = new Stopwatch();
int limit = -1;
if (sample)
{
limit = sample_size;
Console.WriteLine("Sample size: " + sample_size);
}
UobmInputReader uobmInputReader = new UobmInputReader(file1, file2, file_gt, limit, prefix1, prefix2,
sep_prefix);
wordList = uobmInputReader.wordList;
Console.WriteLine(string.Format("\r\nInstances count: {0}", wordList.Count));
//long possiblePairCount = PermutationsAndCombinations.nCr(wordList.Count, 2);
long possiblePairCount = uobmInputReader.possiblePairsCount;
sw0.Restart();
Console.WriteLine("\r\nGenerating MinHash signatures ... ");
sw.Restart();
Dictionary<string, int[]> docMinhashes = minHasher.createMinhashCollection(wordList);
sw.Stop();
Console.WriteLine("Generated MinHash signatures in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Restart();
Console.WriteLine("\r\nCreating MinHash buckets ... ");
Dictionary<string, HashSet<string>> m_lshBuckets = minHasher.createBandBuckets(wordList, docMinhashes);
Console.WriteLine("Created MinHash buckets in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
Console.WriteLine("\r\nComplexity with regular jaccard lookup(estimate): " + Math.Pow(wordList.Count, 3) / 5);
sw.Restart();
Console.WriteLine("\r\nGenerating vertex pairs using MinHash buckets ... ");
Dictionary<string, Tuple<string, string, double>> pairsDictionary =
minHasher.generateVertexPairs(m_lshBuckets, docMinhashes, wordList, exclude_sim_under_threshold, null,
true, prefix1, prefix2, sep_prefix);
Console.WriteLine("Generated vertex pairs using MinHash buckets in Time : " +
sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
sw0.Stop();
Console.WriteLine("\r\nTook total time of: " + sw0.Elapsed.ToString("mm\\:ss\\.ff"));
int foundPairsCount = pairsDictionary.Count;
double prunePercentage = ((double)(possiblePairCount - foundPairsCount) / (double)possiblePairCount) * 100.0;
Cluster<string, string> cls = new Cluster<string, string>(pairsDictionary, null);
sw.Restart();
double precision_from_actualSimilarity = cls.calculatePrecision_fromActualSimilarity(wordList, simThreshold);
Console.WriteLine("Calculated precision from found pairs in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
sw.Restart();
Console.WriteLine("Calculating recall from ground truth:");
double recall = Util.calculateRecall<string>(uobmInputReader.gtPairsDictionary, pairsDictionary);
Console.WriteLine("Calculated recall from the algorithm vs pairwise-comparison in Time : " +
sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
double fmeasure = 2 * ((precision_from_actualSimilarity * recall) / (precision_from_actualSimilarity + recall));
Console.WriteLine("F-measure: " + fmeasure);
Console.WriteLine(string.Format("\r\nPossible pairs count: {0}", possiblePairCount));
Console.WriteLine(string.Format("\r\nFound pairs count: {0}", foundPairsCount));
Console.WriteLine(string.Format("\r\nPrune percentage: {0}", prunePercentage));
Console.ReadKey();
}
private static void processSpimbenchFiles_InstanceMatch(bool sample = false, int sample_size = 1000)
{
Console.WriteLine("Processing Spimbench_large ...");
string file1 = dataset_main_location + @"\IM2016_Spimbench_large\Abox1.nt";
string file2 = dataset_main_location + @"\IM2016_Spimbench_large\Abox2.nt";
string file_gt =
dataset_main_location + @"\IM2016_Spimbench_large\refalign.rdf";
int numHashFunctions = 128;
double simThreshold = 0.3;
//ground truth file
string pair_output_filename = file1 + "_minhashpairs.txt";
string prefix1 = "|first|", prefix2 = "|second|", sep_prefix = "-";
bool exclude_sim_under_threshold = false;
//vertex pairs which have estimated similarity under the threshold will be excluded if set
MinHasher2 minHasher = new MinHasher2(numHashFunctions, simThreshold);
Dictionary<string, string[]> wordList;
Console.BufferHeight = Int16.MaxValue - 1; // ***** Alters the BufferHeight *****
Stopwatch sw = new Stopwatch();
Stopwatch sw0 = new Stopwatch();
int limit = -1;
if (sample)
{
limit = sample_size;
Console.WriteLine("Sample size: " + sample_size);
}
UobmInputReader uobmInputReader = new UobmInputReader(file1, file2, file_gt, limit, prefix1, prefix2,
sep_prefix);
wordList = uobmInputReader.wordList;
Console.WriteLine(string.Format("\r\nInstances count: {0}", wordList.Count));
//long possiblePairCount = PermutationsAndCombinations.nCr(wordList.Count, 2);
long possiblePairCount = uobmInputReader.possiblePairsCount;
sw0.Restart();
Console.WriteLine("\r\nGenerating MinHash signatures ... ");
sw.Restart();
Dictionary<string, int[]> docMinhashes = minHasher.createMinhashCollection(wordList);
sw.Stop();
Console.WriteLine("Generated MinHash signatures in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Restart();
Console.WriteLine("\r\nCreating MinHash buckets ... ");
Dictionary<string, HashSet<string>> m_lshBuckets = minHasher.createBandBuckets(wordList, docMinhashes);
Console.WriteLine("Created MinHash buckets in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
Console.WriteLine("\r\nComplexity with regular jaccard lookup(estimate): " + Math.Pow(wordList.Count, 3)/5);
sw.Restart();
Console.WriteLine("\r\nGenerating vertex pairs using MinHash buckets ... ");
Dictionary<string, Tuple<string, string, double>> pairsDictionary =
minHasher.generateVertexPairs(m_lshBuckets, docMinhashes, wordList, exclude_sim_under_threshold, null,
true, prefix1, prefix2, sep_prefix);
Console.WriteLine("Generated vertex pairs using MinHash buckets in Time : " +
sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
sw0.Stop();
Console.WriteLine("\r\nTook total time of: " + sw0.Elapsed.ToString("mm\\:ss\\.ff"));
int foundPairsCount = pairsDictionary.Count;
double prunePercentage = ((double)(possiblePairCount - foundPairsCount) / (double)possiblePairCount) * 100.0;
Cluster<string, string> cls = new Cluster<string, string>(pairsDictionary, null);
sw.Restart();
double precision_from_actualSimilarity = cls.calculatePrecision_fromActualSimilarity(wordList, simThreshold);
Console.WriteLine("Calculated precision from found pairs in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
sw.Restart();
Console.WriteLine("Calculating recall from ground truth:");
double recall = Util.calculateRecall<string>(uobmInputReader.gtPairsDictionary, pairsDictionary);
Console.WriteLine("Calculated recall from the algorithm vs pairwise-comparison in Time : " +
sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
double fmeasure = 2*((precision_from_actualSimilarity*recall)/(precision_from_actualSimilarity + recall));
Console.WriteLine("F-measure: " + fmeasure);
Console.WriteLine(string.Format("\r\nPossible pairs count: {0}", possiblePairCount));
Console.WriteLine(string.Format("\r\nFound pairs count: {0}", foundPairsCount));
Console.WriteLine(string.Format("\r\nPrune percentage: {0}", prunePercentage));
Console.ReadKey();
}
static void processNewsCorporaFiles_InstanceMatch(bool sample = false, int sample_size = 1000)
{
//to try minhash on news corpora file
string file = @"C:\Users\maydar\Dropbox\Semantic Study\ScabilityPaper\datasets\news aggregator\newsCorpora.csv-clean.txt";
string pair_output_filename = file + "_minhashpairs.txt";
int numHashFunctions = 130;
double simThreshold = 0.65;
bool exclude_sim_under_threshold = false; //vertex pairs which have estimated similarity under the threshold will be excluded if set
MinHasher2 minHasher = new MinHasher2(numHashFunctions, simThreshold);
Dictionary<int, string[]> wordList1, wordList2;
Dictionary<string, string[]> wordList3;
Console.BufferHeight = Int16.MaxValue - 1; // ***** Alters the BufferHeight *****
Stopwatch sw = new Stopwatch();
int[] index_locations = { 0, 1, 2 };
string sep = @"\t";
int limit = -1;
if (sample)
limit = sample_size;
SepInputReader<int, string> sepInputReader1 = new SepInputReader<int, string>(file, index_locations, sep, false, limit);
wordList1 = sepInputReader1.wordList;
SepInputReader<int, string> sepInputReader2 = new SepInputReader<int, string>(file, index_locations, sep, false, limit);
wordList2 = sepInputReader2.wordList;
Console.WriteLine("\r\nMerging the two wordLists ... ");
wordList3 = Util.mergeTwoWordLists(wordList1, wordList2);
Console.WriteLine("\r\nGenerating MinHash signatures ... ");
sw.Restart();
Dictionary<string, int[]> docMinhashes = minHasher.createMinhashCollection(wordList3);
sw.Stop();
Console.WriteLine("Generated MinHash signatures in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Restart();
Console.WriteLine("\r\nCreating MinHash buckets ... ");
Dictionary<string, HashSet<string>> m_lshBuckets = minHasher.createBandBuckets(wordList3, docMinhashes);
Console.WriteLine("Created MinHash buckets in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
Console.WriteLine("\r\nComplexity with regular jaccard lookup(estimate): " + Math.Pow(wordList3.Count, 3) / 5);
sw.Restart();
Console.WriteLine("\r\nGenerating vertex pairs using MinHash buckets ... ");
Dictionary<string, Tuple<string, string, double>> pairsDictionary = minHasher.generateVertexPairs(m_lshBuckets, docMinhashes, wordList3, exclude_sim_under_threshold, null,
true);
Console.WriteLine("Generated vertex pairs using MinHash buckets in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
Cluster<string, string> cls = new Cluster<string, string>(pairsDictionary, null);
sw.Restart();
double precision_from_actualSimilarity = cls.calculatePrecision_fromActualSimilarity(wordList3, simThreshold);
Console.WriteLine("Calculated precision from found pairs in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
if (sample)
{
sw.Restart();
Console.WriteLine("Calculating recall from actual should be pairs:");
Dictionary<string, Tuple<string, string, double>> actualPairsDictionary = Util.getActualPairsDictionary(wordList3, simThreshold);
double recall = Util.calculateRecall<string>(actualPairsDictionary, pairsDictionary);
Console.WriteLine("Calculated recall from the algorithm vs pairwise-comparison in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
/*Dictionary<string, Tuple<int, int, double>> actualMinHashPairsDictionary = Util.getActualPairsDictionary(docMinhashes, simThreshold);
Console.WriteLine("Calculating recall from actual MinHash pairs:");
recall = Util.calculateRecall<int>(actualMinHashPairsDictionary, pairsDictionary);*/
int a = 0;
}
int x = 1;
Console.ReadKey();
}
static void processNumbersTest3(bool sample = false, int sample_size = 1000)
{
int numHashFunctions = 128;
int universeSize = 1000;
double simThreshold = 0.65;
double atn = 0.05;
MinHasher2 mh2 = new MinHasher2(numHashFunctions, simThreshold);
NumberDocumentCreator numDocCreator2 = new NumberDocumentCreator(10, universeSize);
int[] a1 = numDocCreator2.createDocument(universeSize);
int[] a2 = numDocCreator2.createDocument(universeSize);
Console.WriteLine("Actual jaccaard: " + MinHasher2.calculateJaccard(a1, a2));
Console.WriteLine("MinHash jaccaard: " + MinHasher2.calculateJaccard(mh2.getMinHashSignature(a1), mh2.getMinHashSignature(a2)));
return;
MinHasher3 mh = new MinHasher3(universeSize, numHashFunctions);
MinHasher_Buckets3 mhb = new MinHasher_Buckets3(mh, simThreshold, atn);
NumberDocumentCreator numDocCreator = new NumberDocumentCreator(10, universeSize);
List<int> s1 = numDocCreator.createDocument(universeSize).ToList();
List<int> s2 = numDocCreator.createDocument(universeSize).ToList();
Console.WriteLine("Actual jaccaard: " + Jaccard.Calc(s1, s2));
Console.WriteLine("MinHash jaccaard: " + Jaccard.Calc(mh.GetMinHash(s1), mh.GetMinHash(s2)));
return;
Dictionary<int, List<int>> wordList = numDocCreator.documentCollectionList;
//Now create a MinHasher object to minhash each of the documents created above
//using 300 unique hashing functions.
//MinHasher minHasher = new MinHasher(500, 5);
Console.WriteLine("\r\nGenerating MinHash signatures ... ");
Dictionary<int, List<uint>> docMinhashes = mhb.createMinhashCollection(wordList); //minHasher.createMinhashCollection(wordList);
double avg_diff_perc_from_actual_and_minhash_jaccard = Util.calculateMinHashFunctionsAccuracy(wordList, docMinhashes);
/*StringDocumentCreator strDocCreator = new StringDocumentCreator(100, 10000);
Dictionary<int, string[]> wordList2 = strDocCreator.documentCollection;
//Now create a MinHasher object to minhash each of the documents created above
//using 300 unique hashing functions.
//MinHasher minHasher = new MinHasher(500, 5);
Console.WriteLine("\r\nGenerating MinHash signatures ... ");
Dictionary<int, int[]> docMinhashes2 = minHasher.createMinhashCollection(wordList2);
double avg_diff_perc_from_actual_and_minhash_jaccard2 = Util.calculateMinHashFunctionsAccuracy(wordList2, docMinhashes2);
*/
Console.ReadKey();
}
public static void MinHasher3TestFunc1()
{
List<int> inums1 = new List<int>();
inums1.Add(10);
inums1.Add(8);
inums1.Add(11);
inums1.Add(13);
inums1.Add(2);
inums1.Add(17);
inums1.Add(3);
inums1.Add(1);
inums1.Add(19);
inums1.Add(11);
inums1.Add(100);
inums1.Add(82);
inums1.Add(115);
inums1.Add(13);
inums1.Add(2);
inums1.Add(107);
inums1.Add(3);
inums1.Add(1);
inums1.Add(19);
inums1.Add(110);
inums1.Add(10);
inums1.Add(8);
inums1.Add(110);
inums1.Add(131);
inums1.Add(2);
inums1.Add(173);
inums1.Add(3);
inums1.Add(1);
inums1.Add(19);
inums1.Add(114);
inums1.Add(10);
inums1.Add(8);
inums1.Add(11);
inums1.Add(13);
inums1.Add(2);
inums1.Add(17);
inums1.Add(3);
inums1.Add(1);
inums1.Add(19);
inums1.Add(115);
inums1.Add(10);
inums1.Add(8);
inums1.Add(11);
inums1.Add(133);
inums1.Add(2);
inums1.Add(17);
inums1.Add(3);
inums1.Add(1);
inums1.Add(19);
inums1.Add(11);
inums1.Add(10);
inums1.Add(8);
inums1.Add(11);
inums1.Add(13);
inums1.Add(2);
inums1.Add(17);
inums1.Add(3);
inums1.Add(1);
inums1.Add(19);
inums1.Add(171);
List<int> inums2 = new List<int>();
inums2.Add(1);
inums2.Add(2);
inums2.Add(5);
inums2.Add(9);
inums2.Add(12);
inums2.Add(17);
inums2.Add(13);
inums2.Add(11);
inums2.Add(9);
inums2.Add(10);
inums2.Add(1);
inums2.Add(2);
inums2.Add(5);
inums2.Add(9);
inums2.Add(12);
inums2.Add(17);
inums2.Add(13);
inums2.Add(11);
inums2.Add(9);
inums2.Add(10);
inums2.Add(1);
inums2.Add(2);
inums2.Add(5);
inums2.Add(9);
inums2.Add(12);
inums2.Add(17);
inums2.Add(13);
inums2.Add(151);
inums2.Add(9);
inums2.Add(510);
inums2.Add(1);
inums2.Add(2);
inums2.Add(5);
inums2.Add(9);
inums2.Add(12);
inums2.Add(17);
inums2.Add(13);
inums2.Add(11);
inums2.Add(95);
inums2.Add(10);
inums2.Add(1);
inums2.Add(23);
inums2.Add(5);
inums2.Add(9);
inums2.Add(162);
inums2.Add(17);
inums2.Add(13);
inums2.Add(11);
inums2.Add(93);
inums2.Add(10);
inums2.Add(19);
inums2.Add(23);
inums2.Add(5);
inums2.Add(9);
inums2.Add(12);
inums2.Add(17);
inums2.Add(13);
inums2.Add(141);
inums2.Add(94);
inums2.Add(10);
int universeSize = Jaccard.unionSize(inums1, inums2);
MinHasher3 mh = new MinHasher3(universeSize, 135);
List<uint> hvs1 = mh.GetMinHash(inums1).ToList();
List<uint> hvs2 = mh.GetMinHash(inums2).ToList();
Console.WriteLine();
Console.WriteLine("Estimated similarity: " + mh.Similarity(hvs1, hvs2));
Console.WriteLine("Jaccard similarity: " + Jaccard.Calc(inums1, inums2));
Console.WriteLine("done");
}
public static void JaccardTest1()
{
Dictionary<int, string> wordDict = new Dictionary<int, string>();
wordDict.Add(1, "Word1");
wordDict.Add(2, "Word2");
wordDict.Add(3, "Word3");
wordDict.Add(4, "Word4");
List<int> doc1 = new List<int>();
doc1.Add(2);
doc1.Add(3);
doc1.Add(4);
doc1.Add(2);
List<int> doc2 = new List<int>();
doc2.Add(1);
doc2.Add(5);
doc2.Add(4);
doc2.Add(2);
List<int> doc3 = new List<int>();
doc3.Add(1);
Console.WriteLine("Jaccard: " + Jaccard.Calc(doc1, doc2));
Console.WriteLine("Jaccard: " + Jaccard.Calc(doc1, doc1));
Console.WriteLine("Jaccard: " + Jaccard.Calc(doc1, doc3));
}
static void processNumbersTest(bool sample = false, int sample_size = 1000)
{
//to try minhash on news corpora file
string file = @"C:\Users\maydar\Dropbox\Semantic Study\ScabilityPaper\datasets\news aggregator\newsCorpora.csv-clean.txt";
string pair_output_filename = file + "_minhashpairs.txt";
int numHashFunctions = 2000;
double simThreshold = 0.65;
bool exclude_sim_under_threshold = false; //vertex pairs which have estimated similarity under the threshold will be excluded if set
MinHasher2 minHasher = new MinHasher2(numHashFunctions, simThreshold);
NumberDocumentCreator numDocCreator = new NumberDocumentCreator(10, 100000);
Dictionary<int, int[]> wordList = numDocCreator.documentCollection;
//Now create a MinHasher object to minhash each of the documents created above
//using 300 unique hashing functions.
//MinHasher minHasher = new MinHasher(500, 5);
Console.WriteLine("\r\nGenerating MinHash signatures ... ");
Dictionary<int, int[]> docMinhashes = minHasher.createMinhashCollection(wordList);
double avg_diff_perc_from_actual_and_minhash_jaccard = Util.calculateMinHashFunctionsAccuracy(wordList, docMinhashes);
/*StringDocumentCreator strDocCreator = new StringDocumentCreator(100, 10000);
Dictionary<int, string[]> wordList2 = strDocCreator.documentCollection;
//Now create a MinHasher object to minhash each of the documents created above
//using 300 unique hashing functions.
//MinHasher minHasher = new MinHasher(500, 5);
Console.WriteLine("\r\nGenerating MinHash signatures ... ");
Dictionary<int, int[]> docMinhashes2 = minHasher.createMinhashCollection(wordList2);
double avg_diff_perc_from_actual_and_minhash_jaccard2 = Util.calculateMinHashFunctionsAccuracy(wordList2, docMinhashes2);
*/
Console.ReadKey();
}
static void generatePairsFileForRoleSim()
{
//to generate pair file for role-sim jaccard
//string rdf_flat_file = @"../../input\infobox_properties_100000_flat.txt";
//string rdf_flat_file = @"C:\Users\maydar\Documents\Visual Studio 2013\Projects\clean-v1-opt1\data-sets\university\sparql_university_4.txt_flat.txt";
//string rdf_flat_file = @"C:\Users\maydar\Documents\Visual Studio 2013\Projects\clean-v1-opt1\data-sets\Lubm\university_all.txt_flat.txt";
string rdf_flat_file =
@"C:\Users\maydar\Documents\Sony Backup\PHD\SEMANTIC STUDY\dbpedia\infobox\infobox_properties_10000000_flat.txt";
string pair_output_filename = rdf_flat_file + "_minhashpairs.txt";
int numHashFunctions = 250;
double simThreshold = 0.33;
bool exclude_sim_under_threshold = false; //vertex pairs which have estimated similarity under the threshold will be excluded if set
//MinHasher minHasher = new MinHasher(numHashFunctions, simThreshold);
MinHasher2 minHasher = new MinHasher2(numHashFunctions, simThreshold);
Console.BufferHeight = Int16.MaxValue - 1; // ***** Alters the BufferHeight *****
Stopwatch sw = new Stopwatch();
//Create a collection of n docuents with a max length of 1000 tokens
/*NumberDocumentCreator numDocCreator = new NumberDocumentCreator(10, 10000);
//Create a single test document
int[] testDoc = numDocCreator.createDocument(10000);*/
//StringDocumentCreator strDocCreator = new StringDocumentCreator(100, 10000);
//Create a single test document
//string[] testDoc = strDocCreator.createDocument(10000);
/*int testDocIndex = 1;
string[] testDoc = strDocCreator.documentCollection[testDocIndex];
double entireCount = testDoc.Length;*/
FlatInputReader flatInputReader = new FlatInputReader(rdf_flat_file);
Console.WriteLine(" ");
Console.WriteLine("\r\nGenerating MinHash signatures ... ");
sw.Restart();
Dictionary<int, int[]> docMinhashes = minHasher.createMinhashCollection(flatInputReader.vertexLabelList);
sw.Stop();
Console.WriteLine("Generated MinHash signatures in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Restart();
Console.WriteLine("\r\nCreating MinHash buckets ... ");
Dictionary<string, HashSet<int>> m_lshBuckets = minHasher.createBandBuckets(flatInputReader.vertexLabelList, docMinhashes);
Console.WriteLine("Created MinHash buckets in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
Console.WriteLine("\r\nComplexity with regular jaccard lookup(estimate): " + Math.Pow(flatInputReader.vertexLabelList.Count, 3) / 5);
sw.Restart();
Console.WriteLine("\r\nGenerating vertex pairs using MinHash buckets ... ");
Dictionary<string, Tuple<int, int, double>> pairsDictionary = minHasher.generateVertexPairs(m_lshBuckets, docMinhashes, flatInputReader.vertexLabelList, exclude_sim_under_threshold, pair_output_filename);
Console.WriteLine("Generated vertex pairs using MinHash buckets in Time : " + sw.Elapsed.ToString("mm\\:ss\\.ff"));
sw.Stop();
Console.WriteLine("\r\nBucket pairsDictionary size: " + pairsDictionary.Count);
Console.ReadKey();
}
}
}