-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathdictionary-sorter.mjs
More file actions
64 lines (51 loc) · 2.15 KB
/
dictionary-sorter.mjs
File metadata and controls
64 lines (51 loc) · 2.15 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
import fs from 'fs';
// Function to process the dictionary file
function processDictionary() {
const inputFile = 'dictionary-octopus.txt';
const outputFile = 'dictionary-octopus.txt';
try {
// Check if input file exists
if (!fs.existsSync(inputFile)) {
console.error(`Error: File '${inputFile}' not found.`);
return;
}
// Read the file
console.log(`Reading ${inputFile}...`);
const data = fs.readFileSync(inputFile, 'utf8');
// Split into lines, filter out empty lines, and trim whitespace
const lines = data
.split('\n')
.map(line => line.trim())
.filter(line => line.length > 0);
console.log(`Original entries: ${lines.length}`);
// Remove duplicates using Set (case-insensitive)
const uniqueLines = [...new Set(lines.map(line => line.toLowerCase()))]
.map(lowerLine => {
// Find the original case version of each unique line
return lines.find(originalLine =>
originalLine.toLowerCase() === lowerLine
);
});
console.log(`Unique entries: ${uniqueLines.length}`);
console.log(`Duplicates removed: ${lines.length - uniqueLines.length}`);
// Sort alphabetically (case-insensitive)
const sortedLines = uniqueLines.sort((a, b) =>
a.toLowerCase().localeCompare(b.toLowerCase())
);
// Write to output file
const output = sortedLines.join('\n') + '\n';
fs.writeFileSync(outputFile, output, 'utf8');
console.log(`✅ Processed dictionary saved to ${outputFile}`);
console.log(`First few entries:`);
sortedLines.slice(0, 5).forEach((line, i) => {
console.log(` ${i + 1}. ${line}`);
});
if (sortedLines.length > 5) {
console.log(` ... and ${sortedLines.length - 5} more`);
}
} catch (error) {
console.error('Error processing file:', error.message);
}
}
// Run the function
processDictionary();