-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_dict_pythainlp.py
More file actions
53 lines (43 loc) · 1.54 KB
/
Copy pathprocess_dict_pythainlp.py
File metadata and controls
53 lines (43 loc) · 1.54 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
import re
import json
def load_dict_ts(filepath):
words = []
with open(filepath, "r", encoding="utf-8") as f:
for line in f:
match = re.search(r'"([^"]+)"', line)
if match:
words.append(match.group(1))
return words
def main():
dict_path = "src/lib/utils/dict.ts"
words = load_dict_ts(dict_path)
print(f"Loaded {len(words)} words from {dict_path}")
try:
from pythainlp.corpus import thai_word_freq
freq_data = dict(thai_word_freq())
print(f"Loaded PyThaiNLP word frequency corpus ({len(freq_data)} entries)")
except Exception as e:
print(f"PyThaiNLP error: {e}")
freq_data = {}
# Rank words by frequency
ranked_words = []
for word in words:
freq = freq_data.get(word, 0)
ranked_words.append((word, freq))
# Sort descending by frequency
ranked_words.sort(key=lambda x: x[1], reverse=True)
matched = sum(1 for _, f in ranked_words if f > 0)
print(f"Words matched with frequency data: {matched} / {len(words)}")
print("\nTop 20 Most Frequent Words:")
for w, f in ranked_words[:20]:
print(f" {w}: {f}")
# Export to dict_sorted.ts
output_path = "src/lib/utils/dict_sorted.ts"
with open(output_path, "w", encoding="utf-8") as f:
f.write("export const dict = [\n")
lines = [f' "{w}"' for w, _ in ranked_words]
f.write(",\n".join(lines))
f.write("\n];\n")
print(f"\nSaved sorted dictionary to {output_path}")
if __name__ == "__main__":
main()