forked from labexp/data_scripts_lascallesdelasmujeres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply_gender.js
214 lines (151 loc) · 5.17 KB
/
apply_gender.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
'use strict';
const womenDic = new Set();
const menDic = new Set();
const LineByLineReader = require('line-by-line');
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const args = require('yargs').argv;
const folder = (args.ciudad ? args.ciudad : 'ciudad');
let numFindWomen = 0;
let numFindMen = 0;
let numNamesor = 0;
function initDictionaryObjects(){
initWomenDic();
}
function initWomenDic(){
var lr = new LineByLineReader(path.join(__dirname, `namesDB/list_mujeres.csv`), { encoding: 'utf8', skipEmptyLines: true });
lr.on('error', function (err) {
console.log(err);
throw err;
});
lr.on('line', function (line) {
lr.pause();
//console.log(line);
womenDic.add(line);
lr.resume();
});
lr.on('end', function () {
initMenDic();
console.log('Diccionario de nombres de mujer init: OK');
});
}
function initMenDic(){
var lr = new LineByLineReader(path.join(__dirname, `namesDB/list_hombres.csv`), { encoding: 'utf8', skipEmptyLines: true });
lr.on('error', function (err) {
console.log(err);
throw err;
});
lr.on('line', function (line) {
lr.pause();
//console.log(line);
menDic.add(line);
lr.resume();
});
lr.on('end', function () {
console.log('Diccionario de nombres de hombre init: OK');
startProcess();
});
}
function startProcess(){
console.log('Starting process...');
try {
const gender_stream2 = fs.createWriteStream(path.join(__dirname, `data/${folder}/list_genderize.csv`), {'flags': 'a'});
gender_stream2.once('open', function(fd) {
initReadFile(gender_stream2);
});
} catch (err) {
console.error(err);
}
}
function initReadFile(stream){
console.log('Procesando listado de calles....');
var lr = new LineByLineReader(path.join(__dirname, `data/${folder}/list.csv`), { encoding: 'utf8', skipEmptyLines: true });
lr.on('error', function (err) {
console.log(err);
throw err;
});
lr.on('line', function (line) {
lr.pause();
var splitLine = line.split(';');
var name_surname = splitLine[1].split(' ');
var name = name_surname[0];
var surname = (name_surname.length > 1 ? name_surname[1] : '' );
var isWoman = false;
var isMan = false;
for (var word of name_surname) {
var w = prepareWord(word);
if(womenDic.has(w.toUpperCase())){
isWoman = true;
break;
} else if(menDic.has(w.toUpperCase())) {
isMan = true;
break;
}
}
if(isWoman){
//console.log("FEMALE", `${splitLine[0]}`);
stream.write(`${splitLine[0]};${splitLine[1]};${name};${surname};2;Female`);
stream.write('\n');
numFindWomen++;
lr.resume();
} else if(isMan){
//console.log("MALE", `${splitLine[0]}`);
stream.write(`${splitLine[0]};${splitLine[1]};${name};${surname};-2;Male`);
stream.write('\n');
numFindMen++;
lr.resume();
} else {
//console.log("UNKNOWN", `${splitLine[0]}`);
stream.write(`${splitLine[0]};${splitLine[1]};${name};${surname};0;Unknown`);
stream.write('\n');
numNamesor++;
lr.resume();
}
});
lr.on('end', function () {
stream.end();
console.log('--------------');
console.log('Nombres de mujer encontrados en el diccionario: ', numFindWomen);
console.log('Nombres de hombre encontrados en el diccionario: ', numFindMen);
console.log('Nombre desconocidos: ', numNamesor);
console.log('----FINISH----');
});
}
function prepareWord(str) {
let accents = 'ÀÁÂÃÄÅàáâãäåßÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
let accentsOut = "AAAAAAaaaaaaBOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz";
str = str.split('');
str.forEach((letter, index) => {
let i = accents.indexOf(letter);
if (i != -1) {
str[index] = accentsOut[i];
}
})
return str.join('');
}
initWomenDic();
function paserDBnames(){
nombres_stream.once('open', function(fd) {
initCleanListNames(nombres_stream);
});
}
function initCleanListNames(){
var lr = new LineByLineReader(path.join(__dirname, `namesDB/mujeres.csv`), { encoding: 'utf8', skipEmptyLines: true });
lr.on('error', function (err) {
console.log(err);
throw err;
});
lr.on('line', function (line) {
lr.pause();
var splitLine = line.split(',');
nombres_stream.write(`${splitLine[0]}`);
nombres_stream.write('\n');
lr.resume();
});
lr.on('end', function () {
nombres_stream.end();
console.log('----FINISH----');
});
}
//paserDBnames();