Skip to content

Instantly share code, notes, and snippets.

@anissen
Created April 5, 2015 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anissen/423c07481dd82b7fc9fd to your computer and use it in GitHub Desktop.
Save anissen/423c07481dd82b7fc9fd to your computer and use it in GitHub Desktop.
Markov chain text generator v2
class Markov {
var cache :Map<String, Array<String>>;
var starting_keys :Array<Array<String>>;
public function new() {
cache = new Map<String, Array<String>>();
starting_keys = [];
}
public function train(input :String) {
var text = StringTools.trim(StringTools.replace(input, '\n', ' '));
var sentences = text.split('.');
for (sentence in sentences) {
var words = StringTools.trim(sentence).split(' ');
if (words.length > 2) starting_keys.push([words[0], words[1]]);
}
var words = text.split(' ');
for (t in triples(words)) {
//trace('"${t[0]}" "${t[1]}" "${t[2]}"');
var key = t[0] + ' ' + t[1];
var value = t[2];
if (!cache.exists(key))
cache[key] = [];
cache[key].push(value);
}
}
function triples(words :Array<String>) :Array<Array<String>> {
return [ for (i in 0 ... words.length - 2) [words[i], words[i+1], words[i+2]] ];
}
function get_from_cache(cur :String, next :String) {
var results = cache[cur + ' ' + next];
if (results == null || results.length == 0) {
//trace('Cannot find in cache: "$cur $next"');
return null;
}
return results[Math.floor(Math.random() * results.length)];
}
public function generate(min_size :Int = 50) {
var start_key = starting_keys[Math.floor(Math.random() * starting_keys.length)];
var this_word = start_key[0];
var next_word = start_key[1];
var gen_words = [];
for (i in 0 ... min_size * 2) {
if (this_word != null) {
gen_words.push(this_word);
if (i >= min_size && this_word.indexOf('.') > -1) break;
}
var temp_next_word = next_word;
next_word = get_from_cache(this_word, next_word);
this_word = temp_next_word;
}
return gen_words.join(' ');
}
}
class Test {
static function main() {
var markov = new Markov();
markov.train("For God so loved the world that he gave his one and only Son, that whoever believes in him shall not perish but have eternal life.
For I know the plans I have for you,” declares the Lord, “plans to prosper you and not to harm you, plans to give you hope and a future.
And we know that in all things God works for the good of those who love him, who have been called according to his purpose.
I can do everything through him who gives me strength.
In the beginning God created the heavens and the earth.
Trust in the Lord with all your heart and lean not on your own understanding.
in all your ways acknowledge him, and he will make your paths straight.
Do not conform any longer to the pattern of this world, but be transformed by the renewing of your mind. Then you will be able to test and approve what God’s will is—his good, pleasing and perfect will.
Do not be anxious about anything, but in everything, by prayer and petition, with thanksgiving, present your requests to God.
Therefore go and make disciples of all nations, baptizing them in the name of the Father and of the Son and of the Holy Spirit.
For it is by grace you have been saved, through faith—and this not from yourselves, it is the gift of God.
But the fruit of the Spirit is love, joy, peace, patience, kindness, goodness, faithfulness,
Therefore, I urge you, brothers, in view of God’s mercy, to offer your bodies as living sacrifices, holy and pleasing to God — this is your spiritual act of worship.
The thief comes only to steal and kill and destroy; I have come that they may have life, and have it to the full.
For I am with you, and no one is going to attack and harm you, because I have many people in this city.”
One night the Lord spoke to Paul in a vision: “Do not be afraid; keep on speaking, do not be silent.
So Paul stayed for a year and a half, teaching them the word of God.
I have been crucified with Christ and I no longer live, but Christ lives in me. The life I live in the body, I live by faith in the Son of God, who loved me and gave himself for me.
: If we confess our sins, he is faithful and just and will forgive us our sins and purify us from all unrighteousness.
for all have sinned and fall short of the glory of God,
Jesus answered, “I am the way and the truth and the life. No one comes to the Father except through me.
and teaching them to obey everything I have commanded you. And surely I am with you always, to the very end of the age.”
But God demonstrates his own love for us in this: While we were still sinners, Christ died for us.
Finally, brothers, whatever is true, whatever is noble, whatever is right, whatever is pure, whatever is lovely, whatever is admirable—if anything is excellent or praiseworthy—think about such things.
And the peace of God, which transcends all understanding, will guard your hearts and your minds in Christ Jesus.");
markov.train("Chuck Norris hits every shot he doesn't take.
Chuck Norris once roundhouse kicked a bad guy in the face so hard he saw stars scientists believed to have burnt out centuries ago.
When Alexander Bell invented the telephone he had 3 missed calls from Chuck Norris
Fear of spiders is aracnaphobia, fear of tight spaces is chlaustraphobia, fear of Chuck Norris is called Logic.
There used to be a street named after Chuck Norris, but it was changed because nobody crosses Chuck Norris and lives.
Chuck Norris died 20 years ago, Death just hasn't built up the courage to tell him yet.
If you rate this 5 roundhouse kicks, then Chuck Norris WILL roundhouse kick Justin Bieber's ass.
Chuck Norris has already been to Mars; that's why there are no signs of life.
Some magicans can walk on water, Chuck Norris can swim through land.
Chuck Norris and Superman once fought each other on a bet. The loser had to start wearing his underwear on the outside of his pants.
Chuck Norris once urinated in a semi truck's gas tank as a joke... that truck is now known as Optimus Prime.
Chuck Norris has a grizzly bear carpet in his room. The bear isn't dead it is just afriad to move.
Chuck Norris counted to infinity - twice.
Chuck Norris can cut through a hot knife with butter.
Chuck Norris doesn't flush the toilet, he scares the sh*t out of it.
Chuck Norris is the reason why Waldo is hiding.
Death once had a near-Chuck Norris experience.
Chuck Norris can slam a revolving door.
When the Boogeyman goes to sleep every night, he checks his closet for Chuck Norris.
Chuck Norris once kicked a horse in the chin. Its decendants are known today as Giraffes.
Chuck Norris will never have a heart attack. His heart isn't nearly foolish enough to attack him.
Chuck Norris can win a game of Connect Four in only three moves.
Chuck Norris once got bit by a rattle snake. After three days of pain and agony... the rattle snake died.
When Chuck Norris does a pushup, he isn't lifting himself up, he's pushing the Earth down.
Chuck Norris can light a fire by rubbing two ice-cubes together.
There is no theory of evolution. Just a list of animals Chuck Norris allows to live.
Chuck Norris doesn't call the wrong number. You answer the wrong phone.
Chuck Norris doesn’t wear a watch. He decides what time it is.
The original title for Alien vs. Predator was Alien and Predator vs Chuck Norris.
The film was cancelled shortly after going into preproduction. No one would pay nine dollars to see a movie fourteen seconds long.
Chuck Norris doesn't read books. He stares them down until he gets the information he wants.
Chuck Norris made a Happy Meal cry.
Some people wear Superman pajamas. Superman wears Chuck Norris pajamas.
Chuck Norris won the World Series of Poker using Pokemon cards
A Japanese man can eat 17 hotdogs in 45 seconds. Chuck Norris can eat 45 Japanese men in 17 seconds.
If you spell Chuck Norris in Scrabble, you win. Forever.
Outer space exists because it's afraid to be on the same planet with Chuck Norris.
They once made a Chuck Norris toilet paper, but there was a problem: It wouldn't take shit from anybody.
Chuck Norris jumped off a building once. The ground didn't make it..
Chuck Norris destroyed the periodic table, because Chuck Norris only recognizes the element of surprise.
Chuck Norris' hand is the only hand that can beat a Royal Flush.
Chuck Norris can strangle you with a cordless phone.
Ghosts sit around the campfire and tell Chuck Norris stories.
It takes Chuck Norris 20 minutes to watch 60 Minutes.
Chuck Norris has a Myspace account... on Facebook.
Chuck Norris can kill two stones with one bird
Chuck Norris doesn't pay attention - attention pays him.
When Chuck Norris plays pac-man the ghosts stay in their box.
When Chuck Norris swings a sword, the air bleeds.
Chuck Norris’ PC doesn’t have a Recycle bin – because when Chuck Norris deletes something, there’s no chance of it coming back.
Chuck Norris can stand at the bottom of a bottomless pit.");
/*
var http = new haxe.Http("http://andersnissen.com/blog/2015/03/03/made-a-blog/");
http.onData = function(data) {
trace('Data size: ${data.length}');
markov.train((data));
trace(markov.generate(200));
}
http.request();
*/
trace(markov.generate());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment