-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathverse.js
360 lines (326 loc) · 8.98 KB
/
verse.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
import {Diagonal} from './utils.js';
import {CUSTOM_SLUG} from './constants.js';
/** A sequence of repeated words. */
class Lala {
constructor(start, end, word) {
this.start = start;
this.end = end;
this.word = word;
this.length = 1 + (this.end - this.start);
}
includes(i) {
return (this.start <= i && i <= this.end);
}
}
class VerseMatrix {
/** TODO: implementation isn't *totally* naive at this point, but is still
* probably pretty darn inefficient. Might be worth thinking about faster/
* more compact data structures at some point. But, in practice, it seems
* like the dominating perf concern is just the number of elements (rects)
* we're drawing, not our js objects/methods. */
constructor(words) {
this.length = words.length;
this.adjacency_list = [];
this.adjacency_map = new Array(this.length);
for (var x=0; x < words.length; x++) {
for (var y=0; y < words.length; y++) {
// TODO: wasteful to include diagonal and both reflections off diag
if (words[x] === words[y]) {
this.set_pair(x, y);
}
}
}
}
set_pair(x, y) {
this.adjacency_list.push({x, y});
if (this.adjacency_map[x] === undefined) {
this.adjacency_map[x] = new Set();
}
this.adjacency_map[x].add(y);
}
/** Return whether the words at the given indices match. */
at(x, y) {
if (x < 0 || this.length <= x || y < 0 || this.length <= y) {
return false;
}
return this.adjacency_map[x].has(y);
}
/** Precondition: (x, y) is in the adjacency list **/
is_singleton(x, y) {
return (x !== y && !this.at(x-1, y-1) && !this.at(x+1,y+1));
}
/** Return the diagonal that contains the given point. Behaviour undefined if
* there's no match at (x, y), or if x == y. */
local_diagonal(x, y) {
var x0 = x, x1 = x;
var y0 = y, y1 = y;
// Don't do anything further if this point is on the main diagonal.
if (x !== y) {
// Probe up and left
for (let x_ = x-1, y_ = y-1; x_ >= 0 && y_ >= 0 && this.at(x_, y_); x_--, y_--) {
x0 = x_;
y0 = y_;
}
// Probe down and to the right
for (let x_ = x+1, y_ = y+1; x_ < this.length && y_ < this.length
&& this.at(x_, y_); x_++, y_++) {
x1 = x_;
y1 = y_;
}
}
return new Diagonal(x0, y0, x1, y1);
}
* matches_for_index(i) {
for (let y = 0; y < this.length; y++) {
if (y === i) continue;
if (this.at(i, y)) {
yield y;
}
}
}
/** Does the given diagonal exist in this matrix? */
containsDiagonal(diag) {
for (let [x, y] of diag.points()) {
if (!this.at(x, y)) return false;
}
return true;
}
/** Extant diagonals in this matrix which are translations of the given
* diagonal in the x or y direction and not on the main diagonal. Plus
* the main diagonal correlates of *those* diagonals.
*/
* incidental_correlates(diag) {
var cor;
for (let x of this.matches_for_index(diag.y0)) {
cor = Diagonal.fromPointAndLength(x, diag.y0, diag.length);
if (this.containsDiagonal(cor)) {
yield cor;
yield cor.down_main();
}
}
for (let y of this.matches_for_index(diag.x0)) {
cor = Diagonal.fromPointAndLength(diag.x0, y, diag.length);
if (this.containsDiagonal(cor)) {
yield cor;
yield cor.side_main();
}
}
}
}
/**
* A chunk of text, presumably some kind of poem/song.
*/
class Verse {
constructor(text, id) {
this.raw = text;
this.id = id;
this.parse(text);
}
get length() {
return this.clean_words.length;
}
static cleanWord(word) {
var punctRE = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]/g;
word = word.toLowerCase();
var depuncd = word.replace(punctRE, '');
// If this word is *all* punctuation, then leave it alone.
return depuncd || word;
}
parse(text) {
var raw_words = [];
var clean_words = [];
var newlines = [];
var word_ids = new Map();
var lalas = [];
var lala_start;
var last_word;
var i = 0;
var lala;
for (const line of text.split(/\n\r|\r\n|\n|\r/)) {
for (const word of line.trim().split(/\s+/)) {
if (!word) {
continue;
}
raw_words.push(word);
var cleaned = Verse.cleanWord(word);
clean_words.push(cleaned);
var count = word_ids.has(cleaned) ? word_ids.get(cleaned)+1 : 1;
word_ids.set(cleaned, count);
if (cleaned === last_word) {
// Should we start a new lala?
if (lala_start === undefined) {
lala_start = i - 1;
}
} else if (lala_start !== undefined) {
// The end of a lala
lala = new Lala(lala_start, i-1, last_word);
lalas.push(lala);
lala_start = undefined;
}
last_word = cleaned;
i++;
}
newlines.push(clean_words.length - 1);
}
// Clean up unresolved lala, if there is one
if (lala_start) {
lala = new Lala(lala_start, i-1, last_word);
lalas.push(lala);
}
this.raw_words = raw_words;
this.clean_words = clean_words;
this.newline_indices = newlines;
this.lalas = lalas;
let RANDOMIZE = false;
let IGNORE_HAPAX = true;
var next_idx = 0;
var nWords = 0;
for (let [word, count] of word_ids) {
if (IGNORE_HAPAX && count === 1) {
word_ids.set(word, -1);
} else {
word_ids.set(word, next_idx++);
nWords++;
}
}
if (RANDOMIZE) {
console.warn("this is broken now");
this.word_ids = this.scramble(word_ids);
} else {
this.word_ids = word_ids;
}
this.nWords = nWords;
this.matrix = new VerseMatrix(this.clean_words);
}
scramble(wids) {
var words = Array.from(wids.keys());
for (let i=words.length; i>0; i--) {
let j = Math.floor(Math.random() * i);
[words[i-1], words[j]] = [words[j], words[i-1]];
}
let res = new Map();
for (let i=0; i < words.length; i++) {
res.set(words[i], i);
}
return res;
}
* rects() {
for (let i=0; i < this.lalas.length; i++) {
let lala = this.lalas[i];
for (let j=i; j < this.lalas.length; j++) {
let lala2 = this.lalas[j];
if (lala.word !== lala2.word) {
continue;
}
yield {x: lala.start, y: lala2.start, width: lala.length, height: lala2.length};
if (i !== j) {
yield {y: lala.start, x: lala2.start, height: lala.length, width: lala2.length};
}
}
}
for (let pt of this.matrix.adjacency_list) {
// this feels inefficient. I guess we're trading time for memory.
var foundx = false, foundy = false;
for (let lalax of this.lalas) {
foundx = foundx || lalax.includes(pt.x);
foundy = foundy || lalax.includes(pt.y);
}
if (!foundx || !foundy) {
yield {x: pt.x, y: pt.y, width: 1, height: 1};
}
}
}
uniqueWordId(idx) {
return this.word_ids.get(this.clean_words[idx]);
}
get lines() {
var lines = []
var i = 0;
for (const eol of this.newline_indices) {
var line = [];
// TODO: this is stupid, don't need a loop
for (; i <= eol; i++) {
line.push({i: i, raw: this.raw_words[i]});
}
lines.push(line);
}
return lines;
}
isCustom() {
console.error("iunno");
}
isBlank() {
return this.raw === '';
}
diagText(diag) {
return this.textInRange(diag.x0, diag.x1);
}
textInRange(a, b) {
console.assert(a <= b);
var res = [];
if (a > b) {
return res;
}
var acc = '';
if (!this.newline_indices.includes(a-1)) {
acc = '... ';
}
for (let x=a; 0 <= x && x <= b && x < this.length; x++) {
acc += this.raw_words[x];
if (this.newline_indices.includes(x)) {
res.push(acc);
acc = '';
} else {
acc += ' ';
}
}
if (acc) {
acc += '...';
res.push(acc);
}
return res;
}
}
class CannedVerse extends Verse {
constructor(text, id, title, artist) {
super(text, id);
this.title = title;
this.artist = artist;
this.clean_title = title ? Verse.cleanWord(title) : "";
this.title_tokens = title ? this.clean_title.split(" ") : [];
}
static fromCanned(c, text) {
return new CannedVerse(text, c.slug, c.title, c.artist);
}
isCustom() {
return false;
}
get slug() {
// TODO: just rename id to slug
return this.id;
}
}
class CustomVerse extends Verse {
/* Optional firebase key. */
constructor(text, key) {
super(text, CUSTOM_SLUG);
this.key = key;
}
isCustom() {
return true;
}
get_permalink(router) {
if (this.key) {
// TODO: pretty darn hacky. Surely there's a better way?
return (window.location.origin
+ window.location.pathname
+ router.createHref('/custom/' + this.key)
);
}
}
// heh, I made a funny
static BlankVerse() {
return new CustomVerse('');
}
}
export {Verse, VerseMatrix, CannedVerse, CustomVerse};