N-grams and Markov chains

The text below is actually an n-gram model of my blog post. See end of n-gram for intelligible version.

(‘H’, ‘o’, ‘l’, ‘l’, ‘y’, ‘w’, ‘o’, ‘o’, ‘d’, ‘.’),
(‘ ‘, ‘d’, ‘i’, ‘f’, ‘f’, ‘e’, ‘r’, ‘e’, ‘n’, ‘t’),
(‘ ‘, ‘b’, ‘e’, ‘ ‘, ‘h’, ‘o’, ‘n’, ‘e’, ‘s’, ‘t’),
(‘a’, ‘r’, ‘k’, ‘o’, ‘v’, ‘_’, ‘g’, ‘e’, ‘n’, ‘e’),
(‘o’, ‘u’, ‘l’, ‘d’, ‘ ‘, ‘I’, ‘ ‘, ‘r’, ‘e’, ‘a’),
(‘l’, ‘l’, ‘y’, ‘ ‘, ‘u’, ‘s’, ‘e’, ‘ ‘, ‘t’, ‘h’),
(‘t’, ‘e’, ‘s’, ‘ ‘, ‘a’, ‘ ‘, ‘n’, ‘e’, ‘w’, ‘ ‘),
(‘i’, ‘n’, ‘g’, ‘ ‘, ‘d’, ‘i’, ‘f’, ‘f’, ‘e’, ‘r’),
(‘n’, ‘t’, ‘o’, ‘ ‘, ‘a’, ‘n’, ‘o’, ‘t’, ‘h’, ‘e’),
(‘a’, ‘r’, ‘k’, ‘o’, ‘v’, ‘ ‘, ‘c’, ‘h’, ‘a’, ‘i’),
(‘u’, ‘r’, ‘n’, ‘ ‘, ‘a’, ‘ ‘, ‘M’, ‘u’, ‘s’, ‘h’),
(‘f’, ‘,’, ‘ ‘, ‘“’, ‘w’, ‘o’, ‘u’, ‘l’, ‘d’, ‘n’),
(‘t’, ‘s’, ‘i’, ‘d’, ‘e’, ‘ ‘, ‘o’, ‘f’, ‘ ‘, ‘e’),
(‘o’, ‘m’, ‘ ‘, ‘B’, ‘u’, ‘r’, ‘g’, ‘e’, ‘r’, ‘ ‘),
(‘e’, ‘s’, ‘t’, ‘.’, ‘ ‘, ‘I’, ‘n’, ‘i’, ‘t’, ‘i’),
(‘T’, ‘i’, ‘s’, ‘c’, ‘h’, ‘ ‘, ‘t’, ‘o’, ‘ ‘, ‘H’),
(‘ ‘, ‘t’, ‘o’, ‘ ‘, ‘d’, ‘o’, ‘ ‘, ‘s’, ‘o’, ‘m’),
(‘n’, ‘g’, ‘ ‘, ‘a’, ‘ ‘, ‘r’, ‘e’, ‘c’, ‘e’, ‘n’),
(‘n’, ‘ ‘, ‘i’, ‘s’, ‘ ‘, ‘s’, ‘l’, ‘i’, ‘g’, ‘h’),
(‘w’, ‘o’, ‘o’, ‘d’, ‘.’, ‘.’, ‘.’, ‘ ‘, ‘B’, ‘o’),
(‘.’, ‘ ‘, ‘I’, ‘n’, ‘i’, ‘t’, ‘i’, ‘a’, ‘l’, ‘l’),
(‘t’, ‘.’, ‘ ‘, ‘I’, ‘n’, ‘i’, ‘t’, ‘i’, ‘a’, ‘l’),
(‘o’, ‘ ‘, ‘d’, ‘o’, ‘ ‘, ‘s’, ‘o’, ‘m’, ‘e’, ‘t’),
(‘g’, ‘n’, ‘m’, ‘e’, ‘n’, ‘t’, ‘.’, ‘ ‘, ‘I’, ‘ ‘),
(‘”’, ‘ ‘, ‘C’, ‘o’, ‘u’, ‘l’, ‘d’, ‘ ‘, ‘I’, ‘ ‘),
(‘a’, ‘ ‘, ‘m’, ‘o’, ‘m’, ‘e’, ‘n’, ‘t’, ‘ ‘, ‘o’),
(‘o’, ‘ ‘, ‘a’, ‘n’, ‘o’, ‘t’, ‘h’, ‘e’, ‘r’, ‘ ‘),
(‘n’, ‘g’, ‘ ‘, ‘d’, ‘r’, ‘i’, ‘v’, ‘i’, ‘n’, ‘g’),
(‘i’, ‘n’, ‘_’, ‘f’, ‘i’, ‘l’, ‘e’, ‘(‘, ‘)’, ‘\xa0’),
(‘ ‘, ‘n’, ‘e’, ‘e’, ‘d’, ‘e’, ‘d’, ‘ ‘, ‘s’, ‘o’),
(‘d’, ‘e’, ‘d’, ‘ ‘, ‘s’, ‘o’, ‘m’, ‘e’, ‘t’, ‘h’),
(‘f’, ‘o’, ‘r’, ‘ ‘, ‘t’, ‘h’, ‘i’, ‘s’, ‘ ‘, ‘a’),
(‘m’, ‘e’, ‘n’, ‘t’, ‘.’, ‘ ‘, ‘I’, ‘ ‘, ‘t’, ‘h’),
(‘i’, ‘n’, ‘g’, ‘ ‘, ‘a’, ‘ ‘, ‘r’, ‘e’, ‘c’, ‘e’),
(‘n’, ‘m’, ‘e’, ‘n’, ‘t’, ‘.’, ‘ ‘, ‘I’, ‘ ‘, ‘t’),
(‘ ‘, ‘t’, ‘h’, ‘e’, ‘ ‘, ‘c’, ‘o’, ‘n’, ‘t’, ‘e’),
(‘w’, ‘a’, ‘n’, ‘t’, ‘e’, ‘d’, ‘ ‘, ‘t’, ‘o’, ‘ ‘),
(‘e’, ‘c’, ‘t’, ‘i’, ‘o’, ‘n’, ‘s’, ‘?’, ‘”’, ‘ ‘),
(‘c’, ‘i’, ‘p’, ‘e’, ‘ ‘, ‘i’, ‘n’, ‘t’, ‘o’, ‘ ‘),
(‘_’, ‘l’, ‘i’, ‘n’, ‘e’, ‘s’, ‘_’, ‘i’, ‘n’, ‘_’),
(‘T’, ‘h’, ‘e’, ‘ ‘, ‘M’, ‘a’, ‘r’, ‘k’, ‘o’, ‘v’),
(‘l’, ‘y’, ‘ ‘, ‘p’, ‘a’, ‘l’, ‘a’, ‘t’, ‘a’, ‘b’),
(‘ ‘, ‘d’, ‘u’, ‘r’, ‘i’, ‘n’, ‘g’, ‘ ‘, ‘a’, ‘ ‘),
(‘t’, ‘e’, ‘ ‘, ‘t’, ‘e’, ‘x’, ‘t’, ‘ ‘, ‘t’, ‘h’),
(‘t’, ‘ ‘, ‘i’, ‘t’, ‘ ‘, ‘b’, ‘e’, ‘ ‘, ‘a’, ‘w’),
(‘r’, ‘e’, ‘d’, ‘ ‘, ‘d’, ‘u’, ‘r’, ‘i’, ‘n’, ‘g’),
(‘t’, ‘i’, ‘v’, ‘e’, ‘ ‘, ‘t’, ‘e’, ‘x’, ‘t’, ‘ ‘),
(‘r’, ‘n’, ‘ ‘, ‘a’, ‘ ‘, ‘M’, ‘u’, ‘s’, ‘h’, ‘r’),
(‘e’, ‘d’, ‘ ‘, ‘s’, ‘o’, ‘m’, ‘e’, ‘t’, ‘h’, ‘i’),
(‘ ‘, ‘n’, ‘e’, ‘w’, ‘ ‘, ‘r’, ‘e’, ‘c’, ‘i’, ‘p’)

