In Part 4 we give TypeGym something interesting to type.
Up until now the app was functional, but the practice text was basically hard-coded. This episode is all about building a small text pipeline so each session can pull content from different places (without touching the UI or typing logic).
What We Covered
- Added a
TextSourceenum that defines where practice text comes from:- static text
- generated nonsense (random words)
- generated weighted nonsense (word frequency-like sampling)
- a slice of lines from a file
- Wired
TextSourceintoConfig(config.text_source) - Implemented
get_text(config)to dispatch to the selected source - Added
randto the project and introduced two small data files:data/words.txtdata/words_weighted.txt
Design Insights
Treat “text” as an input system
Typing logic and UI shouldn’t care where the text came from.
By making “text generation/loading” an isolated module (and routing it through Config), we can keep the rest of the app stable while we iterate on content:
- swap sources
- add new sources
- improve formatting and sampling
- eventually add “real corpora” or smarter generators
All without ripping up the main loop.
“Nonsense text” is surprisingly useful
Random words are a great default because:
- it’s always available (no external files required beyond the word list)
- it produces lots of variety
- it’s easy to scale (more/less words, different chunking, different weighting)
And for a typing trainer series, it’s perfect: we can keep building features without needing to constantly curate practice texts.
Implementation Highlights
1) TextSource enum
We add a small enum that represents how we fetch practice text:
This is the key abstraction for the episode: everything else becomes “just implementation details”.
2) Central get_text(config) dispatche
get_text() now does the obvious thing: match on config.text_source and return the requested content.
3) Random nonsense from data/words.txt
We read a list of words, pick a bunch at random, and format them into lines:
- sample 100 words
- chunk into groups of 10
- join into multiple lines separated by
\n
This gives a predictable layout while still being random every session.
4) Weighted nonsense from data/words_weighted.txt
Same idea, but with a “word + weight” file:
- parse lines like
word 123 - sample based on weights
- format the same way (lines of 10 words)
The result feels more “natural” because common words appear more often. Although it is still a complete nonsense.
5) File-based text: pick a random window of lines
For file mode we keep it intentionally simple:
- read the file
- pick up to 5 lines
- choose a random start index so each session feels different
It’s not paragraph-aware yet, but it’s a great stepping stone.
What’s Next?
Now that TypeGym can pull practice text from multiple sources, the next improvements become much easier:
- Markov chain to generate more interesting practice texts
- Command line arguments to configure our app
- Improvements to existing functionality (better sampling?)
Project Code
You will find the complete source code here: typegym