-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalphabetizer-lab.html
More file actions
255 lines (224 loc) · 9.65 KB
/
Copy pathalphabetizer-lab.html
File metadata and controls
255 lines (224 loc) · 9.65 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Alphabetizer 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>
<!-- Tool content here -->
<div class="container">
<div class="header">
<h1>Alphabetizer Lab</h1>
<p>Analyze and rearrange words from your text alphabetically and numerically</p>
</div>
<p style="margin-bottom: 30px; line-height: 1.8; color: #4a4a4a;">
[Add your description here about how the Alphabetizer Lab reorganizes text and reveals word frequency patterns.]
</p>
<div class="upload-section">
<div class="upload-icon">📄</div>
<h2>Upload Text File</h2>
<p>Select a .txt file to analyze and rearrange its words</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>Display Arrangement:</label>
<div class="radio-group">
<label class="radio-option" for="horizontal">
<input type="radio" id="horizontal" name="arrangement" value="horizontal" checked />
<span>Horizontal (rows)</span>
</label>
<label class="radio-option" for="vertical">
<input type="radio" id="vertical" name="arrangement" value="vertical" />
<span>Vertical (columns)</span>
</label>
</div>
</div>
<div class="option-group">
<label>Display Type:</label>
<div class="radio-group">
<label class="radio-option" for="allWords">
<input type="radio" id="allWords" name="displayType" value="allWords" checked />
<span>All Words (full corpus)</span>
</label>
<label class="radio-option" for="withFrequency">
<input type="radio" id="withFrequency" name="displayType" value="withFrequency" />
<span>Words with Frequency Count</span>
</label>
</div>
</div>
<button class="rearrange-btn" id="rearrangeBtn">Rearrange Words</button>
</div>
<div class="results-section" id="resultsSection" style="display: none;">
<div class="output-header">
<div class="results-header" id="resultsHeader">Word Analysis Results</div>
<div class="font-size-control">
<label for="fontSizeSlider">Font Size:</label>
<input type="range" id="fontSizeSlider" class="slider" min="10" max="28" value="16" step="1">
</div>
</div>
<div class="word-display" id="wordDisplay"></div>
<div class="stats" id="statsDisplay"></div>
<button class="apply-btn" id="exportBtn" style="margin-top: 15px;">Export TXT</button>
</div>
</div>
<script>
// JavaScript for this specific tool
let fileContent = '';
let processedWords = [];
const fileInput = document.getElementById('textFile');
const optionsSection = document.getElementById('optionsSection');
const rearrangeBtn = document.getElementById('rearrangeBtn');
const resultsSection = document.getElementById('resultsSection');
const wordDisplay = document.getElementById('wordDisplay');
const statsDisplay = document.getElementById('statsDisplay');
const resultsHeader = document.getElementById('resultsHeader');
const fontSizeSlider = document.getElementById('fontSizeSlider');
fileInput.addEventListener('change', handleFileUpload);
rearrangeBtn.addEventListener('click', rearrangeWords);
// 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];
console.log('File selected:', file); // Debug log
if (file) {
if (file.type === 'text/plain' || file.name.endsWith('.txt')) {
const reader = new FileReader();
reader.onload = function(e) {
fileContent = e.target.result;
console.log('File content loaded, length:', fileContent.length); // Debug log
optionsSection.style.display = 'block';
resultsSection.style.display = 'none';
rearrangeBtn.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 processText() {
if (!fileContent) return;
// Clean and split text into words
const words = fileContent
.toLowerCase()
.replace(/[^\w\s]/g, ' ') // Replace punctuation with spaces
.split(/\s+/)
.filter(word => word.length > 0);
const displayType = document.querySelector('input[name="displayType"]:checked').value;
if (displayType === 'allWords') {
// Return all words alphabetized
processedWords = words.sort();
} else {
// Count frequencies and return unique words with counts
const wordFreq = {};
words.forEach(word => {
wordFreq[word] = (wordFreq[word] || 0) + 1;
});
processedWords = Object.entries(wordFreq)
.sort(([a], [b]) => a.localeCompare(b))
.map(([word, freq]) => ({ word, freq }));
}
}
function rearrangeWords() {
if (!fileContent) {
alert('Please upload a text file first');
return;
}
console.log('Processing text, content length:', fileContent.length); // Debug log
processText();
const arrangement = document.querySelector('input[name="arrangement"]:checked').value;
const displayType = document.querySelector('input[name="displayType"]:checked').value;
console.log('Selected options:', { arrangement, displayType }); // Debug log
// Clear previous results
wordDisplay.innerHTML = '';
wordDisplay.className = `word-display ${arrangement}`;
if (displayType === 'allWords') {
// Display all words
processedWords.forEach(word => {
const wordElement = document.createElement('span');
wordElement.className = 'word-item';
wordElement.textContent = word;
wordDisplay.appendChild(wordElement);
});
resultsHeader.textContent = `All Words (${processedWords.length} total)`;
statsDisplay.textContent = `Total words in corpus: ${processedWords.length}`;
} else {
// Display words with frequency
processedWords.forEach(({ word, freq }) => {
const wordElement = document.createElement('span');
wordElement.className = 'word-item';
wordElement.innerHTML = `${word}<span class="frequency">(${freq})</span>`;
wordDisplay.appendChild(wordElement);
});
resultsHeader.textContent = `Unique Words with Frequency (${processedWords.length} unique)`;
const totalWords = processedWords.reduce((sum, { freq }) => sum + freq, 0);
statsDisplay.textContent = `Unique words: ${processedWords.length} | Total words: ${totalWords}`;
}
resultsSection.style.display = 'block';
resultsSection.scrollIntoView({ behavior: 'smooth' });
}
// Font size slider
if (fontSizeSlider && wordDisplay) {
fontSizeSlider.addEventListener('input', (e) => {
wordDisplay.style.fontSize = e.target.value + 'px';
});
}
document.getElementById('exportBtn').addEventListener('click', function() {
const items = wordDisplay.querySelectorAll('.word-item');
const words = Array.from(items).map(function(item) {
const freqSpan = item.querySelector('.frequency');
if (freqSpan) {
return item.textContent.replace(freqSpan.textContent, '').trim();
}
return item.textContent.trim();
});
const text = words.join('\n');
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 = 'alphabetizer-lab-output.txt';
a.click();
URL.revokeObjectURL(url);
});
</script>
</body>
</html>