-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_dict.js
More file actions
74 lines (60 loc) · 2.28 KB
/
Copy pathprocess_dict.js
File metadata and controls
74 lines (60 loc) · 2.28 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
import fs from 'fs';
import path from 'path';
// 1. Read PyThaiNLP TNC Frequency data
const tncPath = process.env.TNC_PATH || '';
const tncContent = fs.readFileSync(tncPath, 'utf-8');
const freqMap = new Map();
const lines = tncContent.split('\n');
for (const line of lines) {
const parts = line.trim().split('\t');
if (parts.length === 2) {
const word = parts[0];
const freq = parseInt(parts[1], 10);
if (word && !isNaN(freq)) {
freqMap.set(word, freq);
}
}
}
console.log(`Loaded ${freqMap.size} word frequencies from PyThaiNLP TNC corpus`);
// 2. Read src/lib/utils/dict.ts
const dictPath = path.resolve('src/lib/utils/dict.ts');
const dictContent = fs.readFileSync(dictPath, 'utf-8');
const wordMatches = dictContent.match(/"([^"]+)"/g);
if (!wordMatches) {
console.error("Failed to parse dict.ts");
process.exit(1);
}
const words = wordMatches.map(w => w.replace(/"/g, ''));
console.log(`Loaded ${words.length} words from dict.ts`);
// 3. Rank words by PyThaiNLP frequency
const ranked = words.map(word => ({
word,
freq: freqMap.get(word) || 0
}));
// Sort descending by frequency (words with higher frequency first)
ranked.sort((a, b) => b.freq - a.freq);
const matchedCount = ranked.filter(item => item.freq > 0).length;
console.log(`Words with PyThaiNLP frequency match: ${matchedCount} / ${words.length}`);
// 4. Create copy as src/lib/utils/dict_freq.ts
const outputPath = path.resolve('src/lib/utils/dict_freq.ts');
const fileHeader = `// Thai dictionary sorted by PyThaiNLP (TNC corpus) word frequency
export interface DictFreqItem {
word: string;
freq: number;
}
export const dictFreq: DictFreqItem[] = [\n`;
const formattedLines = ranked.map(item => ` { word: "${item.word}", freq: ${item.freq} }`).join(',\n');
const fileFooter = `\n];
const freqLookupMap = new Map<string, number>();
export function getWordFrequency(word: string): number {
if (freqLookupMap.size === 0 && dictFreq.length > 0) {
for (let i = 0; i < dictFreq.length; i++) {
freqLookupMap.set(dictFreq[i].word, dictFreq[i].freq);
}
}
const clean = word.split(' ')[0];
return freqLookupMap.get(clean) ?? freqLookupMap.get(word) ?? 0;
}
`;
fs.writeFileSync(outputPath, fileHeader + formattedLines + fileFooter, 'utf-8');
console.log(`\nSuccessfully updated copy at: ${outputPath}`);