-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDisambigEngine.cpp
2230 lines (1875 loc) · 82.3 KB
/
DisambigEngine.cpp
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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* DisambigEngine.cpp
*
* Created on: Dec 13, 2010
* Author: ysun
*/
#include "DisambigEngine.h"
#include "DisambigDefs.h"
#include "DisambigFileOper.h"
#include "DisambigCluster.h"
#include "DisambigRatios.h"
#include "DisambigNewCluster.h"
#include <algorithm>
#include <map>
#include <set>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <numeric>
using std::map;
using std::set;
/*
* Declaration ( and definition ) of static members in some classes.
*/
vector <string> cRecord::column_names;
vector <string> cRecord::active_similarity_names;
const cRecord * cRecord::sample_record_pointer = NULL;
const string cBlocking_Operation::delim = "##";
/*
* Aim: to check the number of columns that are supposed to be useful in a cRecord object.
* Firstname, middlename, lastname, assignee (company), latitude and city are believed to be useful.
* Other attributes, such as street and coauthor, are allowed to be missing.
* Algorithm: use " is_informative() " function to check each specified attribute, and return the sum.
*/
unsigned int cRecord::informative_attributes() const {
static const unsigned int firstname_index = cRecord::get_index_by_name(cFirstname::static_get_class_name());
static const unsigned int middlename_index = cRecord::get_index_by_name(cMiddlename::static_get_class_name());
static const unsigned int lastname_index = cRecord::get_index_by_name(cLastname::static_get_class_name());
static const unsigned int assignee_index = cRecord::get_index_by_name(cAssignee::static_get_class_name());
static const unsigned int lat_index = cRecord::get_index_by_name(cLatitude::static_get_class_name());
static const unsigned int ctry_index = cRecord::get_index_by_name(cCountry::static_get_class_name());
unsigned int cnt = 0;
this->vector_pdata.at(firstname_index)->is_informative() && (++cnt);
this->vector_pdata.at(middlename_index)->is_informative() && (++cnt);
this->vector_pdata.at(lastname_index)->is_informative() && (++cnt);
this->vector_pdata.at(assignee_index)->is_informative() && (++cnt);
this->vector_pdata.at(lat_index)->is_informative() && (++cnt);
this->vector_pdata.at(ctry_index)->is_informative() && (++cnt);
return cnt;
}
/*
* Aim: to keep updated the names of current similarity profile columns.
* Algorithm: use a static sample cRecord pointer to check the comparator status of each attribute.
* Clears the original cRecord::active_similarity_names and update with a newer one.
*/
void cRecord::update_active_similarity_names() {
cRecord::active_similarity_names.clear();
const cRecord * pr = cRecord::sample_record_pointer;
for ( vector < const cAttribute *>::const_iterator p = pr->vector_pdata.begin(); p != pr->vector_pdata.end(); ++p ) {
//std::cout << (*p)->get_class_name() << " , "; //for debug purpose
if ( (*p)->is_comparator_activated() )
cRecord::active_similarity_names.push_back((*p)->get_class_name());
}
}
/*
* Aim: a global function that performs the same functionality as the above one. However, this function is declared and callable in
* the template implementations in "DisambigDefs.h", where cRecord has not be declared yet.
*/
void cRecord_update_active_similarity_names() { cRecord::update_active_similarity_names();}
/*
* Aim: to print a record to an ostream object, such as a file stream or a standard output (std::cout).
* Algorithm: call each attribute pointer's "print( ostream )" method in the record object.
*
*/
void cRecord::print( std::ostream & os ) const {
const char lend = '\n';
for ( vector <const cAttribute *>::const_iterator p = this->vector_pdata.begin(); p != this->vector_pdata.end(); ++p )
(*p)->print( os );
os << "===============================" << lend;
}
/*
* Aim: compare (*this) record object with rhs record object, and return a similarity profile ( which is vector < unsigned int > ) for all activated columns.
* Algorithm: call each attribute pointer's "compare" method.
*/
vector <unsigned int> cRecord::record_compare(const cRecord & rhs) const {
static const bool detail_debug = false;
vector <unsigned int > rec_comp_result;
if ( detail_debug ) {
static const unsigned int uid_index = cRecord::get_index_by_name(cUnique_Record_ID::static_get_class_name());
const string debug_string = "06476708-1";
const string * ps = this->get_attrib_pointer_by_index(uid_index)->get_data().at(0);
const string * qs = rhs.get_attrib_pointer_by_index(uid_index)->get_data().at(0);
if ( *ps == debug_string || * qs == debug_string ) {
std::cout << "Before record compare: "<< std::endl;
std::cout << "-----------" << std::endl;
this->print();
std::cout << "===========" << std::endl;
rhs.print();
std::cout << std::endl << std::endl;
}
}
try{
for ( unsigned int i = 0; i < this->vector_pdata.size(); ++i ) {
try {
unsigned int stage_result = this->vector_pdata[i]->compare(*(rhs.vector_pdata[i]));
rec_comp_result.push_back( stage_result );
}
catch (const cException_No_Comparision_Function & err) {
//std::cout << err.what() << " does not have comparision function. " << std::endl; //for debug purpose
}
}
}
catch ( const cException_Interactive_Misalignment & except) {
std::cout << "Skipped" << std::endl;
rec_comp_result.clear();
}
//for debug only.
if ( detail_debug ) {
static const unsigned int uid_index = cRecord::get_index_by_name(cUnique_Record_ID::static_get_class_name());
const string debug_string = "06476708-1";
const string * ps = this->get_attrib_pointer_by_index(uid_index)->get_data().at(0);
const string * qs = rhs.get_attrib_pointer_by_index(uid_index)->get_data().at(0);
if ( *ps == debug_string || * qs == debug_string ) {
std::cout << "After record compare: "<< std::endl;
std::cout << "-----------" << std::endl;
this->print();
std::cout << "===========" << std::endl;
rhs.print();
std::cout << "..........." << std::endl;
std::cout << "Similarity Profile =";
for ( vector < unsigned int >::const_iterator t = rec_comp_result.begin(); t != rec_comp_result.end(); ++t )
std::cout << *t << ",";
std::cout << std::endl << std::endl;
}
}
return rec_comp_result;
}
/*
* Aim: compare (*this) record object with rhs record object, and returns a similarity profile for columns that
* are both activated and passed in the "attrib_indice_to_compare" vector.
* Algorithm: call each attribute pointer's "compare" method.
*
*/
vector <unsigned int> cRecord::record_compare_by_attrib_indice (const cRecord &rhs,
const vector < unsigned int > & attrib_indice_to_compare) const {
vector <unsigned int > rec_comp_result;
try{
for ( unsigned int j = 0; j < attrib_indice_to_compare.size(); ++j ) {
try {
unsigned int i = attrib_indice_to_compare.at(j);
unsigned int stage_result = this->vector_pdata[i]->compare(*(rhs.vector_pdata[i]));
rec_comp_result.push_back( stage_result );
}
catch (const cException_No_Comparision_Function & err) {
//std::cout << err.what() << " does not have comparision function. " << std::endl;
}
}
}
catch ( const cException_Interactive_Misalignment & except) {
std::cout << "Skipped" << std::endl;
rec_comp_result.clear();
}
return rec_comp_result;
}
/*
* Aim: to check the number of exact identical attributes between (*this) and rhs record objects.
* Algorithm: call each attribute pointer's "exact_compare" method.
*/
unsigned int cRecord::record_exact_compare(const cRecord & rhs ) const {
unsigned int result = 0;
for ( unsigned int i = 0; i < this->vector_pdata.size(); ++i ) {
int ans = this->vector_pdata.at(i)->exact_compare( * rhs.vector_pdata.at(i));
if ( 1 == ans )
++result;
}
return result;
}
/*
* Aim: print the record on standard output. The definition here is to avoid inlineness to allow debugging.
*/
void cRecord::print() const { this->print(std::cout);}
/*
* Aim: to clean some specific attribute pool.
* Algorithm: for those whose reference counting = 0, this operation will delete those nodes.
*
*/
void cRecord::clean_member_attrib_pool() {
for ( vector < const cAttribute *>::const_iterator p = sample_record_pointer->vector_pdata.begin();
p != sample_record_pointer->vector_pdata.end(); ++p )
(*p)->clean_attrib_pool();
}
/*
* Aim: get the index of the desired column name in the columns read from text file.
* Algorithm: exhaustive comparison. Time complexity = O(n); if no matching is found, a exception will be thrown.
*/
unsigned int cRecord::get_index_by_name(const string & inputstr) {
for ( unsigned int i = 0 ; i < column_names.size(); ++i )
if ( column_names.at(i) == inputstr )
return i;
throw cException_ColumnName_Not_Found(inputstr.c_str());
}
/*
* Aim: get the index of the desired column name in the active similarity profile columns.
* Algorithm: exhaustive comparison. Time complexity = O(n); if no matching is found, a exception will be thrown.
*/
unsigned int cRecord::get_similarity_index_by_name(const string & inputstr) {
for ( unsigned int i = 0 ; i < active_similarity_names.size(); ++i )
if ( active_similarity_names.at(i) == inputstr )
return i;
throw cException_ColumnName_Not_Found(inputstr.c_str());
}
void cRecord::activate_comparators_by_name ( const vector < string > & inputvec) {
cRecord::active_similarity_names = inputvec;
for ( vector < const cAttribute *>::const_iterator p = cRecord::sample_record_pointer->get_attrib_vector().begin();
p != cRecord::sample_record_pointer->get_attrib_vector().end(); ++p ) {
const string & classlabel = (*p)->get_class_name();
if ( std::find( inputvec.begin(), inputvec.end(), classlabel) == inputvec.end() ) {
(*p)->deactivate_comparator();
}
else {
(*p)->activate_comparator();
}
}
cRecord::update_active_similarity_names();
}
/*
* Aim: to truncate string as desired. See the explanation in the header file for more details
* Algorithm: simple string manipulation in C.
*/
string cString_Truncate::manipulate( const string & inputstring ) const {
if ( ! is_usable )
throw cException_Blocking_Disabled("String Truncation not activated yet.");
if ( 0 == nchar ) {
if ( is_forward )
return inputstring;
else {
return string("");
}
}
if ( inputstring.size() == 0 )
return inputstring;
char * p = new char[ nchar + 1];
const char * res = p;
const char * source;
if ( begin >= 0 && static_cast<unsigned int >(begin) < inputstring.size() )
source = &inputstring.at(begin);
else if ( begin < 0 && ( begin + inputstring.size() >= 0 ) )
source = &inputstring.at( begin + inputstring.size() );
else {
delete [] p;
throw cString_Truncate::cException_String_Truncation(inputstring.c_str());
}
if ( is_forward) {
for ( unsigned int i = 0; i < nchar && *source !='\0'; ++i )
*p++ = *source++;
*p = '\0';
}
else {
for ( unsigned int i = 0; i < nchar && source != inputstring.c_str() ; ++i )
*p++ = *source--;
*p = '\0';
}
string result (res);
delete [] res;
return result;
}
/*
* Aim: to extract initials of each word in a string, maybe not starting from the first word.
* See the explanation in the header file for more details
* Algorithm: simple string manipulation in C.
*/
string cExtract_Initials::manipulate( const string & inputstring ) const {
size_t pos, prev_pos;
pos = prev_pos = 0;
if ( inputstring.empty() )
return string();
list < char > tempres;
do {
tempres.push_back(inputstring.at(prev_pos) );
pos = inputstring.find(delimiter, prev_pos );
prev_pos = pos + 1;
} while ( pos != string::npos );
const unsigned int word_count = tempres.size();
if ( word_count >= starting_word ) {
for ( unsigned int i = 0; i < starting_word; ++i )
tempres.pop_front();
}
return string(tempres.begin(), tempres.end());
}
/*
* Aim: to extract the first word of a string.
* Algorithm: STL string operations.
*/
string cString_Extract_FirstWord::manipulate( const string & inputstring ) const {
string res = inputstring.substr(0, inputstring.find(delimiter, 0));
return res;
}
/*
* Aim: constructors of cBlocking_Operation_Multiple_Column_Manipulate class. Look at the header file for more information.
*/
cBlocking_Operation_Multiple_Column_Manipulate::cBlocking_Operation_Multiple_Column_Manipulate (const vector < const cString_Manipulator * > & inputvsm, const vector<string> & columnnames, const vector < unsigned int > & di )
:vsm(inputvsm), attributes_names(columnnames) {
if ( inputvsm.size() != columnnames.size() )
throw cException_Other("Critical Error in cBlocking_Operation_Multiple_Column_Manipulate: size of string manipulaters is different from size of columns");
for ( unsigned int i = 0; i < columnnames.size(); ++i ) {
indice.push_back(cRecord::get_index_by_name( columnnames.at(i)));
infoless += delim;
pdata_indice.push_back( di.at(i));
}
}
cBlocking_Operation_Multiple_Column_Manipulate::cBlocking_Operation_Multiple_Column_Manipulate (const cString_Manipulator * const* pinputvsm, const string * pcolumnnames, const unsigned int * pdi, const unsigned int num_col ) {
for ( unsigned int i = 0; i < num_col; ++i ) {
vsm.push_back(*pinputvsm++);
attributes_names.push_back(*pcolumnnames);
indice.push_back(cRecord::get_index_by_name(*pcolumnnames++));
infoless += delim;
pdata_indice.push_back(*pdi ++);
}
}
/*
* Aim: to extract blocking information from a record pointer and returns its blocking id string.
* Algorithm: call the polymorphic methods by cString_Manipulator pointers to create strings, and concatenate them.
*/
string cBlocking_Operation_Multiple_Column_Manipulate::extract_blocking_info(const cRecord * p) const {
string temp;
for ( unsigned int i = 0; i < vsm.size(); ++i ) {
temp += vsm[i]->manipulate(* p->get_data_by_index(indice[i]).at( pdata_indice.at(i)));
temp += delim;
}
return temp;
};
void cBlocking_Operation_Multiple_Column_Manipulate::reset_data_indice ( const vector < unsigned int > & indice ) {
if ( indice.size() != this->pdata_indice.size() )
throw cException_Other("Indice size mismatch. cannot reset.");
else
this->pdata_indice = indice;
}
/*
* Aim: to extract a specific blocking string. look at the header file for mor details.
*/
string cBlocking_Operation_Multiple_Column_Manipulate::extract_column_info ( const cRecord * p, unsigned int flag ) const {
if ( flag >= indice.size() )
throw cException_Other("Flag index error.");
return vsm[flag]->manipulate( * p->get_data_by_index(indice[flag]).at( pdata_indice.at(flag)) );
}
/*
* Aim: constructors of cBlocking_Operation_By_Coauthors.
* The difference between the two constructors is that the former one builds unique record id to unique inventor id binary tree,
* whereas the latter does not, demanding external explicit call of the building of the uid2uinv tree.
*/
cBlocking_Operation_By_Coauthors::cBlocking_Operation_By_Coauthors(const list < const cRecord * > & all_rec_pointers,
const cCluster_Info & cluster, const unsigned int coauthors)
: patent_tree(cSort_by_attrib(cPatent::static_get_class_name())), num_coauthors(coauthors) {
if ( num_coauthors > 4 ) {
std::cout << "================ WARNING =====================" << std::endl;
std::cout << "Number of coauthors in which cBlocking_Operation_By_Coauthors uses is probably too large. Number of coauthors = " << num_coauthors << std::endl;
std::cout << "==================END OF WARNING ================" << std::endl;
}
build_patent_tree(all_rec_pointers);
build_uid2uinv_tree(cluster);
for ( unsigned int i = 0; i < num_coauthors; ++i ) {
infoless += cBlocking_Operation::delim;
infoless += cBlocking_Operation::delim;
}
}
cBlocking_Operation_By_Coauthors::cBlocking_Operation_By_Coauthors(const list < const cRecord * > & all_rec_pointers, const unsigned int coauthors)
: patent_tree(cSort_by_attrib(cPatent::static_get_class_name())), num_coauthors(coauthors) {
if ( num_coauthors > 4 ) {
std::cout << "================ WARNING =====================" << std::endl;
std::cout << "Number of coauthors in which cBlocking_Operation_By_Coauthors uses is probably too large. Number of coauthors = " << num_coauthors << std::endl;
std::cout << "==================END OF WARNING ================" << std::endl;
}
build_patent_tree(all_rec_pointers);
for ( unsigned int i = 0; i < num_coauthors; ++i ) {
infoless += cBlocking_Operation::delim + cBlocking_Operation::delim;
infoless += cBlocking_Operation::delim + cBlocking_Operation::delim;
}
}
/*
* Aim: to create a binary tree of patent -> patent holders to allow fast search.
* Algorithm: create a patent->patent holder map (std::map), and for any given const cRecord pointer p, look for the patent attribute of p in
* the map. If the patent attribute is not found, insert p into the map as a key, and insert a list of const cRecord pointers which includes p as
* a value of the key; if the patent attribute is found, find the corresponding value ( which is a list ), and append p into the list.
*
*/
void cBlocking_Operation_By_Coauthors::build_patent_tree(const list < const cRecord * > & all_rec_pointers) {
map < const cRecord *, cGroup_Value, cSort_by_attrib >::iterator ppatentmap;
for ( list < const cRecord * >::const_iterator p = all_rec_pointers.begin(); p != all_rec_pointers.end(); ++p ) {
ppatentmap = patent_tree.find(*p);
if ( ppatentmap == patent_tree.end() ) {
cGroup_Value temp ( 1, *p);
patent_tree.insert( std::pair < const cRecord *, cGroup_Value > (*p, temp) );
}
else {
ppatentmap->second.push_back(*p);
}
}
}
/*
* Aim: to create binary tree of unique record id -> unique inventor id, also to allow fast search and insertion/deletion.
* the unique inventor id is also a const cRecord pointer, meaning that different unique record ids may be associated with a same
* const cRecord pointer that represents them.
* Algorithm: clean the uinv2count and uid2uinv tree first.
* For any cluster in the cCluser_Info object:
* For any const cRecord pointer p in the cluster member list:
* create a std::pair of ( p, d ), where d is the delegate ( or representative ) of the cluster
* insert the pair into uid2uinv map.
* End for
* End for
* uinv2count is updated in the same way.
*/
void cBlocking_Operation_By_Coauthors::build_uid2uinv_tree( const cCluster_Info & cluster ) {
uinv2count_tree.clear();
uid2uinv_tree.clear();
unsigned int count = 0;
typedef list<cCluster> cRecGroup;
std::cout << "Building trees: 1. Unique Record ID to Unique Inventer ID. 2 Unique Inventer ID to Number of holding patents ........" << std::endl;
//for ( map < string, cRecGroup >::const_iterator p = cluster.get_cluster_map().begin(); p != cluster.get_cluster_map().end(); ++p ) {
for ( map < string, cRecGroup >::const_iterator p = cluster.get_cluster_map().begin(); p != cluster.get_cluster_map().end(); ++p ) {
for ( cRecGroup::const_iterator q = p->second.begin(); q != p->second.end(); ++q ) {
const cRecord * value = q->get_cluster_head().m_delegate;
map < const cRecord *, unsigned int >::iterator pcount = uinv2count_tree.find(value);
if ( pcount == uinv2count_tree.end() )
pcount = uinv2count_tree.insert(std::pair<const cRecord *, unsigned int>(value, 0)).first;
for ( cGroup_Value::const_iterator r = q->get_fellows().begin(); r != q->get_fellows().end(); ++r ) {
const cRecord * key = *r;
uid2uinv_tree.insert(std::pair< const cRecord * , const cRecord *> (key, value ));
++ ( pcount->second);
++count;
}
}
}
std::cout << count << " nodes has been created inside the tree." << std::endl;
}
/*
* Aim: to get a list of top N coauthors ( as represented by a const cRecord pointer ) of an inventor to whom prec ( a unique record id ) belongs.
* Algorithm:
* 1. create a binary tree T( std::map ). Key = number of holding patents, value = const cRecord pointer to the unique inventor.
* 2. For any associated record r:
* find r in the uinv2count tree.
* if number of nodes in T < N, insert (count(r), r) into T;
* else
* if count(r) > front of T:
* delete front of T from T
* insert ( count(r), r );
* 3. return values in T.
*
*/
cGroup_Value cBlocking_Operation_By_Coauthors::get_topN_coauthors( const cRecord * prec, const unsigned int topN ) const {
const cGroup_Value & list_alias = patent_tree.find(prec)->second;
map < unsigned int, cGroup_Value > occurrence_map;
unsigned int cnt = 0;
for ( cGroup_Value::const_iterator p = list_alias.begin(); p != list_alias.end(); ++p ) {
if ( *p == prec )
continue;
map < const cRecord *, const cRecord * >::const_iterator puid2uiv = uid2uinv_tree.find(*p);
if ( puid2uiv == uid2uinv_tree.end() )
throw cException_Other("Critical Error: unique record id to unique inventer id tree is incomplete!!");
const cRecord * coauthor_pointer = puid2uiv->second;
map < const cRecord *, unsigned int >::const_iterator puinv2count = uinv2count_tree.find(coauthor_pointer);
if ( puinv2count == uinv2count_tree.end())
throw cException_Other("Critical Error: unique inventer id to number of holding patents tree is incomplete!!");
const unsigned int coauthor_count = puinv2count->second;
if ( cnt <= topN || coauthor_count > occurrence_map.begin() ->first ) {
map < unsigned int, cGroup_Value >::iterator poccur = occurrence_map.find ( coauthor_count );
if ( poccur == occurrence_map.end() ) {
cGroup_Value temp (1, coauthor_pointer);
occurrence_map.insert(std::pair<unsigned int, cGroup_Value>(coauthor_count, temp));
}
else
poccur->second.push_back(coauthor_pointer);
if ( cnt < topN )
++cnt;
else {
map < unsigned int, cGroup_Value >::iterator pbegin = occurrence_map.begin();
pbegin->second.pop_back();
if ( pbegin->second.empty() )
occurrence_map.erase(pbegin);
}
}
}
//output
cGroup_Value ans;
for ( map < unsigned int, cGroup_Value>::const_reverse_iterator rp = occurrence_map.rbegin(); rp != occurrence_map.rend(); ++rp )
ans.insert(ans.end(), rp->second.begin(), rp->second.end());
return ans;
}
/*
* Aim: to get the blocking string id for prec.
* Algorithm: see get_topN_coauthor
*/
string cBlocking_Operation_By_Coauthors::extract_blocking_info(const cRecord * prec) const {
const cGroup_Value top_coauthor_list = get_topN_coauthors(prec, num_coauthors);
// now make string
const unsigned int firstnameindex = cRecord::get_index_by_name(cFirstname::static_get_class_name());
const unsigned int lastnameindex = cRecord::get_index_by_name(cLastname::static_get_class_name());
string answer;
for ( cGroup_Value::const_iterator p = top_coauthor_list.begin(); p != top_coauthor_list.end(); ++p ) {
answer += *(*p)->get_data_by_index(firstnameindex).at(0);
answer += cBlocking_Operation::delim;
answer += *(*p)->get_data_by_index(lastnameindex).at(0);
answer += cBlocking_Operation::delim;
}
if ( answer.empty() )
answer = infoless;
return answer;
}
/*
* Aim: constructor of cReconfigurator_AsianNames.
*/
cReconfigurator_AsianNames::cReconfigurator_AsianNames(): country_index(cRecord::get_index_by_name(cCountry::static_get_class_name())),
firstname_index(cRecord::get_index_by_name(cFirstname::static_get_class_name())),
middlename_index(cRecord::get_index_by_name(cMiddlename::static_get_class_name())),
lastname_index(cRecord::get_index_by_name(cLastname::static_get_class_name())){
east_asian.push_back(string("KR"));
east_asian.push_back(string("CN"));
east_asian.push_back(string("TW"));
}
/*
* Aim: reconfigure the asian name in accordance with the current scoring system.
* Algorithm: check the country first. If the country is in [ "KR", "CN", "TW"], do this operation:
* 1. create a vector of string, and save FULL first name into the vector. Then create a firstname attribute from the vector
* 2. create a vector of string, and save "FULL firstname" + "FULL lastname" into the vector. Then create a middlename attribute from the vector.
* 3. change the cRecord data such that the firstname pointer points to the newly created firstname object. So for the middle name.
*/
void cReconfigurator_AsianNames::reconfigure( const cRecord * p ) const {
bool need_reconfigure = false;
const string & country = * p->get_attrib_pointer_by_index(country_index)->get_data().at(0) ;
for ( register vector<string>::const_iterator ci = east_asian.begin(); ci != east_asian.end(); ++ci )
if ( country == *ci ) {
need_reconfigure = true;
break;
}
if ( need_reconfigure == false )
return;
// do not change original attributes. add new ones.
const cAttribute * cur_af = p->get_attrib_pointer_by_index(firstname_index);
const string & fn_alias = * cur_af ->get_data().at(0);
const vector <string> fn ( 2, fn_alias );
const cAttribute * paf = cFirstname::static_clone_by_data(fn);
const cAttribute * cur_am = p->get_attrib_pointer_by_index(middlename_index);
const string & lnstr = * p->get_attrib_pointer_by_index(lastname_index)->get_data().at(0);
const string mnstr ( fn_alias + "." + lnstr);
const vector < string > mn(2, mnstr);
const cAttribute * pam = cMiddlename ::static_clone_by_data(mn);
cRecord * q = const_cast < cRecord * > (p);
cur_af->reduce_attrib(1);
cur_am->reduce_attrib(1);
q->set_attrib_pointer_by_index(paf, firstname_index);
q->set_attrib_pointer_by_index(pam, middlename_index);
}
cReconfigurator_Interactives::cReconfigurator_Interactives( const string & my_name,
const vector < string > & relevant_attribs ) {
my_index = cRecord::get_index_by_name(my_name);
for ( vector<string>::const_iterator p = relevant_attribs.begin(); p != relevant_attribs.end(); ++p ) {
unsigned int idx = cRecord::get_index_by_name(*p);
relevant_indice.push_back(idx);
}
}
void cReconfigurator_Interactives::reconfigure ( const cRecord * p ) const {
vector < const cAttribute * > interact;
for ( vector < unsigned int >::const_iterator i = relevant_indice.begin(); i != relevant_indice.end(); ++i ) {
interact.push_back(p->get_attrib_pointer_by_index(*i));
}
const cAttribute * const & tp = p->get_attrib_pointer_by_index(my_index);
const cAttribute * & cp = const_cast< const cAttribute * &> (tp);
cp = tp->config_interactive(interact);
}
/*
* Aim: constructor of cReconfigurator_Coauthor
* Algorithm: The constructor calls cCoauthor::clear_data_pool() and cCoauthor::clear_attrib_pool(). So it is critical to remember that
* construction of each cReconfigurator_Coauthor object will DESTROY all the information of any cCoauthor class and INVALIDATE any pointers
* that point to a cCoauthor object. Therefore, creation of cReconfigurator_Coauthor objects shall NEVER happen during disambiguation.
*
*/
cReconfigurator_Coauthor::cReconfigurator_Coauthor ( const map < const cRecord *, cGroup_Value, cSort_by_attrib > & patent_authors) :
reference_pointer ( & patent_authors), coauthor_index ( cRecord::get_index_by_name(cCoauthor::static_get_class_name())) {
cCoauthor::clear_data_pool();
cCoauthor::clear_attrib_pool();
}
/*
* Aim: to recreate the whole coauthor database, and link them to appropriate pointers.
* Algorithm: for each unique record id, find all other unique id records with which this one is associated. Then extract the FULL names
* of those records, and save in cCoauthor data pool. After that, reset the pointer of this unique record to point to the newly built
* attribute.
*/
void cReconfigurator_Coauthor :: reconfigure ( const cRecord * p ) const {
static const string dot = ".";
static const unsigned int firstnameindex = cRecord::get_index_by_name(cFirstname::static_get_class_name());
static const unsigned int lastnameindex = cRecord::get_index_by_name(cLastname::static_get_class_name());
static const cString_Extract_FirstWord firstname_extracter;
static const cString_Remove_Space lastname_extracter;
map < const cRecord *, cGroup_Value, cSort_by_attrib >::const_iterator cpm;
cCoauthor temp;
cpm = reference_pointer->find( p);
if ( cpm == reference_pointer->end())
throw cException_Other("Missing patent data.");
const cGroup_Value & patent_coauthors = cpm->second;
for ( cGroup_Value::const_iterator q = patent_coauthors.begin(); q != patent_coauthors.end(); ++q ) {
if ( *q == p )
continue;
string fullname = firstname_extracter.manipulate( * (*q)->get_data_by_index(firstnameindex).at(0) ) + dot
+ lastname_extracter.manipulate( * (*q)->get_data_by_index(lastnameindex).at(0) );
temp.attrib_set.insert(cCoauthor::static_add_string (fullname) );
}
const cAttribute * np = cCoauthor::static_add_attrib(temp, 1);
const cAttribute ** to_change = const_cast< const cAttribute ** > ( & p->get_attrib_pointer_by_index(coauthor_index));
*to_change = np;
}
/*
* Aim: to find a ratio that corresponds to a given similarity profile in a given similarity profile binary tree.
* Algorithm: STL map find.
*/
double fetch_ratio(const vector < unsigned int > & ratio_to_lookup, const map < vector < unsigned int>, double, cSimilarity_Compare > & ratiosmap ) {
map < vector < unsigned int >, double, cSimilarity_Compare >::const_iterator p = ratiosmap.find( ratio_to_lookup);
if ( p == ratiosmap.end())
return 0;
else
return p->second;
}
#if 0
/*
* old legacy stuff. disabled now but could be useful.
*/
std::pair<const cRecord *, double> disambiguate_by_set_old (
const cRecord * key1, const cGroup_Value & match1, const double cohesion1,
const cRecord * key2, const cGroup_Value & match2, const double cohesion2,
const double prior,
const cRatios & ratio, const double mutual_threshold ) {
const double minimum_threshold = 0.8;
const double threshold = max_val <double> (minimum_threshold, mutual_threshold * cohesion1 * cohesion2);
static const cException_Unknown_Similarity_Profile except(" Fatal Error in Disambig by set.");
const unsigned int size_minimum = 50;
double r_value = 0;
if ( match1.size() > size_minimum && match2.size() > size_minimum ) {
vector< unsigned int > sp = key1->record_compare(*key2);
//if ( power == 0 )
r_value = fetch_ratio(sp, ratio.get_ratios_map());
//else
// throw cException_Other("Error in disambiguate_by_set: invalid input parameter.");
//r_value = fetch_ratio(sp, ratio.get_coefficients_vector(), power);
if ( 0 == r_value ) {
std::cout << " OK. Disabled now but should be enabled in the real run." << std::endl;
return std::pair<const cRecord *, double> (NULL, 0);
//throw except;
}
const double probability = 1 / ( 1 + ( 1 - prior )/prior / r_value );
if ( probability < threshold )
return std::pair<const cRecord *, double> (NULL, probability);
else
return std::pair<const cRecord *, double> ( ( ( match1.size() > match2.size() ) ? key1 : key2 ), probability );
}
else {
vector < const cRecord *> merged;
cGroup_Value::const_iterator q = match1.begin();
if ( key1 != NULL)
merged.push_back(key1);
for ( unsigned int i = 0; i < size_minimum && i < match1.size(); ++i ) {
if (*q != key1 )
merged.push_back(*q++);
}
q = match2.begin();
if ( key2 != NULL )
merged.push_back(key2);
for ( unsigned int i = 0; i < size_minimum && i < match2.size(); ++i ) {
if (*q != key2)
merged.push_back(*q++);
}
//const bool should_adjust = ( merged.size() > size_minimum );
double max_sum = 0;
double matrix_sum = 0;
unsigned int chosen_i = 0;
unsigned int crude_chosen_i = 0;
const unsigned int size = merged.size();
unsigned int num_useful_columns = 0;
for (unsigned int i = 0; i < size; ++i ) {
unsigned int informative_columns = merged[i]->informative_attributes();
if ( informative_columns > num_useful_columns )
num_useful_columns = informative_columns;
}
bool is_updated = false;
vector <unsigned int> max_history;
for (unsigned int i = 0; i < size; ++i ) {
double temp_sum = 0;
unsigned int informative_columns = merged[i]->informative_attributes();
for (unsigned int j = 0; j < size; ++j ) {
if ( i == j )
continue;
vector< unsigned int > tempsp = merged[i]->record_compare(* merged[j]);
//if ( power == 0 )
r_value = fetch_ratio(tempsp, ratio.get_ratios_map());
//else
//r_value = fetch_ratio(tempsp, ratio.get_coefficients_vector(), power);
if ( r_value == 0 ) {
/*
std::cout << "Missing Similarity Profile : ";
for ( vector < unsigned int >::const_iterator qq = tempsp.begin(); qq != tempsp.end(); ++qq )
std::cout << *qq << ", ";
std::cout << std::endl;
*/
//throw except;
temp_sum += 0;
}
else {
temp_sum += 1 / ( 1 + ( 1 - prior )/prior / r_value );
}
}
if ( temp_sum > max_sum ) {
if ( informative_columns == num_useful_columns ) {
max_sum = temp_sum;
chosen_i = i;
is_updated = true;
}
crude_chosen_i = i;
max_history.clear();
}
if ( temp_sum == max_sum ) {
max_history.push_back(i);
}
matrix_sum += temp_sum;
}
vector<bool> shall_use(max_history.size(), true);
const double probability = matrix_sum / size / (size - 1 );
if ( probability > 1 )
throw cException_Invalid_Probability("Cohesion value error.");
if ( probability > threshold ) {
//double check chosen_i;
if ( is_updated ) {
for ( unsigned int k = 0; k != max_history.size(); ++k) {
const unsigned int seq = max_history.at(k);
double another_probability = 0;
for ( unsigned int i = 0; i < size; ++i ) {
if ( i == seq )
continue;
vector< unsigned int > tempsp = merged[i]->record_compare(* merged[seq]);
r_value = fetch_ratio(tempsp, ratio.get_ratios_map());
if ( r_value == 0 )
another_probability += 0;
else
another_probability += 1 / ( 1 + ( 1 - prior )/prior / r_value );
}
another_probability /= ( size - 1 ) ;
if ( another_probability < threshold )
shall_use.at(k) = false;
}
}
//compare each chosen i s
is_updated = false;
for ( vector<bool>::const_iterator bi= shall_use.begin(); bi != shall_use.end(); ++bi )
if ( *bi == true ) {
is_updated = true;
break;
}
if ( is_updated ) {
unsigned int max_exact_match = 0;
for ( unsigned int i = 0; i < max_history.size(); ++i ) {
if ( shall_use.at(i) == false )
continue;
unsigned int temp_c = 0;
for ( vector < const cRecord *>::const_iterator p = merged.begin(); p != merged.end(); ++p ) {
temp_c += merged.at( max_history.at(i) )->record_exact_compare( **p );
}
if ( temp_c > max_exact_match ) {
max_exact_match = temp_c;
chosen_i = max_history.at(i);
}
}
}
const unsigned int ok = ( is_updated ) ? chosen_i : crude_chosen_i;
return std::pair<const cRecord *, double>( merged[ok], probability );
}
else
return std::pair<const cRecord *, double> (NULL, probability);
}
}
//==================================================================================================
//There are probably some bugs in this function. Use old one as an expediency.
std::pair<const cRecord *, double> disambiguate_by_set_old2 (
const cRecord * key1, const cGroup_Value & match1, const double cohesion1,
const cRecord * key2, const cGroup_Value & match2, const double cohesion2,
const double prior,
const cRatios & ratio, const double mutual_threshold ) {
const double minimum_threshold = 0.8;
const double threshold = max_val <double> (minimum_threshold, mutual_threshold * cohesion1 * cohesion2);
static const cException_Unknown_Similarity_Profile except(" Fatal Error in Disambig by set.");
const unsigned int size_minimum = 50;
double r_value = 0;
if ( match1.size() > size_minimum && match2.size() > size_minimum ) {
vector< unsigned int > sp = key1->record_compare(*key2);
//if ( power == 0 )
r_value = fetch_ratio(sp, ratio.get_ratios_map());
//else
// throw cException_Other("Error in disambiguate_by_set: invalid input parameter.");
//r_value = fetch_ratio(sp, ratio.get_coefficients_vector(), power);
if ( 0 == r_value ) {
std::cout << " OK. Disabled now but should be enabled in the real run." << std::endl;
return std::pair<const cRecord *, double> (NULL, 0);
//throw except;
}
const double probability = 1 / ( 1 + ( 1 - prior )/prior / r_value );
if ( probability < threshold )
return std::pair<const cRecord *, double> (NULL, probability);
else
return std::pair<const cRecord *, double> ( ( ( match1.size() > match2.size() ) ? key1 : key2 ), probability );
}
else {
vector < const cRecord *> merged;
cGroup_Value::const_iterator q = match1.begin();
if ( key1 != NULL)
merged.push_back(key1);
for ( unsigned int i = 0; i < size_minimum && i < match1.size(); ++i ) {
if (*q != key1 )
merged.push_back(*q++);
}
q = match2.begin();
if ( key2 != NULL )
merged.push_back(key2);
for ( unsigned int i = 0; i < size_minimum && i < match2.size(); ++i ) {
if (*q != key2)
merged.push_back(*q++);
}
//const bool should_adjust = ( merged.size() > size_minimum );
double max_sum = 0;
double matrix_sum = 0;
unsigned int chosen_i = 0;
//unsigned int crude_chosen_i = 0;
const unsigned int size = merged.size();
unsigned int num_useful_columns = 0;
vector < unsigned int > qualified_index;
for (unsigned int i = 0; i < size; ++i ) {
unsigned int informative_columns = merged[i]->informative_attributes();
if ( informative_columns > num_useful_columns ) {
num_useful_columns = informative_columns;
qualified_index.clear();
}
if ( informative_columns == num_useful_columns )
qualified_index.push_back(i);
}
vector < double > max_history( qualified_index.size(), 0);
//bool is_updated = false;
vector < unsigned int > :: iterator cvi = qualified_index.begin();
//unsigned int location = 0;
for (unsigned int i = 0; i < size; ++i ) {
//for ( vector < unsigned int >:: const_iterator cvi = qualified_index.begin(); cvi != qualified_index.end(); ++cvi ) {
//const unsigned int i = *cvi;
double temp_sum = 0;
//unsigned int informative_columns = merged[i]->informative_attributes();
for (unsigned int j = 0; j < size; ++j ) {
if ( i == j )
continue;
vector< unsigned int > tempsp = merged[i]->record_compare(* merged[j]);
r_value = fetch_ratio(tempsp, ratio.get_ratios_map());
if ( r_value == 0 ) {
temp_sum += 0;
}
else {
temp_sum += 1 / ( 1 + ( 1 - prior )/prior / r_value );
}
}
cvi = std::find( qualified_index.begin(), qualified_index.end(), i);