# Pause for Breath --- CS 65 // 2021-04-08 # Weekend Review Sessions ## Assignment 7 - Can be completed individually or in a group of your choosing # Questions ## ...about anything? # Case Study ## Procedural Text Generation ## Procedural Text Generation - As a case study, we were implementing to randomly generate English sentences - The idea we were employing was using the **two previous words** to determine which word should come next - The probability of choosing a word is based on what words commonly follow the previous words ## Procedural Text Generation - Consider the text from *Eric, the Half a Bee*:
> Half a bee, philosophically,
> Must, ipso facto, half not be.
> But half the bee has got to be
> Vis a vis, its entity. D’you see? > > But can a bee be said to be
> Or not to be an entire bee
> When half the bee is not a bee
> Due to some ancient injury?
## Procedural Text Generation - We decided to use a **dictionary** `d` like this: + The keys are pairs of words: `(w1,w2)` + The value `d[w1,w2]` is a list of all the words that commonly follow those two words ## Parsing Words ```py def parse_words(filename): f = open(filename) words = [] for line in f: words.extend(line.split()) f.close() return words ``` ## Markov Analysis ```py def markov(words, order=2): suffixes = {} prefix = tuple(words[:order]) for next_word in words[order:]: if prefix not in suffixes: suffixes[prefix] = [next_word] else: suffixes[prefix].append(next_word) prefix = prefix[1:] + (next_word,) return suffixes ``` ## Random Text ```py def random_text(suffixes, n=100): text = "" prefix = random.choice(list(suffixes.keys())) for i in range(n): next_word = random.choice(suffixes[prefix]) text = text + " " + next_word prefix = prefix[1:] + (next_word,) return text ``` # Assignment 7 Time