-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
230 lines (204 loc) · 6.61 KB
/
script.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
"use strict";
const nullChar = "@";
const depth = 4;
let cityNames;
let markovData = {};
let loadedNames = 0;
let loadedMarkov = 0;
let result = document.querySelector("#result");
start();
function refreshLoading() {
result.innerText = "Chargement " + Math.round((loadedNames + loadedMarkov) * 50) + "%";
}
function onResize() {
document.body.style.height = window.innerHeight + 'px'; //"hack" for safari ios
}
async function start() {
onResize();
window.addEventListener("resize", onResize);
refreshLoading();
try {
let [noms, markov] = await Promise.all([
loadJSON("noms.json", (p) => loadedNames = p.loaded / (p.total || 527807)),
loadJSON("markov.json", (p) => loadedMarkov = p.loaded / (p.total || 1760271))]);
cityNames = JSON.parse(noms);
markovData = JSON.parse(markov);
} catch (e) {
console.error(e);
}
onClick();
let touchEvent = 'ontouchstart' in window ? 'touchstart' : 'click';
document.body.addEventListener(touchEvent, onClick);
document.body.addEventListener("keydown", event => {
if (event.key === "Spacebar" || event.key === " " || event.key === "ArrowRight" || event.key === "ArrowLeft" || event.key === "ArrowUp" || event.key === "ArrowDown") {
onClick();
}
});
}
async function loadJSON(url, onProgress) {
let request = new XMLHttpRequest();
return new Promise((resolve, reject) => {
request.onreadystatechange = () => {
if (request.readyState !== 4) return; //The request isn't ready
if (request.status >= 200 && request.status < 300) {
resolve(request.responseText);
} else {
reject({
status: request.status,
statusText: request.statusText
});
}
};
request.onprogress = (p) => {
onProgress(p);
refreshLoading();
};
request.overrideMimeType("application/json");
request.open('GET', url, true);
request.send(null);
});
}
function substrOrNullChar(str, index, length) { //Returns substring, negative index returns nullChar
let result = "";
if (index < 0) {
result += nullChar.repeat(-index);
result += str.substr(0, length + index);
} else {
result = str.substr(index, length);
}
return result;
}
async function onClick() {
fadeOut();
let rand = getRandomName();
await delay(100);
setAndFadeIn(rand);
}
async function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function setAndFadeIn(str) {
result.innerText = str;
fadeIn();
}
function getRandomName() {
let result = "";
let index = 0;
let nextLetter = getNextLetter(markovData[nullChar.repeat(depth)]);
while (nextLetter !== nullChar) {
result += nextLetter;
index++;
let key = substrOrNullChar(result, index - depth, depth);
if (!markovData.hasOwnProperty(key)) {
console.error(`key ${key} doesn't exist`);
result = getRandomName();
break;
}
nextLetter = getNextLetter(markovData[key]);
}
//todo: get rid of "au-en" "les-sur" "des le"... "saint saint" (tolowercase?)
let splits = splitWithDelimiters(result);
let modified = false;
//add "s"
for (let i = 0; i + 2 < splits.length; i += 2) {
if (splits[i] === "Les" || splits[i] === "les" || splits[i] === "lès" || splits[i] === "Lès" || splits[i] === "aux") {
if (splits[i + 2] === "en" || splits[i + 2] === "au" || splits[i + 2] === "et" || splits[i + 2] === "sur") {
continue;
}
if (!splits[i + 2].endsWith("s") && !splits[i + 2].endsWith("x")) {
splits[i + 2] = splits[i + 2] + "s";
modified = true;
}
}
}
if (modified) {
result = splits.join("");
}
let tooSimilar = false;
for (let name of cityNames) {
const similarity = computeSimilarity(result, name);
if (similarity.areSimilar) {
tooSimilar = true;
break;
}
}
if (tooSimilar) result = getRandomName();
return result;
}
function fadeOut() {
document.body.style.color = "white";
}
function fadeIn() {
document.body.style.color = "black";
}
function getNextLetter(token) {
let rand = Math.round(Math.random() * token.total); //todo: out of range?
let index = 0;
for (let candidate in token.counts) {
if (!token.counts.hasOwnProperty(candidate)) continue;
index += token.counts[candidate];
if (rand <= index) {
return candidate;
}
}
}
function createMarkovData(names) {
function createOrGetLetterCount(key) {
if (!tokens.hasOwnProperty(key)) {
tokens[key] = new LetterCount();
}
return tokens[key];
}
let tokens = {};
// creation of markov data
for (name of names) {
for (let i = 0; i < name.length; i++) {
let key = substrOrNullChar(name, i - depth, depth);
createOrGetLetterCount(key).addLetter(name.charAt(i));
let isLast = i + 1 >= name.length;
if (isLast) {
let lastKey = name.substr((i + 1) - depth, depth);
createOrGetLetterCount(lastKey).addLetter("@");
}
}
}
// Total computation
for (let dictProp in tokens) {
if (!tokens.hasOwnProperty(dictProp)) continue;
let count = 0;
for (let countProp in tokens[dictProp].counts) {
if (!tokens[dictProp].counts.hasOwnProperty(countProp)) continue;
count += tokens[dictProp].counts[countProp];
}
tokens[dictProp].total = count;
}
return tokens;
}
function computeSimilarity(str1, str2) {
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
if (str1 === str2) return { areSimilar: true, difference: 0 };
if (!str2.includes(str1) && !str1.includes(str2)) return { areSimilar: false };
return { areSimilar: true, difference: Math.abs(str2.length - str1.length) };
}
//splits string using '-' & ' ', keeping the original delimiter in the resulting array
function splitWithDelimiters(str) {
return str.split(/([- ])/g);
}
function splitWithoutDelimiters(str) {
return str.split(/[- ]+/);
}
class LetterCount {
constructor() {
this.counts = {};
this.total = 0;
}
addLetter(letter) {
if (this.counts.hasOwnProperty(letter)) {
this.counts[letter]++;
} else {
this.counts[letter] = 1;
}
this.total++;
}
}