-
Notifications
You must be signed in to change notification settings - Fork 1
/
form-from-tsv.js
959 lines (862 loc) · 36.9 KB
/
form-from-tsv.js
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
var fs = require('fs');
var csv = require('fast-csv');
var sqlite3 = require("sqlite3");
var md5 = require('md5');
var csvStream, errorStream, corrected, closed, candid_id;
// candidate final db?
var db = new sqlite3.Database('./official.sqlite3');
// original db
var matches = new sqlite3.Database('./database.sqlite3');
// convert numbers
var myanmarNumbers = require('myanmar-numbers').myanmarNumbers;
// lower house constituencies - official names
var lower = require('./data/lower_house.json');
// people whose names are kinda similar
var phototraps = require("./data/phototraps.json");
// natid normalization / matching
var nat_id_sql =
"REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(norm_national_id, 'နိုင်', 'XOX'), 'နို်င', 'XOX'), ' ', ''), '-', ''), '.', ''), '/', ''), '့်', '့်'),'(', ''), ')', ''), '−', ''), '၊', ''), 'ပဘ', 'သဘ')";
// party normalization
var fixnames = require('./data/fixnames.json');
var id_by_party_name = require('./data/idbp.json');
// get the name of the state for a township
function getState (township, i) {
if (!township) {
return '';
}
var searchTownship = '';
for (var s = 0; s < township.length; s++) {
if (township.charCodeAt(s) < 65000) {
searchTownship += township[s];
}
}
searchTownship = searchTownship.replace('မဲဆန္ဒနယ်', '').replace('မြို့နယ်','').replace(/မြို့/g, '');
for (var state in lower) {
for (var c = 0; c < lower[state].constituencies.length; c++) {
if (lower[state].constituencies[c].replace('မြို့နယ်','').replace(/မြို့/g, '') === searchTownship) {
return lower[state].area;
}
}
}
}
// normalize a national ID
var normalizeNatId = function(natid) {
natid = (natid + "").replace(/နိုင်/g, 'XOX');
natid = natid.replace(/နို်င/g, 'XOX');
if (natid.indexOf('-') > -1 && natid.indexOf('XOX') === -1) {
natid = natid.replace('-', 'XOX');
}
natid = natid.replace(/\-/g, '');
natid = natid.replace(/\−/g, '');
natid = natid.replace(/\./g, '');
natid = natid.replace(/\//g, '');
natid = natid.replace(/့်/g, '့်');
natid = natid.replace(/\(/g, '');
natid = natid.replace(/\)/g, '');
natid = natid.replace(/\s/g, '');
natid = natid.replace(/၊/g, '');
natid = natid.replace(/ပဘ/g, 'သဘ');
return natid;
};
// output candidates for this house
//outputHouse('upper');
outputHouse('lower');
//outputHouse('state');
function outputHouse (house) {
phototraps = phototraps[house] || [];
var houses = {
'lower': 'ပြည်သူ့လွှတ်တော်',
'upper': 'အမျိုးသားလွှတ်တော်',
'state': 'တိုင်းဒေသကြီး/ပြည်နယ်လွှတ်တော်'
}
csvStream = csv.createWriteStream({headers: true});
corrected = fs.createWriteStream("./" + house + "_house_candidates.csv");
csvStream.pipe(corrected);
errorStream = csv.createWriteStream({headers: true});
closed = fs.createWriteStream("./" + house + "_house_unmatched.csv");
errorStream.pipe(closed);
// rebuild the official DB
db.run('DELETE FROM candidates WHERE house = ?', houses[house], function (err) {
if (err) {
throw err;
}
if (house === 'upper') {
doUpperHouse();
} else if (house === 'lower') {
doLowerHouse();
} else if (house === 'state') {
doStatesRegions();
}
});
}
/* structure
CREATE TABLE candidates (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"form_id" INTEGER,
"house" TEXT,
"full_name" TEXT,
"state" TEXT,
"constituency" TEXT,
"constituency_number" INTEGER,
"party" TEXT,
"best_consensus_id" INTEGER,
"best_entry_id" INTEGER
);
*/
function processCandidate(candidate, advance, islower, setname) {
var original_candidate = candidate.concat([]);
var con_name, con_number, state, name, party;
var house = candidate[0];
if (islower) {
if (islower === 'state') {
// states and regions house
con_name = (candidate[1].split(/\s/)[0] || '').split('(')[0].replace(/\s/g, '').replace(/့်/g, '့်');
con_number = (myanmarNumbers(candidate[1]) + "").match(/\d+/);
name = candidate[2].replace(/\s/g, '').replace(/့်/g, '့်');
party = candidate[3] || '';
} else {
// lower house
con_name = candidate[1].replace(/\s/g, '').replace(/့်/g, '့်');
con_number = 0;
name = candidate[3].replace(/\s/g, '').replace(/့်/g, '့်');
party = candidate[4];
}
state = getState(con_name);
} else {
// upper house
con_name = candidate[1].replace(/\s/g, '').split('-')[0];
state = con_name.replace(/\s/g, '').replace(/့်/g, '့်');
con_number = myanmarNumbers(candidate[1].replace(/\s/g, '').split('-')[1]);
name = candidate[2].replace(/\s/g, '').replace(/့်/g, '့်');
party = candidate[3];
}
/*
if (!con_number) {
console.log(candidate);
}
*/
// correct weird name errors in source
name = name.replace('ဦီး', 'ဦး');
// if there is no name match, candidates get a second pass at name-matching
// setname is that more-fuzzy name
var original_name = name;
/*
if (phototraps.indexOf(original_name) > -1) {
return advance('photo trap');
}
*/
if (setname) {
name = setname;
}
// remove blank chars
name = name.replace(//g, '');
party = (party || '').replace(//g, '');
con_name = (con_name || '').replace(//g, '');
state = (state || '').replace(//g, '');
// change name into a like query
// drop prefixes U and Daw
var searchname = name.trim();
if (searchname.indexOf('ဦး') === 0) {
searchname = name.replace('ဦး', '%');
} else if (searchname.indexOf('ဒေါက်တ') === 0) {
searchname = name.replace('ဒေါက်တ', '%');
} else if (searchname.indexOf('ဒေါ်') === 0) {
searchname = name.replace('ဒေါ်', '%');
} else {
searchname = '%' + name;
}
// normally this is the only normalization done on names
var full_name_sql = "REPLACE(REPLACE(full_name, ' ', ''), '့်', '့်')";
// if the first attempt did not find any name matches
// these replacements are made to combine similar-looking letters
var replaces = [
['ဂ', 'ဝ'],
['က','တ'],
['ထ','တ'],
['ယ','တ'],
['(', ''],
[')', ''],
['ာ', 'ါ'],
['့်', '့်'],
['ဥ', 'ဉ'],
['ဦ', 'ဉ'],
['ခ', 'န']
];
if (setname) {
for (var r = 0; r < replaces.length; r++) {
full_name_sql = "REPLACE(" + full_name_sql + ", '" + replaces[r][0] + "', '" + replaces[r][1] + "')";
}
}
// search our original DB for filter info
matches.all("SELECT id, full_name, form_id, party, constituency_number, constituency_name, national_id, norm_national_id, verified, house, 'consensus_form' AS source FROM consensus_forms WHERE " + full_name_sql + " LIKE ? UNION SELECT id, full_name, form_id, party, constituency_number, constituency_name, national_id, norm_national_id, verified, house, 'entry' AS source FROM entries WHERE " + full_name_sql + " LIKE ? ORDER BY source, verified DESC, form_id DESC, id DESC", [searchname, searchname], function (err, people) {
if (!people.length) {
// no results? retry with a more fuzzy name query if you can
var check_name = name;
for (var r = 0; r < replaces.length; r++) {
while (name.indexOf(replaces[r][0]) > -1) {
name = name.replace(replaces[r][0], replaces[r][1]);
}
}
if (searchname.indexOf('(ခ)') > -1) {
searchname = searchname.split('(ခ)')[0] + '%';
}
if (setname || check_name === name) {
// none of the fuzz replacements changed this candidate's name
// not worth rerunning their name
errorStream.write({
house: house,
state: state,
constituency_name: con_name,
constituency_number: con_number,
name: original_name,
party: party
});
// console.log('no matches: (' + name + ')');
return advance(original_name + ' - no matches');
} else {
// go run with fuzz
return processCandidate(original_candidate, advance, islower, name);
}
}
var natids = [];
var formids = [];
var multimatch = false;
var found_form = 0;
var best_consensus = 0;
var best_entry = 0;
var best_source = [];
for (var p = 0; p < people.length; p++) {
// normalize the national ID
var normid = normalizeNatId(people[p].norm_national_id);
if (natids.length && natids.indexOf(normid) === -1) {
// during this loop, if you find national IDs which don't match each other; you have multiple people showing up in results
// we'll use multimatch to identify how serious this is later
multimatch = true;
}
// keep track of any good national ids and photos
natids.push(normid);
// keep track of the best-result photo, entry, and consensus form
formids.push(people[p].form_id);
if (!found_form && people[p].form_id) {
found_form = people[p].form_id;
}
if (!best_entry && people[p].source === 'entry') {
best_entry = people[p].id;
if (!best_source.length) {
best_source = ['entries', best_entry];
}
}
if (!best_consensus && people[p].source === 'consensus_form') {
best_consensus = people[p].id;
if (!best_source.length) {
best_source = ['consensus_forms', best_consensus];
}
}
}
function nomatch(natids, id) {
try {
for (var n = 0; n < natids.length; n++) {
if (!(natids[n].split('XOX')[0] === id.split('XOX')[0]) && !(natids[n].split('XOX')[1] === id.split('XOX')[1])) {
// this ID was just TOO different
// definitely two or more people share this name
return true;
}
}
} catch(e) {
console.log(JSON.stringify(natids) + " - " + id);
}
return false;
}
if (multimatch) {
// ok there are multiple ids in your results
// let's see if some have a matching ID prefix or suffix
// this would make them the same person
// foundBlocker = true if we find someone whose ID is just TOO different
var foundBlocker = false;
for (var n = 0; n < natids.length - 1; n++) {
if (natids[n].length < 5 || !natids[n].match(/\d\d\d/)) {
// this ID was not a good format
// I'm not sure what to do with it
// console.log('bad nid');
foundBlocker = true;
break;
}
foundBlocker = nomatch([natids[n]], natids[n+1]);
if (foundBlocker) {
break;
}
}
if (foundBlocker) {
// there are multiple IDs, but you can get another chance to save matches
// we're going to check if there are any factors which match once (for example, only one person with this name who has the right constituency or party)
// when we're done we'll need this savePeople function
var savePeople = function (uniques) {
// uniques is a list of normal ids which equal that one candidate
formids = [];
best_entry = null;
best_source = [];
best_consensus = null;
found_form = 0;
for (var p = 0; p < people.length; p++) {
// only look at data from the person with this person's ID
var normid = normalizeNatId(people[p].norm_national_id);
if (uniques.indexOf(normid) === -1) {
continue;
}
// look for the best image, entry, consensus form
formids.push(people[p].form_id);
if (!found_form && people[p].form_id) {
found_form = people[p].form_id;
}
if (!best_entry && people[p].source === 'entry') {
best_entry = people[p].id;
if (!best_source.length) {
best_source = ['entries', best_entry];
}
}
if (!best_consensus && people[p].source === 'consensus_form') {
best_consensus = people[p].id;
if (!best_source.length) {
best_source = ['consensus_forms', best_consensus];
}
}
}
};
// before I start looking for individual matches (party, constituency), I should keep score in case one is an obvious winner across multiple categories
var points_by_id = {};
if (multimatch) {
// check if names start the same (avoid u / daw confusion)
var uniques = [];
people.map(function (person) {
var normid = normalizeNatId(person.norm_national_id);
if (original_name.substring(0, 2) === person.full_name.substring(0, 2) && original_name[original_name.length - 1] === person.full_name[person.full_name.length - 1]) {
if (normid) {
if (!uniques.length) {
// at least one person is a match
multimatch = false;
} else if (uniques.length && nomatch(uniques, normid)) {
// ok, now we are back to 2+ people; multimatch = true
multimatch = true;
}
uniques.push(normid);
}
// add a point
points_by_id[normid] = 1;
} else {
points_by_id[normid] = 0;
}
});
if (!multimatch) {
// I just ended the multimatch
// better save this record
savePeople(uniques);
}
}
if (multimatch) {
// normalize constituency name and then check for matches
var adjust_con_name = con_name.replace(/ပြည်နယ်|မြို့နယ်|တိုင်းဒေသကြီး|မဲဆန္ဒနယ်/g, '');
var uniques = [];
people.map(function (person) {
var normid = normalizeNatId(person.norm_national_id);
if (person.constituency_name.indexOf(adjust_con_name) > -1) {
if (normid) {
if (!uniques.length) {
// at least one person is a match
multimatch = false;
//} else if (nomatch(uniques, normid)) {
}
if (phototraps.indexOf(original_name) > -1) {
// ok, now we are back to 2+ people; multimatch = true
multimatch = true;
}
uniques.push(normid);
}
// add a point
points_by_id[normid]++;
}
});
if (!multimatch) {
// I just ended the multimatch
// better save this record
savePeople(uniques);
}
}
if (multimatch) {
// normalize party name and then check for matches
var adjust_party_name = party;
for (var f = 0; f < fixnames.length; f++) {
if (adjust_party_name.indexOf(fixnames[f][0]) > -1) {
partyid = fixnames[f][1];
adjust_party_name = id_by_party_name[partyid - 1][1];
}
}
var uniques = [];
people.map(function (person) {
var normid = normalizeNatId(person.norm_national_id);
if (person.party.indexOf(party) > -1 || party.indexOf(person.party) > -1 || adjust_party_name.indexOf(person.party) > -1 || person.party.indexOf(adjust_party_name) > -1) {
if (normid) {
if (!uniques.length) {
// at least one person is a match
multimatch = false;
} else if (uniques.length && nomatch(uniques, normid)) {
// ok, now we are back to 2+ people; multimatch = true
multimatch = true;
}
uniques.push(normid);
}
// add a point
points_by_id[normid]++;
}
});
if (!multimatch) {
// I just ended the multimatch
// better save this record
savePeople(uniques);
}
}
if (multimatch) {
// check for house match
var uniques = [];
people.map(function (person) {
var normid = normalizeNatId(person.norm_national_id);
if (person.house === house) {
if (normid) {
if (!uniques.length) {
// at least one person is a match
multimatch = false;
}
if (uniques.length && nomatch(uniques, normid)) {
// ok, now we are back to 2+ people; multimatch = true
multimatch = true;
}
uniques.push(normid);
}
// add a point
points_by_id[normid]++;
}
});
if (!multimatch) {
// I just ended the multimatch
// better save this record
savePeople(uniques);
}
}
if (multimatch) {
// check if only one has been verified
var uniques = [];
people.map(function (person) {
var normid = normalizeNatId(person.norm_national_id);
if (person.verified) {
if (normid) {
if (!uniques.length) {
// at least one person is a match
multimatch = false;
}
if (uniques.length && nomatch(uniques, normid)) {
// ok, now we are back to 2+ people; multimatch = true
multimatch = true;
}
uniques.push(normid);
}
// add a point
points_by_id[normid]++;
}
});
if (!multimatch) {
// I just ended the multimatch
// better save this record
savePeople(uniques);
}
}
if (multimatch && con_number) {
var adjust_party_name = party;
for (var f = 0; f < fixnames.length; f++) {
if (adjust_party_name.indexOf(fixnames[f][0]) > -1) {
partyid = fixnames[f][1];
adjust_party_name = id_by_party_name[partyid - 1][1];
}
}
var uniques = [];
people.map(function (person) {
var normid = normalizeNatId(person.norm_national_id);
if (person.party.indexOf(party) > -1 || party.indexOf(person.party) > -1 || adjust_party_name.indexOf(person.party) > -1 || person.party.indexOf(adjust_party_name) > -1) {
if (normid) {
if (!uniques.length) {
// at least one person is a match
multimatch = false;
}
if (uniques.length && nomatch(uniques, normid)) {
// ok, now we are back to 2+ people; multimatch = true
multimatch = true;
}
uniques.push(normid);
}
// add a point
points_by_id[normid]++;
}
});
if (!multimatch) {
// I just ended the multimatch
// better save this record
savePeople(uniques);
}
}
if (multimatch) {
// check for house match
var uniques = [];
people.map(function (person) {
var normid = normalizeNatId(person.norm_national_id);
if (person.constituency_number === con_number) {
if (normid) {
if (!uniques.length) {
// at least one person is a match
multimatch = false;
}
if (uniques.length && nomatch(uniques, normid)) {
// ok, now we are back to 2+ people; multimatch = true
multimatch = true;
}
uniques.push(normid);
}
// add a point
points_by_id[normid]++;
}
});
if (!multimatch) {
// I just ended the multimatch
// better save this record
savePeople(uniques);
}
}
if (multimatch) {
// final chance - score the natids by number of matches made
// look for a leader
var normids = Object.keys(points_by_id);
normids.sort(function (a, b) {
return points_by_id[b] - points_by_id[a];
});
if (points_by_id[normids[0]] > points_by_id[normids[1]] || !nomatch([points_by_id[normids[0]]], points_by_id[normids[1]])) {
var uniques = [];
for (var p = 0; p < people.length; p++) {
var normid = normalizeNatId(people[p].norm_national_id);
if (!nomatch([normid], normids[0])) {
uniques.push(normid);
}
}
savePeople(uniques);
} else {
// console.log('multiple IDs for ' + name);
errorStream.write({
house: house,
state: state,
constituency_name: con_name,
constituency_number: con_number,
name: original_name,
party: party
});
return advance('multiple IDs');
}
}
}
}
// after I find out this person's photo, I'm going to save them to the database and I'm going to output them to the CSV
var saveForm = function() {
// here's where I update the candidate DB
db.run("UPDATE candidates SET form_id = ?, best_consensus_id = ?, best_entry_id = ? WHERE id = ?", [found_form, best_consensus, best_entry, candid_id], function (err) {
if (err) {
throw err;
}
// here's all of the info that we share on the CSV
matches.get('SELECT gender, religion, ward_village, voter_list_number, dob, nationality, father, father_ethnicity, father_religion, mother, mother_ethnicity, mother_religion, occupation, education FROM ' + best_source[0] + ' WHERE id = ?', best_source[1], function (err, row) {
if (err) {
throw err;
}
// output CSV in this order
var outrow = {
candidate_id: md5([house, con_name, con_number, original_name, party].join(';')),
house: house,
state: state,
constituency_name: con_name,
constituency_number: con_number,
full_name: original_name,
photo_id: found_form,
party: party,
gender: row.gender,
birthdate: row.dob,
occupation: row.occupation,
education: row.education,
nationality: row.nationality,
religion: row.religion,
ward_village: row.ward_village,
voter_list_number: row.voter_list_number,
father: row.father,
father_nationality: row.father_ethnicity,
father_religion: row.father_religion,
mother: row.mother,
mother_nationality: row.mother_ethnicity,
mother_religion: row.mother_religion
};
csvStream.write(outrow);
advance();
});
});
};
if (found_form) {
// this is easy - if I have a photo then save it
saveForm();
} else {
// if I found a person but not their photo, search the original DB for other people with that ID who *do* have a photo
var normid;
if (!people.length) {
return advance('no people');
}
if (!best_source.length) {
return advance('no best source');
}
for (var q = 0; q < people.length; q++) {
if (people[q].source === best_source[0] && people[q].id === best_source[1]) {
normid = normalizeNatId(people[q].norm_national_id);
break;
}
}
matches.get("SELECT form_id, saved FROM entries WHERE form_id > 0 AND " + nat_id_sql + " = ? UNION SELECT form_id, saved FROM consensus_forms WHERE form_id > 0 AND " + nat_id_sql + " = ? ORDER BY saved DESC", [normid, normid], function (err, row) {
if (err) {
throw err;
}
if (row) {
//console.log('refound form: ' + row.form_id);
found_form = row.form_id;
saveForm();
} else {
// still not able to find this photo
//console.log('person but not form for ' + name);
//advance(original_name + ' - no photo');
console.log(original_name + ' - no photo?');
found_form = '0';
saveForm();
}
});
}
});
}
function doUpperHouse() {
var candidates = [];
var lastConstituency = null;
var activeConstituency = null;
csv.fromPath('./data/upper_house.tsv', { delimiter: '\t' })
.on('data', function (data) {
// delete null chars
for (var i = 0; i < data.length; i++) {
var fixprop = "";
for (var c = 0; c < data[i].length; c++) {
if (!data[i].charCodeAt(c) < 60000) {
fixprop += data[i][c];
}
}
data[i] = fixprop;
}
if (data[1]) {
activeConstituency = data[1];
//if (activeConstituency !== lastConstituency && lastConstituency) {
// csvStream.write({ state: activeConstituency, constituency_name: activeConstituency });
//}
} else {
data[1] = activeConstituency;
}
if (data.length == 5) {
data = data.slice(0, 2).concat(data.slice(3, 5));
}
if (data.length < 4) {
return;
}
candidates.push(data);
})
.on('end', function () {
function addCandidate (c) {
var candidate = candidates[c];
candidate[0] = 'အမျိုးသားလွှတ်တော်';
var con_name = candidate[1].replace(/\s/g, '').split('-')[0];
var con_number = myanmarNumbers(candidate[1].replace(/\s/g, '').split('-')[1]);
var name = candidate[2].replace(/\s/g, '').replace(/့်/g, '့်');
var party = candidate[3];
function advance(reason) {
// advance
if (reason) {
console.log(reason);
//console.log(candidates[c]);
}
c++;
if (!(c % 50)) {
console.log("-- " + c);
}
if (c < candidates.length) {
addCandidate(c);
} else {
//console.log('done upper house');
corrected.end();
errorStream.end();
//doLowerHouse();
}
}
var cvalues = "'" + ['အမျိုးသားလွှတ်တော်', con_name, con_number, name, party.replace(/'/g, '"')].join("','") + "'";
db.run('INSERT INTO candidates (house, constituency, constituency_number, full_name, party) VALUES (' + cvalues + ')', function (err) {
if (err) {
throw err;
}
candid_id = this.lastID;
processCandidate(candidate, advance);
});
}
addCandidate(0);
});
}
function doLowerHouse() {
var candidates = [];
var activeConstituency = null;
// load the UEC TSV list
// we've done some modifications and canidate rejections and overall improvements to the underlying WinResearcher file, so PLEASE do not update the TSV itself
csv.fromPath('./data/lower_house_norm.tsv', { delimiter: '\t' })
.on('data', function (data) {
// delete null chars
for (var i = 0; i < data.length; i++) {
var fixprop = "";
for (var c = 0; c < data[i].length; c++) {
if (!data[i].charCodeAt(c) < 60000) {
fixprop += data[i][c];
}
}
data[i] = fixprop;
}
// the constituency isn't repeated on every line, so I track it here
if (data[1]) {
activeConstituency = data[1];
} else {
data[1] = activeConstituency;
}
// some rows are just a constituency name, so they shouldn't be run through the candidates system
if (data.length < 4) {
return;
}
candidates.push(data);
})
.on('end', function () {
var lastConstituency = null;
// each candidate row goes through here
function addCandidate (c) {
// parsing what I know about the candidate at this time
var candidate = candidates[c];
candidate[0] = 'ပြည်သူ့လွှတ်တော်';
var con_name = candidate[1];
var name = candidate[3].replace(/\s/g, '').replace(/့်/g, '့်');
var party = candidate[4] || '';
// get state name not from the CSV but from the official JSON file
state = getState(con_name);
candidate[5] = state;
lastConstituency = con_name;
function advance(reason) {
// advance to the next candidate or finish
if (reason) {
console.log(reason);
//console.log(candidates[c]);
}
c++;
if (!(c % 50)) {
console.log("-- " + c);
}
if (c < candidates.length) {
addCandidate(c);
} else {
console.log('done');
corrected.end();
}
}
if (!name || !con_name) {
// no point in working with a nameless candidate
return advance('no name or con name');
}
// add this candidate to the final DB
var cvalues = "'" + ['ပြည်သူ့လွှတ်တော်', con_name, 0, name, party.replace(/'/g, '"')].join("','") + "'";
db.run('INSERT INTO candidates (house, constituency, constituency_number, full_name, party) VALUES (' + cvalues + ')', function (err) {
if (err) {
throw err;
}
candid_id = this.lastID;
// try to find the rest of the details on this candidate
processCandidate(candidate, advance, true);
});
}
addCandidate(0);
});
}
function doStatesRegions() {
var candidates = [];
csv.fromPath('./data/states_regions_norm.tsv', { delimiter: '\t' })
.on('data', function (data) {
// delete null chars
for (var i = 0; i < data.length; i++) {
var fixprop = "";
for (var c = 0; c < data[i].length; c++) {
if (!data[i].charCodeAt(c) < 60000) {
fixprop += data[i][c];
}
}
data[i] = fixprop;
}
if (data[1]) {
activeConstituency = data[1];
} else {
data[1] = activeConstituency;
}
if (data.length < 4) {
return;
}
candidates.push(data);
})
.on('end', function () {
var state = 'ရှမ်းပြည်နယ်';
var lastConstituency = null;
function addCandidate (c) {
var candidate = candidates[c];
candidate[0] = 'တိုင်းဒေသကြီး/ပြည်နယ် လွှတ်တော်';
var con_name = (candidate[1].split(/\s/)[0] || '').split('(')[0].replace(/\s/g, '').replace(/့်/g, '့်');
var con_number = (myanmarNumbers(candidate[1]) + "").match(/\d+/);
var name = candidate[2].replace(/\s/g, '').replace(/့်/g, '့်');
var party = candidate[3] || '';
//candidate[5] = state;
if (con_name !== lastConstituency && lastConstituency) {
csvStream.write({ state: state, constituency_name: con_name });
}
lastConstituency = con_name;
function advance(reason) {
// advance
if (reason) {
console.log(reason);
//console.log(candidates[c]);
}
c++;
if (!(c % 50)) {
console.log("-- " + c);
}
if (c < candidates.length) {
addCandidate(c);
} else {
console.log('done');
corrected.end();
}
}
if (con_name.indexOf('ပြည်နယ်') > -1 || con_name.indexOf('ဒေသကြီး') > -1) {
state = con_name;
return advance('no name or con name');
}
var cvalues = "'" + ['တိုင်းဒေသကြီး/ပြည်နယ် လွှတ်တော်', con_name, con_number, name, party.replace(/'/g, '"')].join("','") + "'";
db.run('INSERT INTO candidates (house, constituency, constituency_number, full_name, party) VALUES (' + cvalues + ')', function (err) {
if (err) {
throw err;
}
candid_id = this.lastID;
processCandidate(candidate, advance, 'state');
});
}
addCandidate(0);
});
}