-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_ef_words.py
More file actions
42 lines (34 loc) · 1.24 KB
/
Copy pathsplit_ef_words.py
File metadata and controls
42 lines (34 loc) · 1.24 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
#!/usr/bin/env python3
"""Split ef-3k.txt into files by word length (UTF-8)."""
from pathlib import Path
ROOT = Path(__file__).resolve().parent
SRC = ROOT / "ef-3k.txt"
OUT = {
2: ROOT / "ef-2char.txt",
3: ROOT / "ef-3char.txt",
4: ROOT / "ef-4char.txt",
5: ROOT / "ef-5char.txt",
}
LONG = ROOT / "ef-long.txt"
def main() -> None:
buckets: dict[int | str, list[str]] = {2: [], 3: [], 4: [], 5: [], "long": []}
text = SRC.read_text(encoding="utf-8")
for raw in text.splitlines():
word = raw.strip()
if not word:
continue
n = len(word)
if n in OUT:
buckets[n].append(word)
else:
buckets["long"].append(word)
for length, path in OUT.items():
path.write_text("\n".join(buckets[length]) + ("\n" if buckets[length] else ""), encoding="utf-8")
LONG.write_text("\n".join(buckets["long"]) + ("\n" if buckets["long"] else ""), encoding="utf-8")
total = sum(len(buckets[k]) for k in (2, 3, 4, 5, "long"))
print(f"Wrote {total} non-empty lines from {SRC.name}")
for length in (2, 3, 4, 5):
print(f" {OUT[length].name}: {len(buckets[length])}")
print(f" {LONG.name}: {len(buckets['long'])}")
if __name__ == "__main__":
main()