Back

CLI program witten in Rust. It uses a Markov chain algorithm to generate text.


Repo


The program takes two arguments: the text on which to "train" the algorithm and the number of words to be generated in the output. It works by mapping pairs of consecutive words (bigrams) to a vector of words that follow that pair in the input text. For example, the text "the quick brown fox and the quick blue fox" would produce:

("the", "quick"): ["brown", "blue"]
("quick", "brown"): ["fox"]
("brown", "fox"): ["and"]
("fox", "and"): ["the"]
("and", "the"): ["quick"]
("quick", "blue"): ["fox"]

It creates a loop by randomly selecting a pair of words from these keys, uses the Markov chain to find possible subsequent words, randomly selects the next word from the vector, then treats this as the second word in the next pair and so on. It produces a text output that sounds like the input corpus but retains none of the sense, or creates new sense based on randomness:


Input:

In the beginning God created the heaven and the earth. And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters. And God said, Let there be light: and there was light. And God saw the light, that it was good: and God divided the light from the darkness. And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day. And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters. And God made the firmament, and divided the waters which were under the firmament from the waters which were above the firmament: and it was so. And God called the firmament Heaven. And the evening and the morning were the second day. And God said, Let the waters under the heaven be gathered together unto one place, and let the dry land appear: and it was so. And God called the dry land Earth; and the gathering together of the waters called the Seas: and God saw that it was good.


Output [50]:

The darkness he called Night. And the evening and the earth. And the Spirit of God moved upon the face of the waters, and let the dry land appear: and it was so. And God made the firmament, and divided the waters which were above the firmament: and it was.


Input:

The Yes or No of the Borderzone. Between the aluminum grilles of the balcony he could see the banks of the drained river half a mile away, piers of collapsing sand like the ruined columns of an ornamental canal. He turned his head on the pillow, following the white flex of a power cable as it angled its way around the bedroom door. A manoeuvre of remarkable chasteness. He listened to the water jet against the frosted panes of the shower stall. As the door opened the blurred profile of her body took on a sudden liquid focus, moving across the bedroom like a pink meniscus. She took a cigarette from his packet, then flashed the lighter in her preoccupied eyes. Head in a towel, she lay on the bedspread, smoking the wet cigarette.


Output [90]:

Preoccupied eyes. Head in a towel, she lay on the pillow, following the white flex of a power cable as it angled its way around the bedroom door. A manoeuvre of remarkable chasteness. He listened to the water jet against the frosted panes of the balcony he could see the banks of the shower stall. As the door opened the blurred profile of her body took on a sudden liquid focus, moving across the bedroom door. A manoeuvre of remarkable chasteness. He listened to the water jet against the frosted.