helpish 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
helpish/__init__.py ADDED
@@ -0,0 +1,136 @@
1
+ """helpish: list English words by length, handy for writing Pilish."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections import defaultdict
6
+ from importlib.resources import files
7
+ from pathlib import Path
8
+
9
+ from textual import on
10
+ from textual.app import App, ComposeResult
11
+ from textual.containers import Horizontal, VerticalScroll
12
+ from textual.widgets import Footer, Header, Input, Label, Static
13
+ from wordfreq import zipf_frequency
14
+
15
+ WORDS_FILE = Path(str(files("helpish") / "words_alpha.txt"))
16
+
17
+
18
+ def load_words_by_length(path: Path) -> dict[int, list[str]]:
19
+ """Read the word list once and bucket every word by its length."""
20
+ buckets: dict[int, list[str]] = defaultdict(list)
21
+ with path.open(encoding="utf-8") as handle:
22
+ for line in handle:
23
+ word = line.strip()
24
+ if word:
25
+ buckets[len(word)].append(word)
26
+ return buckets
27
+
28
+
29
+ class WordLengthApp(App[None]):
30
+ """Type a number, see every English word of that length."""
31
+
32
+ CSS = """
33
+ #controls {
34
+ height: auto;
35
+ padding: 1 2;
36
+ }
37
+
38
+ #length-input {
39
+ width: 20;
40
+ }
41
+
42
+ #summary {
43
+ padding: 1 2;
44
+ color: $text-muted;
45
+ }
46
+
47
+ #results {
48
+ padding: 0 2;
49
+ }
50
+ """
51
+
52
+ TITLE = "English Words by Length"
53
+ BINDINGS = [("ctrl+c", "quit", "Quit")]
54
+
55
+ def __init__(self) -> None:
56
+ super().__init__()
57
+ self.words_by_length: dict[int, list[str]] = {}
58
+
59
+ def compose(self) -> ComposeResult:
60
+ yield Header()
61
+ with Horizontal(id="controls"):
62
+ yield Label("Word length: ")
63
+ yield Input(
64
+ placeholder="enter a number",
65
+ id="length-input",
66
+ type="integer",
67
+ restrict=r"\d*",
68
+ )
69
+ yield Label(" Contains: ")
70
+ yield Input(
71
+ placeholder="substring filter",
72
+ id="filter-input",
73
+ )
74
+ yield Static("", id="summary")
75
+ yield VerticalScroll(Static("", id="results"))
76
+ yield Footer()
77
+
78
+ def on_mount(self) -> None:
79
+ if not WORDS_FILE.exists():
80
+ self.query_one("#summary", Static).update(
81
+ f"[red]Word list not found:[/] {WORDS_FILE}"
82
+ )
83
+ return
84
+ self.words_by_length = load_words_by_length(WORDS_FILE)
85
+ total = sum(len(w) for w in self.words_by_length.values())
86
+ longest = max(self.words_by_length) if self.words_by_length else 0
87
+ self.query_one("#summary", Static).update(
88
+ f"Loaded {total:,} words (lengths 1–{longest}). "
89
+ "Enter a number above."
90
+ )
91
+ self.query_one("#length-input", Input).focus()
92
+
93
+ @on(Input.Changed, "#length-input")
94
+ @on(Input.Changed, "#filter-input")
95
+ def show_words(self) -> None:
96
+ summary = self.query_one("#summary", Static)
97
+ results = self.query_one("#results", Static)
98
+
99
+ value = self.query_one("#length-input", Input).value.strip()
100
+ needle = self.query_one("#filter-input", Input).value.strip().lower()
101
+ if not value:
102
+ summary.update("Enter a number above.")
103
+ results.update("")
104
+ return
105
+
106
+ length = int(value)
107
+ words = self.words_by_length.get(length, [])
108
+ if needle:
109
+ words = [word for word in words if needle in word]
110
+ if not words:
111
+ if needle:
112
+ summary.update(
113
+ f"No words of length {length} containing '{needle}'."
114
+ )
115
+ else:
116
+ summary.update(f"No words of length {length}.")
117
+ results.update("")
118
+ return
119
+
120
+ ordered = sorted(
121
+ words, key=lambda word: zipf_frequency(word, "en"), reverse=True
122
+ )
123
+ suffix = f" containing '{needle}'" if needle else ""
124
+ summary.update(
125
+ f"[b]{len(ordered):,}[/] words of length {length}{suffix} "
126
+ "(most frequent first):"
127
+ )
128
+ results.update(" ".join(ordered))
129
+
130
+
131
+ def main() -> None:
132
+ WordLengthApp().run()
133
+
134
+
135
+ if __name__ == "__main__":
136
+ main()