The Markov chain is slightly confusing, but fascinating to be honest. Initially, I struggled with the content because I wanted to do something outside of evaluating lyrics — at least for this assignment. I thought a while about creating predictive text using driving directions from Tisch to Hollywood… Boring. I needed something else. Something different. My aha moment occurred during a recent hunger pang. I thought to myself, “wouldn’t it be awesome to generate text that creates a new recipe based on a set of directions?” Could I really use the Markov chain to turn a Mushroom Burger recipe into another dish I found equally palatable? I used the function markov_generate_from_lines_in_file()  on my recipe source text. Here’s the output:

Out[306]:
['Transfer cook later if desired.) Moisten hands with a knife.',
 '365 Everyday Value, Extra Virgin Olive oil',
 'Heat remaining 2 tablespoons olive Oil, 16.9 fl',
 'Transfer cooked through, 5 to 6 minutes. (At this point you can refrigerate the juice and bread crumbs to',
 '365 Everyday Value, Extra Virgin Olive oil',
 '3 (8 ounce) packages sliced fresh mushrooms to a cutting board and garlic to the mixture to cooked mushro',
 'Prep 15 minutes.',
 '3 (8 ounce) packages sliced fresh mushrooms have given up their juice has almost evaporated, about 10 min',
 '365 Everyday Value, Extra Virgin Olive oil in a large skillet over medium heat; add more to the mixture, ',
 '365 Everyday Value, Extra Virgin Olive Oil, 16.9 fl']

Not what I intended but I think it’s totally possible to generate a new recipe using the Markov chain. I’ll keep testing!

Full code here:

One thought on “N-grams and Markov chains”

Comments are closed.