-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJustifyText.js
More file actions
355 lines (259 loc) · 5.69 KB
/
Copy pathJustifyText.js
File metadata and controls
355 lines (259 loc) · 5.69 KB
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
// Split Line
function splitLine(line, width)
{
var arr = new Array();
var ind = 0;
arr.push([]);
line.forEach(val =>
{
if (val.length <= width)
{
arr[0].push(val);
ind++;
}
width -= (val.length + 1);
})
arr.push(line.slice(ind, line.length));
return arr;
}
// Iterative Solution:
// var arr = new Array();
// var str = line.join(' ');
// var j = 0;
// arr.push([]);
// for (var i = 0; i < width && i < str.length; i++)
// {
// if (str[i] === line[j][line[j].length-1])
// {
// arr[0].push(line[j++]);
// }
// }
// if (j === line.length)
// arr.push([]);
// else
// arr.push(line.slice(j, line.length));
// return arr;
// Line Breaks
function getPermutations(arr)
{
var perms = new Array();
var str = "";
arr.forEach(val =>
{
if (str.length > 1)
str = str.slice(0, -1);
str += (val + '-');
perms.push(str)
})
return perms;
}
function lineBreaks(dict, width, line)
{
var ret = new Array();
var orig = splitLine(line, width);
ret.push(orig);
if (orig[1].length > 0)
{
var key = orig[1][0].replace(/[^A-Za-z]/g, "");
if (key in dict)
{
var perms = getPermutations(dict[key]);
var str = ret[0][0].join(' ');
var temp = width - str.length - 1;
var copies = new Array();
perms.forEach((val, ind) =>
{
copies.push([[...orig[0]], [...orig[1]]]);
if (val.length <= temp)
{
copies[ind][0].push(val);
copies[ind][1][0] = copies[ind][1][0].slice(val.length - 1);
ret.push(copies[ind]);
}
})
}
}
return ret;
}
// Blank Insertions
function singleBlankInsert(line)
{
line.forEach((_, ind) =>
{
if (ind != 0 && ind != line.length - 1)
line.splice(ind, 0, ' ');
})
return line;
}
function blankInsertions(num, line)
{
if (num === 0)
return line;
if (line.length <= 1)
return null;
}
function singleBlankInsert(line)
{
var arr = new Array();
line.forEach((_, ind) =>
{
if (ind != 0)
{
var copy = [...line];
copy.splice(ind, 0, ' ');
arr.push(copy);
if (ind === line.length - 1)
arr.push(copy);
}
})
return arr;
}
function blankInsertions(num, line)
{
// Base Cases
if (num === 0)
return [line];
else if (line.length <= 1)
return [line];
// Recursive Case
else
{
var arr = new Array();
arr = singleBlankInsert(line);
arr.forEach(val =>
{
var temp = blankInsertions(num - 1, val);
temp.forEach(val => arr.push(val))
});
var temp = Array.from(new Set(arr.map(JSON.stringify)), JSON.parse);
var ans = new Array();
temp.forEach((val, ind) =>
{
if (val.length >= line.length + num)
ans.push(val);
});
return ans;
}
}
// Line Cost
const blankCost = 1.0
const blankProxCost = 1.0
const blankUnevenCost = 1.0
const hypCost = 1.0
function blankPlacements(line, position)
{
var arr = new Array();
// I want to recurse until I get to the start of the line
if (position === 0)
{
return [];
}
else
{
arr = blankPlacements(line, position - 1);
if (line[position] === " ")
{
if (arr.length > 0)
arr.push([position, position - arr[arr.length-1][0] - 1]);
else
arr.push([position, position]);
}
if (position != line.length - 1)
return arr;
}
var spacePos = new Array();
arr.forEach(val => spacePos.push(val[1]));
spacePos.push(position - arr[arr.length-1][0]);
return spacePos;
}
function lineCost(line)
{
var totalHyphens = 0;
var regex = /[-]/g;
line.forEach(val =>
{
if (regex.test(val))
totalHyphens++;
});
var totalBlanks;
var avgDist;
var varDist;
var totalCost;
var str = line.join("");
var regex = /[ ]/g;
if (regex.test(str))
{
var spacePos = blankPlacements(line, line.length - 1);
totalBlanks = spacePos.length - 1;
var sum = spacePos.reduce((accum, curr) =>
{
return accum + curr;
}, 0);
avgDist = sum / spacePos.length;
sum = 0;
spacePos.forEach(val =>
{
var diff = val - avgDist;
sum += (diff * diff);
});
varDist = sum / spacePos.length;
totalCost = (blankCost * totalBlanks)
+ (blankProxCost * (line.length - avgDist))
+ (blankUnevenCost * varDist)
+ (hypCost * totalHyphens);
}
else
totalCost = hypCost * totalHyphens;
return totalCost;
}
// Best Line Break
function lineLength(line)
{
var length = 0;
line.forEach((val, ind) =>
{
length += val.length;
if (ind != 0 && val != " ")
length++;
})
return length;
}
function bestLineBreak(lineCostFunc, dict, width, line)
{
if (line.length === 0)
return [[], []];
var hyphComb = lineBreaks(dict, width, line);
var bestLine = hyphComb[0];
hyphComb.forEach(val =>
{
if (lineLength(val[0]) > lineLength(bestLine[0]))
bestLine = val;
});
var blankComb = blankInsertions(width - lineLength(bestLine[0]), bestLine[0]);
bestLine[0] = blankComb[0];
blankComb.forEach(val =>
{
if (lineCostFunc(val) < lineCostFunc(bestLine[0]))
bestLine[0] = val;
});
return bestLine;
}
// Justify Text
function justifyTextRecur(lineCostFunc, dict, width, text)
{
var arr = new Array();
if (text.length <= width)
arr.push(text.split(' '));
else
{
var lineArr = bestLineBreak(lineCostFunc, dict, width, text.split(' '));
arr.push(lineArr[0]);
var temp = justifyTextRecur(lineCostFunc, dict, width, lineArr[1].join(' '));
temp.forEach(val => arr.push(val));
}
return arr;
}
function justifyText(lineCostFunc, dict, width, text)
{
return justifyTextRecur(lineCostFunc, dict, width, text);
}