-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.js
87 lines (80 loc) · 2.46 KB
/
parsing.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
var BEGIN = 0;
var END = 1;
var ENIA = "N".charCodeAt(0); //ñ
var ARRAYLEN = "z".charCodeAt(0) + 1;
var MINWORDLEN = 3;
var occurences = [];
function initArray() {
for(var i = 0; i < ARRAYLEN; i++){
occurences[i] = [];
for(var j = 0; j < ARRAYLEN; j++){
occurences[i][j] = 0;
}
}
}
function printArray() {
var firstLine = "";
for(var c = "a".charCodeAt(0); c < ARRAYLEN; c++){
firstLine += "\t" + String.fromCharCode(c);
}
console.log(firstLine);
for(var i = "a".charCodeAt(0); i < ARRAYLEN; i++){
var line = String.fromCharCode(i);
for(var j = "a".charCodeAt(0); j < ARRAYLEN; j++) {
line += "\t" + (occurences[i][j] * 100).toFixed(1);
}
console.log(line);
}
}
function readfile() {
initArray();
for(var icity = 0; icity < names_raw.length; icity++) {
var cityname = names_raw[icity];
//console.log(cityname);
occurences[BEGIN][cityname.charCodeAt(0)]++;
for(var ichar = 1; ichar < cityname.length; ichar++) {
occurences[cityname.charCodeAt(ichar-1)][cityname.charCodeAt(ichar)]++;
}
occurences[cityname.charCodeAt(cityname.length-1)][END]++;
}
normalizeArray();
//printArray();
}
function normalizeArray() {
for(var i = 0; i < ARRAYLEN; i++) {
var sum = 0;
for (var j1 = 0; j1 < ARRAYLEN; j1++) {
sum += occurences[i][j1];
}
for (var j2 = 0; j2 < ARRAYLEN; j2++) {
occurences[i][j2] /= sum;
}
}
}
function generateName() {
var name = "";
var current = BEGIN;
while(true) {
var choice = Math.random();
var accu = 0;
for (var i = 0; i < ARRAYLEN; i++) {
accu += occurences[current][i];
if (accu >= choice) {
if (i == END) {
if(name.length < MINWORDLEN)
break;
console.log(name);
document.getElementById("cityname").innerHTML = name + ", Peru 🇵🇪";
return;
}
current = i;
name += i == ENIA ? "ñ" : String.fromCharCode(i);
break;
}
}
if(i == ARRAYLEN)
console.warn("reached end of eval for letter " + String.fromCharCode(current) + " (" + current + ")"
+ " and choice " + choice
+ " accu was only " + accu);
}
}