CS 70

More Fun with wordgen

Here are some ideas for going further with your word generator. Each one starts from your working WordModel and HashMultiset<T>. Whatever you do, describe it in Fun.md, say how to run it, and show us some of what it made. Put any programs you write in an Experiments folder in your repository (the autograder leaves that folder alone).

Blends

wordgen will learn from more than one list at once, so you can make an archipelago that doesn't exist:

./wordgen -s 3 -n 30 places-hawaii places-wales

Most of what comes out is plainly one language or the other, but some names cross over: kalanfaesyn starts out Hawaiian and ends up Welsh. Work out where and why words cross. (A hint: with \( k = 3 \), a word can only move from one language to the other at a three-character context that both lists contain.) Then try other pairs: Hawaiian and Māori are both Polynesian languages; can a model trained on both make names that are believably either? Report what you find, with examples.

Between Girls' and Boys' Names

/cs70/data/wordlists has first names given mostly to girls, mostly to boys, and to both (names-androgynous). Is an androgynous name halfway between a girl's name and a boy's? Train a model on each of the three lists, invent a few thousand names from each, and measure something about them: what fraction end in -a, say, or in -y. Then train one model on the girls' and boys' lists together, and compare it with the androgynous one. What do you find, and why might that be?

For more, /cs70/data/wordlists/names-spectrum.tsv (and names-fr-spectrum.tsv, for France) gives, for each name, the fraction of babies given that name who were girls. Make a dial: train on the names in a band of that fraction (say, 40% to 60%), or mix a girls' model and a boys' model by choosing each letter with probability \( (1 - w) \) times the boys' model's chance plus \( w \) times the girls', and listen to what happens as \( w \) goes from 0 to 1.

Is It Really New?

A word without a * is only "not in the list you trained on": the verb model happily "invents" rump. Add an option to wordgen (or write a small program of your own) that also loads a big dictionary, such as /cs70/data/web2 (lowercase its words as you read them: it capitalizes proper nouns, and the lists don't), into another HashMultiset<std::string>, and sorts invented words into real-but-not-in-the-list words and genuinely new ones. How does the share of genuinely new words change with \( k \)?

Start From a Prefix

Add a way to ask for words that begin a particular way ("invent a word starting mel"). The catch is that the model may never have seen the context you need. One fix is backoff: if a context was never seen, look at a shorter one. (That means training models for every context length up to \( k \), which is a nice excuse to keep several WordModels in a std::vector.)

Temperature

Instead of choosing each character in proportion to its count, choose it in proportion to its count raised to some power (convert to doubles, and draw a random number with rng.get() scaled to the total). A power above 1 makes the model play it safe (more common letters, more real words); a power below 1 makes it adventurous. Listen to what changes, and describe it.

Words Made of Words

Do the whole thing again with words instead of letters: the context is the last two words, and the model chooses the next word in proportion to how often it followed those two in a book. This is the program in chapter 3 of Kernighan and Pike's The Practice of Programming. Project Gutenberg (https://www.gutenberg.org/) has thousands of public-domain books to train on.

The big difference is the alphabet: the "characters" are now all the words in the book, so weighing every possible next word for every step gets slow. Can you think of a better way? (One is to keep, for each context, a list of the words that followed it; another is to give your HashMultiset<T> an iterator.)

Write a Vollantry

Read The Vollantry, whose invented words follow English's rules for building words. Use your part-of-speech models to invent some nouns, verbs, adjectives and adverbs, and write a paragraph of your own story with them. Put it in Fun.md, with the commands that invented its words. (No code needed for this one, but a story that works is harder than it looks!)

(When logged in, completion status appears here.)