-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.js
More file actions
96 lines (74 loc) · 2.27 KB
/
util.js
File metadata and controls
96 lines (74 loc) · 2.27 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
import * as fs from "fs";
import { createInterface } from "node:readline";
const computeHistogram = false;
const ignoreSameLetters = false;
const stripDiacritics = false;
const saveOnlyWords = true;
const file = createInterface({
input: fs.createReadStream("./src/assets/czech.txt"),
crlfDelay: Infinity,
});
const parsed = [];
const existing = new Map();
const histogram = {};
for await (const line of file) {
const word = stripDiacritics
? line
.split("/")[0]
.toLowerCase()
.normalize("NFD")
.replace(/\p{Diacritic}/gu, "")
: line.split("/")[0].toLowerCase();
const letters = [];
for (const letter of word) if (!letters.includes(letter)) letters.push(letter);
const lettersSorted = letters.sort();
const lettersHash = lettersSorted.join();
if (ignoreSameLetters && existing.has(lettersHash)) continue;
if (!ignoreSameLetters && existing.has(word)) continue;
if (saveOnlyWords) parsed.push(word);
else
parsed.push({
w: word,
l: lettersSorted,
});
if (ignoreSameLetters) existing.set(lettersHash, true);
else existing.set(word, true);
if (computeHistogram) letters.forEach((l) => (histogram[l] = (histogram[l] ?? 0) + 1));
}
fs.writeFileSync("./public/algorithms/dictionary/dictionary.json", JSON.stringify(parsed));
fs.writeFileSync(
"./public/stats.json",
JSON.stringify(
{
numberOfWords: parsed.length,
lettersFrequency: computeHistogram ? histogram : false,
},
null,
2
)
);
/**
* Generate all binary numbers of length `length` with exactly `numberOfOnes` bits set to 1
*/
export const generateNumbers = (numberOfOnes, length) => {
const numbers = [];
let max = nCk(length, numberOfOnes);
for (let i = 0b0; i < max; i++)
if (
i
.toString(2)
.split("")
.reduce((acc, v) => acc + (v === "1" ? 1 : 0), 0) === numberOfOnes
)
numbers.push(i.toString(2).padStart(length, "0"));
else max = max + 1;
return numbers;
};
export const nCk = (n, k) => {
let coefficient = 1;
for (let x = n - k + 1; x <= n; x++) coefficient *= x;
for (let x = 1; x <= k; x++) coefficient /= x;
return coefficient;
};
fs.writeFileSync("./public/algorithms/dictionary/lookup-8.json", JSON.stringify(generateNumbers(8, 25)));
fs.writeFileSync("./public/algorithms/dictionary/lookup-9.json", JSON.stringify(generateNumbers(9, 25)));