-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_wordlist.py
More file actions
37 lines (26 loc) · 1.08 KB
/
Copy pathbuild_wordlist.py
File metadata and controls
37 lines (26 loc) · 1.08 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
"""Build filtered word list from a language corpus."""
import importlib
import json
import lang as _lang
def build_wordlist(language: str) -> list[tuple[str, int]]:
"""Build word list for the given language. Requires set_lang() called first."""
mod = importlib.import_module(f"languages.{language}.parse_corpus")
words = mod.parse_corpus(_lang.corpus_dir())
out_path = _lang.wordlist_path()
out_path.parent.mkdir(parents=True, exist_ok=True)
with open(out_path, "w", encoding="utf-8") as f:
json.dump(words, f, ensure_ascii=False, indent=2)
return words
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Build word list from corpus")
parser.add_argument(
"--lang", required=True, choices=_lang.available_languages(), help="language code"
)
args = parser.parse_args()
_lang.set_lang(args.lang)
words = build_wordlist(args.lang)
print(f"Words: {len(words)}")
print(f"Top 10: {words[:10]}")
print(f"Bottom 10: {words[-10:]}")
print(f"Written to {_lang.wordlist_path()}")