-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlong-tail-lab.html
More file actions
335 lines (293 loc) · 13.2 KB
/
Copy pathlong-tail-lab.html
File metadata and controls
335 lines (293 loc) · 13.2 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Long Tail Lab - Text-Kitchen</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<nav class="main-nav">
<a href="index.html">Home</a>
<a href="text-reverse-lab.html">Text Reversal</a>
<a href="line-break-lab.html">Line Break Lab</a>
<a href="eraser-lab.html">Eraser Lab</a>
<a href="alphabetizer-lab.html">Alphabetizer Lab</a>
<a href="markov-mashup-lab.html">Markov Mashup Lab</a>
<a href="lipogram-lab.html">Lipogram Lab</a>
<a href="long-tail-lab.html">Long Tail Lab</a>
<a href="grid-it-lab.html">Grid-It Lab</a>
<a href="letter-replacement-lab.html">Letter Swap</a>
<a href="chromacode-lab.html">Chromacode</a>
<a href="about.html">About</a>
</nav>
<div class="container">
<div class="header">
<h1>Long Tail Lab</h1>
<p>Reorganize text by the length of clauses, sentences, lines, or words</p>
</div>
<p style="margin-bottom: 30px; line-height: 1.8; color: #4a4a4a;">
[Add your description here about how the Long Tail Lab reorganizes text and how this can reveal patterns or create new reading experiences.]
</p>
<div class="upload-section">
<div class="upload-icon">📄</div>
<h2>Upload Text File</h2>
<p>Select a .txt file to reorganize by length</p>
<label class="file-input-wrapper">
Choose File
<input type="file" id="textFile" accept=".txt" />
</label>
</div>
<div class="options-section" id="optionsSection" style="display: none;">
<div class="option-group">
<label>Organize By:</label>
<div class="radio-group">
<label class="radio-option" for="byClause">
<input type="radio" id="byClause" name="organizeBy" value="clause" checked />
<span>Clause (. : ; — , --)</span>
</label>
<label class="radio-option" for="byNewline">
<input type="radio" id="byNewline" name="organizeBy" value="newline" />
<span>New-lines</span>
</label>
<label class="radio-option" for="bySentence">
<input type="radio" id="bySentence" name="organizeBy" value="sentence" />
<span>Sentences (. ! ?)</span>
</label>
<label class="radio-option" for="byWord">
<input type="radio" id="byWord" name="organizeBy" value="word" />
<span>Words</span>
</label>
</div>
</div>
<div class="option-group">
<label>Sort Order:</label>
<div class="radio-group">
<label class="radio-option" for="longestFirst">
<input type="radio" id="longestFirst" name="sortOrder" value="longest" checked />
<span>Longest First</span>
</label>
<label class="radio-option" for="shortestFirst">
<input type="radio" id="shortestFirst" name="sortOrder" value="shortest" />
<span>Shortest First</span>
</label>
</div>
</div>
<div class="option-group">
<label>Whitespace Handling:</label>
<div class="checkbox-container">
<input type="checkbox" id="ignoreLineBreaks" checked />
<label for="ignoreLineBreaks">Ignore line breaks when measuring length</label>
</div>
<div class="checkbox-container">
<input type="checkbox" id="ignoreWhitespace" checked />
<label for="ignoreWhitespace">Ignore extra whitespace when measuring length</label>
</div>
</div>
<button class="apply-btn" id="organizeBtn">Organize Text</button>
</div>
<div class="results-section" id="resultsSection" style="display: none;">
<div class="output-header">
<div class="output-title">Reorganized Text</div>
<div class="font-size-control">
<label for="fontSizeSlider">Font Size:</label>
<input type="range" id="fontSizeSlider" class="slider" min="12" max="32" value="16" step="1">
</div>
</div>
<div class="output-text" id="outputText"></div>
<div class="stats" id="statsDisplay"></div>
<button class="apply-btn" id="exportBtn" style="margin-top: 15px;">Export TXT</button>
</div>
</div>
<script>
let fileContent = '';
const fileInput = document.getElementById('textFile');
const optionsSection = document.getElementById('optionsSection');
const organizeBtn = document.getElementById('organizeBtn');
const resultsSection = document.getElementById('resultsSection');
const outputText = document.getElementById('outputText');
const statsDisplay = document.getElementById('statsDisplay');
fileInput.addEventListener('change', handleFileUpload);
organizeBtn.addEventListener('click', organizeText);
// Add event listeners for radio buttons
document.querySelectorAll('input[type="radio"]').forEach(radio => {
radio.addEventListener('change', updateRadioStyles);
});
// Initialize radio button styles
updateRadioStyles();
function updateRadioStyles() {
document.querySelectorAll('.radio-option').forEach(option => {
const radio = option.querySelector('input[type="radio"]');
if (radio.checked) {
option.classList.add('checked');
} else {
option.classList.remove('checked');
}
});
}
function handleFileUpload(event) {
const file = event.target.files[0];
if (file) {
if (file.type === 'text/plain' || file.name.endsWith('.txt')) {
const reader = new FileReader();
reader.onload = function(e) {
fileContent = e.target.result;
optionsSection.style.display = 'block';
resultsSection.style.display = 'none';
organizeBtn.disabled = false;
};
reader.onerror = function(e) {
console.error('Error reading file:', e);
alert('Error reading file. Please try again.');
};
reader.readAsText(file);
} else {
alert('Please select a valid .txt file');
}
}
}
function splitByClause(text) {
// Split by periods, colons, semicolons, em-dashes (— and --), and commas
// Preserve the delimiter with each segment
const regex = /([^.,;:—]+[.,;:—]|[^.,;:—]+--[^.,;:—]*|[^.,;:—]+)/g;
let segments = [];
let match;
// First normalize double hyphens to em-dashes
text = text.replace(/--/g, '—');
// Split by clause delimiters while preserving them
const parts = text.split(/([.,;:—])/);
let currentSegment = '';
for (let i = 0; i < parts.length; i++) {
if (parts[i].match(/[.,;:—]/)) {
currentSegment += parts[i];
if (currentSegment.trim()) {
segments.push(currentSegment);
}
currentSegment = '';
} else {
currentSegment += parts[i];
}
}
// Add any remaining segment
if (currentSegment.trim()) {
segments.push(currentSegment);
}
return segments.filter(s => s.trim().length > 0);
}
function splitByNewline(text) {
return text.split(/\n/).filter(line => line.trim().length > 0);
}
function splitBySentence(text) {
// Split by periods, exclamation marks, and question marks
// Preserve the delimiter with each segment
const parts = text.split(/([.!?])/);
let segments = [];
let currentSegment = '';
for (let i = 0; i < parts.length; i++) {
if (parts[i].match(/[.!?]/)) {
currentSegment += parts[i];
if (currentSegment.trim()) {
segments.push(currentSegment);
}
currentSegment = '';
} else {
currentSegment += parts[i];
}
}
// Add any remaining segment
if (currentSegment.trim()) {
segments.push(currentSegment);
}
return segments.filter(s => s.trim().length > 0);
}
function splitByWord(text) {
return text.split(/\s+/).filter(word => word.trim().length > 0);
}
function getMeasuredLength(text) {
let measured = text;
const ignoreLineBreaks = document.getElementById('ignoreLineBreaks').checked;
const ignoreWhitespace = document.getElementById('ignoreWhitespace').checked;
if (ignoreLineBreaks) {
measured = measured.replace(/\n/g, '');
}
if (ignoreWhitespace) {
measured = measured.replace(/\s+/g, ' ').trim();
}
return measured.length;
}
function organizeText() {
if (!fileContent) {
alert('Please upload a text file first');
return;
}
const organizeBy = document.querySelector('input[name="organizeBy"]:checked').value;
const sortOrder = document.querySelector('input[name="sortOrder"]:checked').value;
let segments = [];
let segmentType = '';
switch (organizeBy) {
case 'clause':
segments = splitByClause(fileContent);
segmentType = 'clauses';
break;
case 'newline':
segments = splitByNewline(fileContent);
segmentType = 'lines';
break;
case 'sentence':
segments = splitBySentence(fileContent);
segmentType = 'sentences';
break;
case 'word':
segments = splitByWord(fileContent);
segmentType = 'words';
break;
}
// Sort by measured length (considering whitespace options)
segments.sort((a, b) => {
const lengthDiff = getMeasuredLength(a) - getMeasuredLength(b);
return sortOrder === 'longest' ? -lengthDiff : lengthDiff;
});
// Calculate statistics using measured lengths
const totalSegments = segments.length;
const lengths = segments.map(s => getMeasuredLength(s));
const avgLength = Math.round(lengths.reduce((sum, l) => sum + l, 0) / totalSegments);
const minLength = Math.min(...lengths);
const maxLength = Math.max(...lengths);
// Display results
let displayText = '';
if (organizeBy === 'word') {
displayText = segments.join(' ');
} else {
displayText = segments.join('\n\n');
}
outputText.textContent = displayText;
statsDisplay.innerHTML = `
<strong>Total ${segmentType}:</strong> ${totalSegments}<br>
<strong>Average length:</strong> ${avgLength} characters<br>
<strong>Shortest:</strong> ${minLength} characters<br>
<strong>Longest:</strong> ${maxLength} characters<br>
<strong>Sort order:</strong> ${sortOrder === 'longest' ? 'Longest to Shortest' : 'Shortest to Longest'}
`;
resultsSection.style.display = 'block';
resultsSection.scrollIntoView({ behavior: 'smooth' });
}
document.getElementById('exportBtn').addEventListener('click', function() {
const text = outputText.textContent;
if (!text) return;
const blob = new Blob([text], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'long-tail-lab-output.txt';
a.click();
URL.revokeObjectURL(url);
});
// Font size slider
const fontSizeSlider = document.getElementById('fontSizeSlider');
if (fontSizeSlider) {
fontSizeSlider.addEventListener('input', (e) => {
outputText.style.fontSize = e.target.value + 'px';
});
}
</script>
</body>
</html>