-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_frequency.js
More file actions
29 lines (24 loc) · 852 Bytes
/
Copy pathcount_frequency.js
File metadata and controls
29 lines (24 loc) · 852 Bytes
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
import fs from 'fs';
import path from 'path';
const filePath = path.resolve('src/lib/dict.json');
const content = fs.readFileSync(filePath, 'utf-8');
// Simple regex parser to extract words
const wordRegex = /"([^"]+)"/g;
let match;
const frequencies = {};
let totalChars = 0;
while ((match = wordRegex.exec(content)) !== null) {
const word = match[1];
// Count each character (consonant, vowel, tone mark)
for (const char of word) {
frequencies[char] = (frequencies[char] || 0) + 1;
totalChars++;
}
}
const sorted = Object.entries(frequencies)
.sort((a, b) => b[1] - a[1]);
console.log(`=== Thai Letter Frequencies (Total Characters: ${totalChars}) ===`);
sorted.forEach(([char, count]) => {
const percentage = ((count / totalChars) * 100).toFixed(2);
console.log(`Letter: ${char} | Count: ${count} | Percentage: ${percentage}%`);
});