-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzzies.html
More file actions
427 lines (390 loc) · 15.2 KB
/
Copy pathfuzzies.html
File metadata and controls
427 lines (390 loc) · 15.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Fuzzy Line Tool</title>
<style>
body {
margin: 0;
font-family: 'Segoe UI', sans-serif;
display: flex;
height: 100vh;
background-color: #fdfdfd;
}
#left-panel {
width: 220px;
display: flex;
flex-direction: column;
background-color: #f1f1f1;
padding: 10px;
box-sizing: border-box;
border-right: 1px solid #ccc;
}
.tool-btn {
background-color: #e3eaf3;
border: none;
padding: 10px;
margin-bottom: 8px;
border-radius: 8px;
font-weight: 600;
cursor: pointer;
transition: all 0.2s ease;
}
.tool-btn:hover,
.tool-btn.selected {
background-color: #007acc;
color: white;
}
#main {
display: flex;
flex: 1;
}
.text-area {
width: 50%;
padding: 20px;
box-sizing: border-box;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
textarea {
width: 100%;
height: 100%;
resize: none;
padding: 10px;
border: 1px solid #ccc;
border-radius: 6px;
font-size: 14px;
line-height: 1.4;
box-shadow: inset 0 1px 3px rgba(0,0,0,0.1);
}
#parameters {
padding: 10px 0;
}
#parameters label {
display: block;
margin-bottom: 6px;
}
#parameters input {
width: 100%;
padding: 5px;
margin-top: 2px;
box-sizing: border-box;
}
#run-button {
margin-top: 10px;
background-color: #007acc;
color: white;
border: none;
padding: 10px;
border-radius: 6px;
cursor: pointer;
}
#run-button:hover {
background-color: #005f99;
}
</style>
</head>
<body>
<div id="left-panel">
<h2>Tools</h2>
<div id="tool-buttons"></div>
<div id="parameters"></div>
<button id="run-button">Run</button>
<button id="clear-button" style="margin-top: 10px; background-color: #999; color: white; border: none; padding: 10px; border-radius: 6px; cursor: pointer;">Clear</button>
<button id="load-sample-button" style="margin-top: 10px; background-color: #999; color: white; border: none; padding: 10px; border-radius: 6px; cursor: pointer;">Load Sample</button>
</div>
<div id="main">
<div class="text-area">
<h2>Input Text</h2>
<textarea id="input-text" placeholder="Paste or write lines of text here..."></textarea>
</div>
<div class="text-area">
<h2>Output Text</h2>
<textarea id="output-text" readonly placeholder="Results will appear here..."></textarea>
</div>
</div>
<script type="module">
import { fuzzy } from 'https://esm.sh/fast-fuzzy@1.12.0';
const tools = {
analyzeText: { name: "Similarity Score 1" },
analyzeText2: { name: "Similarity Score 2" },
findDuplicates: { name: "Find Duplicates" },
findCommonWords: { name: "Find Common Words", params: [{ label: "Min Length", name: "minLength", type: "number", value: 3, min: 1 }] },
normalizeLines: { name: "Normalize Lines" },
clusterLines: {
name: "Cluster Lines",
params: [{ label: "Threshold", name: "threshold", type: "number", value: 0.6, min: 0, max: 1, step: 0.05 }]
},
exploreNGrams: {
name: "Explore N-Grams",
params: [
{ label: "N", name: "n", type: "number", value: 2, min: 1 },
{ label: "Min Count", name: "minCount", type: "number", value: 2, min: 1 }
]
}
};
let selectedTool = null;
const toolButtonsContainer = document.getElementById("tool-buttons");
const parametersContainer = document.getElementById("parameters");
Object.entries(tools).forEach(([key, tool]) => {
const btn = document.createElement("button");
btn.className = "tool-btn";
btn.textContent = tool.name;
btn.dataset.tool = key;
btn.onclick = () => selectTool(key, btn);
toolButtonsContainer.appendChild(btn);
});
function selectTool(key, btn) {
selectedTool = key;
document.querySelectorAll(".tool-btn").forEach(b => b.classList.remove("selected"));
btn.classList.add("selected");
showParameters(key);
// 🔁 Auto-run tool after switching
const input = document.getElementById("input-text").value;
const params = {};
parametersContainer.querySelectorAll("input").forEach(el => {
const val = el.type === 'number' ? parseFloat(el.value) : el.value;
params[el.name] = val;
});
document.getElementById("output-text").value = runTool(selectedTool, input, params);
}
function showParameters(toolKey) {
const tool = tools[toolKey];
parametersContainer.innerHTML = "";
if (!tool.params) return;
tool.params.forEach(param => {
const label = document.createElement("label");
const input = document.createElement("input");
input.type = param.type;
input.name = param.name;
input.value = param.value;
if (param.min !== undefined) input.min = param.min;
if (param.max !== undefined) input.max = param.max;
if (param.step !== undefined) input.step = param.step;
// Auto-run when parameter changes
input.addEventListener("input", () => {
const inputText = document.getElementById("input-text").value;
if (!inputText.trim()) return;
const params = {};
parametersContainer.querySelectorAll("input").forEach(el => {
const val = el.type === 'number' ? parseFloat(el.value) : el.value;
params[el.name] = val;
});
document.getElementById("output-text").value = runTool(selectedTool, inputText, params);
});
label.textContent = param.label + ":";
label.appendChild(input);
parametersContainer.appendChild(label);
});
}
document.getElementById("run-button").onclick = () => {
const input = document.getElementById("input-text").value;
const params = {};
parametersContainer.querySelectorAll("input").forEach(el => {
const val = el.type === 'number' ? parseFloat(el.value) : el.value;
params[el.name] = val;
});
document.getElementById("output-text").value = runTool(selectedTool, input, params);
};
function runTool(tool, input, params) {
const lines = input.trim().split("\n").map(line => line.trim()).filter(Boolean);
switch (tool) {
case 'analyzeText': return analyzeText(lines);
case 'analyzeText2': return analyzeText2(lines);
case 'findDuplicates': return findDuplicates(lines);
case 'findCommonWords': return findCommonWords(input, params.minLength);
case 'normalizeLines': return normalizeLines(lines);
case 'clusterLines': return clusterLines(lines, params.threshold);
case 'exploreNGrams': return exploreNGrams(lines, params.n, params.minCount);
default: return 'Tool not implemented.';
}
}
function analyzeText(lines) {
const scores = lines.map((lineA, i) => {
let total = 0;
let count = 0;
for (let j = 0; j < lines.length; j++) {
if (i !== j) {
total += fuzzy(lineA, lines[j]);
count++;
}
}
return { line: lineA, avgSimilarity: count ? total / count : 0 };
});
scores.sort((a, b) => b.avgSimilarity - a.avgSimilarity);
return scores.map((s, i) => `${i + 1}. [avg similarity: ${s.avgSimilarity.toFixed(3)}] - ${s.line}`).join("\n") || "No lines to compare.";
}
function analyzeText2(lines) {
const scores = lines.map((lineA, i) => {
let total = 0;
let count = 0;
for (let j = 0; j < lines.length; j++) {
if (i !== j) {
total += similarityScore2(lineA, lines[j]);
count++;
}
}
return { line: lineA, avgSimilarity: count ? total / count : 0 };
});
scores.sort((a, b) => b.avgSimilarity - a.avgSimilarity);
return scores.map((s, i) => `${i + 1}. [avg similarity: ${s.avgSimilarity.toFixed(3)}] - ${s.line}`).join("\n") || "No lines to compare.";
}
function similarityScore2(a, b) {
const maxLength = Math.max(a.length, b.length);
if (maxLength === 0) return 1;
return 1 - levenshtein(a.toLowerCase(), b.toLowerCase()) / maxLength;
}
function levenshtein(a, b) {
const matrix = Array.from({ length: a.length + 1 }, (_, i) => Array(b.length + 1).fill(0));
for (let i = 0; i <= a.length; i++) matrix[i][0] = i;
for (let j = 0; j <= b.length; j++) matrix[0][j] = j;
for (let i = 1; i <= a.length; i++) {
for (let j = 1; j <= b.length; j++) {
matrix[i][j] = a[i - 1] === b[j - 1]
? matrix[i - 1][j - 1]
: 1 + Math.min(matrix[i - 1][j], matrix[i][j - 1], matrix[i - 1][j - 1]);
}
}
return matrix[a.length][b.length];
}
function findDuplicates(lines) {
const seen = new Map();
const duplicates = [];
lines.forEach((line, i) => {
const lower = line.toLowerCase();
if (seen.has(lower)) {
duplicates.push(`Duplicate: "${line}" (Line ${i + 1}, original on Line ${seen.get(lower) + 1})`);
} else {
seen.set(lower, i);
}
});
return duplicates.length ? duplicates.join("\n") : "No duplicates found.";
}
function findCommonWords(input, minLength = 3) {
const words = input.toLowerCase().match(/\b\w+\b/g);
const counts = {};
if (!words) return "No words found.";
words.forEach(word => {
if (word.length >= minLength) {
counts[word] = (counts[word] || 0) + 1;
}
});
return Object.entries(counts)
.sort((a, b) => b[1] - a[1])
.map(([word, count]) => `${word}: ${count}`)
.slice(0, 30).join("\n") || "No significant words found.";
}
function normalizeLines(lines) {
return lines.map(line => line.toLowerCase().replace(/[^\w\s]/g, '').trim()).join("\n");
}
function clusterLines(lines, threshold = 0.6) {
const tokenSets = lines.map(line => new Set(line.toLowerCase().split(/\s+/)));
const clusters = [], assigned = Array(lines.length).fill(false);
const jaccard = (a, b) => {
const intersection = new Set([...a].filter(x => b.has(x)));
const union = new Set([...a, ...b]);
return intersection.size / union.size;
};
for (let i = 0; i < lines.length; i++) {
if (assigned[i]) continue;
const cluster = [lines[i]];
assigned[i] = true;
for (let j = i + 1; j < lines.length; j++) {
if (!assigned[j] && jaccard(tokenSets[i], tokenSets[j]) >= threshold) {
cluster.push(lines[j]);
assigned[j] = true;
}
}
clusters.push(cluster);
}
return clusters.map((group, i) => `Cluster ${i + 1} (${group.length} lines):\n` + group.map(line => ` - ${line}`).join("\n")).join("\n\n") || "No clusters found.";
}
function exploreNGrams(lines, n = 2, minCount = 2) {
const counts = {};
lines.forEach(line => {
const words = line.toLowerCase().split(/\s+/).filter(Boolean);
for (let i = 0; i <= words.length - n; i++) {
const ngram = words.slice(i, i + n).join(" ");
counts[ngram] = (counts[ngram] || 0) + 1;
}
});
return Object.entries(counts)
.filter(([_, count]) => count >= minCount)
.sort((a, b) => b[1] - a[1])
.map(([phrase, count]) => `${phrase}: ${count}`)
.join("\n") || `No ${n}-grams found that occur at least ${minCount} times.`;
}
document.getElementById("clear-button").onclick = () => {
document.getElementById("input-text").value = "";
document.getElementById("output-text").value = "";
selectedTool = null;
parametersContainer.innerHTML = "";
document.querySelectorAll(".tool-btn").forEach(b => b.classList.remove("selected"));
};
document.getElementById("load-sample-button").onclick = () => {
document.getElementById("input-text").value = `The quick brown fox jumps over the lazy dog.
I love eating pizza on rainy days.
Pizza is my favorite food to eat.
The quick brown fox leaps over the lazy dog.
The quick brown fox jumps over a lazy dog.
The quick brown fox jumps over the lazy cat.
The quick brown dog jumps over the lazy fox.
Quick brown fox jumps over lazy dog.
the quick brown fox jumps over the lazy dog.
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog..
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick browwn fox jumps over the lazy dog.
The quik brown fox jumps over the lazy dog.
Thequick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy frog.
A quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dogs.
The quick brown fox jumps over lazy dogs.
The quick brown fox was jumping over the lazy dog.
A swift brown fox hops over the idle hound.
A swift brown fox hops o’er th’ idle hound.
Swift brown fox hops over idle hound.
The brown quick fox jumps over the lazy dog.
The quick brown fox, jumps over the lazy dog.
The quick brown fox jumps, over the lazy dog.
The quick brown fox jumps over the lazy dog!
The quick brown fox jumps over the lazy dog?.
The quick brown fox jumps over the energetic dog.
The quick brown cat jumps over the lazy dog.
The quick brown foxes jump over the lazy dog.
The quick brown fox jumps high over the lazy dog.
High the quick brown fox jumps over the lazy dog.
Over the lazy dog, the quick brown fox jumps.
the quick brown fox jumps over the drzay dog.
Th3 quick brown fox jumps ov3r th3 lazy dog.
The quick Brown fox jumps over the lazy Dog.
THe QuIck BrOwn FoX jUmPs OvEr ThE lAzY dOg.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog.
The quick brown fox—jumps over the lazy dog.
The quick brown fox–jumps over the lazy dog.
ↈhe quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.`;
// Auto-run current tool if one is selected
if (selectedTool) {
const params = {};
parametersContainer.querySelectorAll("input").forEach(el => {
const val = el.type === 'number' ? parseFloat(el.value) : el.value;
params[el.name] = val;
});
document.getElementById("output-text").value = runTool(selectedTool, document.getElementById("input-text").value, params);
}
};
</script>
</body>
</html>