Created
February 21, 2016 17:16
-
-
Save bluejava/943715be35e0212a760d to your computer and use it in GitHub Desktop.
Demonstration of a Markov Text Generator to create a Sarah Palin endorsement for Donald Trump.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Demonstration of a Markov Text Generator to create a Sarah Palin endorsement for Donald Trump. | |
// See blog post at http://www.bluejava.com/4P9/Sarah-Palin-vs-12-lines-of-JavaScript | |
var text = "“Thank you so much. It’s so great to be here in Iowa. We’re here just thawing out. Todd and I and a couple of our friends here from Alaska, lending our support for the next president of our great United States of America, Donald J. Trump. “Mr. Trump, you’re right, look back there in the press box. Heads are spinning, media heads are spinning. This is going to be so much fun. “Are you ready to make America great again? We all have a part in this. We all have a responsibility. Looking around at all of you, you hardworking Iowa families. You farm families, and teachers, and teamsters, and cops, and cooks. You rockin’ rollers. And holy rollers! All of you who work so hard. You full-time moms. You with the hands that rock the cradle. You all make the world go round, and now our cause is one. “When asked why I would jump into a primary — kind of stirring it up a little bit maybe — and choose one over some friends who are running and I’ve endorsed a couple others in their races before they decided to run for president, I was told left and right, “You are going to get so clobbered in the press. You are just going to get beat up, and chewed up, and spit out.” You know, I’m thinking, And? You know, like you guys haven’t tried to do that every day since that night in ‘08, when I was on stage nominated for VP, and I got to say, “yeah, I’ll go, send me, you betcha. I’ll serve.” And, like you all, I’m still standing. So those of us who’ve kind of gone through the wringer as Mr. Trump has, makes me respect you even more. That you’re here, and you’re putting your efforts, you’re putting reputations, you’re putting relationships on the line to do the right thing for this country. Because you are ready to make America great again. “Well, I am here because like you I know that it is now or never. I’m in it to win it because we believe in America, and we love our freedom. And if you love your freedom, thank a vet. Thank a vet, and know that the United States military deserves a commander-in-chief that our country passionately, and will never apologize for this country. A new commander-in-chief who will never leave our men behind. A new commander-in-chief, one who will never lie to the families of the fallen. I’m in it, because just last week, we’re watching our sailors suffer and be humiliated on a world stage at the hands of Iranian captors in violation of international law, because a weak-kneed, capitulator-in-chief has decided America will lead from behind. And he, who would negotiate deals, kind of with the skills of a community organizer maybe organizing a neighborhood tea, well, he deciding that, “No, America would apologize as part of the deal,” as the enemy sends a message to the rest of the world that they capture and we kowtow, and we apologize, and then, we bend over and say, “Thank you, enemy.” We are ready for a change. We are ready and our troops deserve the best. A new commander-in-chief whose track record of success has proven he is the master at the art of the deal. He is one who would know to negotiate. “Only one candidate’s record of success proves he is the master of the art of the deal. He is beholden to no one but we the people, how refreshing. He is perfectly positioned to let you make America great again. Are you ready for that, Iowa? “No more pussy footin’ around! Our troops deserve the best, you deserve the best! “He is from the private sector, not a politician, can I get a “Hallelujah!” Where, in the private sector, you actually have to balance budgets in order to prioritize, to keep the main thing, the main thing, and he knows the main thing: a president is to keep us safe economically and militarily. He knows the main thing, and he knows how to lead the charge. So troops, hang in there, because help’s on the way because he, better than anyone, isn’t he known for being able to command, fire! Are you ready for a commander-in-chief, you ready for a commander-in-chief who will let our warriors do their job and go kick ISIS ass? Ready for someone who will secure our borders, to secure our jobs, and to secure our homes? Ready to make America great again, are you ready to stump for Trump? I’m here to support the next president of the United States, Donald Trump. “Now, eight years ago, I warned that Obama’s promised fundamental transformation of America. That is was going to take more from you, and leave America weaker on the world stage. And that we would soon be unrecognizable. Well, it’s the one promise that Obama kept. But he didn’t do it alone, and this is important to remember, especially those of you, like me, a member of the GOP, this is what we have to remember, in this very contested, competitive, great primary race. “Trump’s candidacy, it has exposed not just that tragic ramifications of that betrayal of the transformation of our country, but too, he has exposed the complicity on both sides of the aisle that has enabled it, okay? Well, Trump, what he’s been able to do, which is really ticking people off, which I’m glad about, he’s going rogue left and right, man, that’s why he’s doing so well. He’s been able to tear the veil off this idea of the system. The way that the system really works, and please hear me on this, I want you guys to understand more and more how the system, the establishment, works, and has gotten us into the troubles that we are in in America. The permanent political class has been doing the bidding of their campaign donor class, and that’s why you see that the borders are kept open. For them, for their cheap labor that they want to come in. That’s why they’ve been bloating budgets. It’s for crony capitalists to be able suck off of them. It’s why we see these lousy trade deals that gut our industry for special interests elsewhere. We need someone new, who has the power, and is in the position to bust up that establishment to make things great again. It’s part of the problem. “His candidacy, which is a movement, it’s a force, it’s a strategy. It proves, as long as the politicos, they get to keep their titles, and their perks, and their media ratings, they don’t really care who wins elections. Believe me on this. And the proof of this? Look what’s happening today. Our own GOP machine, the establishment, they who would assemble the political landscape, they’re attacking their own frontrunner. Now would the Left ever, would the DNC ever come after their frontrunner and her supporters? No because they don’t eat their own, they don’t self-destruct. But for the GOP establishment to be coming after Donald Trump’s supporters even, with accusations that are so false. They are so busted, the way that this thing works. “We, you, a diverse, dynamic, needed support base that they would attack. And now, some of them even whispering, they’re ready to throw in for Hillary over Trump because they can’t afford to see the status quo go, otherwise, they won’t be able to be slurping off the gravy train that’s been feeding them all these years. They don’t want that to end. “Well, and then, funny, ha ha, not funny, but now, what they’re doing is wailing, “well, Trump and his, uh, uh, uh, Trumpeters, they’re not conservative enough.” Oh my goodness gracious. What the heck would the establishment know about conservatism? Tell me, is this conservative? GOP majorities handing over a blank check to fund Obamacare and Planned Parenthood and illegal immigration that competes for your jobs, and turning safety nets into hammocks, and all these new Democrat voters that are going to be coming on over border as we keep the borders open, and bequeathing our children millions in new debt, and refusing to fight back for our solvency, and our sovereignty, even though that’s why we elected them and sent them as a majority to DC. No! If they’re not willing to do that, then how are they to tell us that we’re not conservative enough in order to be able to make these changes in America that we know need to be…Now they’re concerned about this ideological purity? Give me a break! Who are they to say that? Oh tell somebody like, Phyllis Schlafly, she is the Republican, conservative movement icon and hero and a Trump supporter. Tell her she’s not conservative. How ‘bout the rest of us? Right wingin’, bitter clingin’, proud clingers of our guns, our god, and our religions, and our Constitution. Tell us that we’re not red enough? Yeah, coming from the establishment. Right. “Well, he being the only one who’s been willing, he’s got the guts to wear the issues that need to be spoken about and debate on his sleeve, where the rest of some of these establishment candidates, they just wanted to duck and hide. They didn’t want to talk about these issue until he brought ‘em up. In fact, they’ve been wearing a, this, political correctness kind of like a suicide vest. And enough is enough. These issues that Donald Trump talks about had to be debated. And he brought them to the forefront. And that’s why we are where we are today with good discussion. A good, heated, and very competitive primary is where we are. And now though, to be lectured that, “Well, you guys are all sounding kind of angry,” is what we’re hearing from the establishment. Doggone right we’re angry! Justifiably so! Yes! You know, they stomp on our neck, and then they tell us, “Just chill, okay just relax.” Well, look, we are mad, and we’ve been had. They need to get used to it. “This election is more than just your basic ABCs, anybody but Clinton. It’s more than that this go-around. When we’re talking about a nation without borders. When we’re talking about bankruptcies in our federal government. Debt that our children and our grandchildren, they’ll never be able to pay off. When we’re talking about no more Reaganesque power that comes from strength. Power through strength. Well, then, we’re talking about our very existence, so no, we’re not going to chill. In fact it’s time to drill, baby, drill down, and hold these folks accountable. And we need to stop the self-sabotage and elect new, and independent, a candidate who represents that and represents America first, finally. Pro-Constitution, common-sense solutions, that he brings to the table. Yes the status quo has got to go. Otherwise we’re just going to get more of the same, and with their failed agenda, it can’t be salvaged. It must be savaged. And Donald Trump is the right one to do that. “Are you ready for new? And are you ready for the leader who will let you make America great again? It’s gonna take a whole team. It’s gonna take a whole team. Fighters, all of us, in the private sector. Fighters in the House and the Senate. So, our friends, who are fighters in the House and the Senate today, they need to stay there and help out. They can help our new leader in the positions that they are in. “Let me say something really positive about one of those individuals: Rand Paul. I’m going to tell you about that libertarian streak in him that is healthy, because he knows, you only go to war if you’re determined to win the war! And you quit footin’ the bill for these nations who are oil-rich, we’re paying for some of their squirmishes that have been going on for centuries. Where they’re fightin’ each other and yellin’ “Allah Akbar” calling Jihad on each other’s heads for ever and ever. Like I’ve said before, let them duke it out and let Allah sort it out. We’ll fight for American interests, and as Donald Trump has said, other nations where we have been footin’ the bill, but we haven’t prioritized our own domestic budgets well enough to be able to afford what we’re doing overseas. Things are gonna change under President Trump. “So it can be an unbeatable team with fighters there in the House and the Senate. Yeah, our leader is a little bit different. He’s a multi-billionaire. Not that there’s anything wrong with that. But, it’s amazing, he is not elitist at all. Oh, I just hope you all get to know him more and more as a person, and a family man. What he’s been able to accomplish, with his um, it’s kind of this quiet generosity. Yeah, maybe his largess kind of, I don’t know, some would say gets in the way of that quiet generosity, and, uh, his compassion, but if you know him as a person and you’ll get to know him more and more, you’ll have even more respect. Not just for his record of success, and the good intentions for America, but who he is as a person. He’s not an elitist. And yes, as a multi-billionaire, we still root him on, because he roots us on. And he has, he’s spent his life with the workin’ man. And he tells us Joe six packs, he said, “You know, I’ve worked very, very hard. And I’ve succeeded. Hugely I’ve succeeded,” he says. And he says, “I want you to succeed too.” And that is refreshing, because he, as he builds things, he builds big things, things that touch the sky, big infrastructure that puts other people to work. He has spent his life looking up and respecting the hard-hats and the steel-toed boots and the work ethic that you all have within you. He, being an optimist, passionate about equal-opportunity to work. The self-made success of his, you know that he doesn’t get his power, his high, off of OPM, other people’s money, like a lot of dopes in Washington do. They’re addicted to OPM, where they take other people’s money, and then their high is getting to redistribute it, right? And then they get to be really popular people when they get to give out your hard money. Well, he doesn’t do that. His power, his passion, is the fabric of America. And it’s woven by work ethic and dreams and drive and faith in the Almighty, what a combination. “Are you ready to share in that again, Iowa? Because that’s what’s going to let you make America great again. He’s going to be able to empower you to look out for one-another again instead of relying on a bankrupt government to supposedly be looking out for you. No, and I think you’re ready for that. And Iowa, I believe too that you’re ready to see that our vets are treated better than illegal immigrants are treated in this country. And you’re ready for the tax reform he talks about to open up main street again. And you’re ready to stop the race-baiting and the division based on color and zip code, to unify around the right issues. The issues important to me, or I wouldn’t be endorsing him. Pro-life, pro-Second Amendment, strict constitutionality. Those things that are unifying values and their time-tested truths involved. These are unifying values from big cities and tiny towns, from big mountain states and the Big Apple, to the big, beautiful heartland that’s in between. “Now, finally friends, I want you to try to picture this, it’s a nice thing to picture. Exactly one year from tomorrow, former President Barack Obama. He packs up the teleprompters and the selfie-sticks, and the Greek columns, and all that hopey, changey stuff and he heads on back to Chicago, where I’m sure he can find some community there to organize again. There, he can finally look up, President Obama will be able to look up, and there, over his head, he’ll be able to see that shining, towering, Trump tower. Yes, Barack, he built that, and that says a lot. Iowa, you say a lot, being here tonight, supporting the right man who will allow you to make America great again. God bless you! God bless the United States of America and our next president of the United States, Donald J. Trump!”" | |
var rwords = text.split(" "), | |
words = lc(function(w,i) { | |
var m = w.match("([\\w'’,\\.!\\?-]+)") | |
if(m && m.length > 1) | |
return m[1] | |
},rwords), | |
starts = words.filter(function(w,i) { | |
return i == 0 || | |
isSentenceEnd(words[i-1]) | |
}) | |
function isSentenceEnd(w) | |
{ | |
return w.endsWith(".") || w.endsWith("?") || w.endsWith("!") || w.endsWith("”") | |
} | |
// List Comprehension - | |
// Pass an array and a function - the function is called with each element of the array - and the | |
// items returned are what forms the new array which is returned. If null is returned from the comprehension function | |
// then nothing is added for that iteration - if an array is returned, then many items are added. This means that the array | |
// returned can be larger, smaller, or of equal size to the original. | |
function lc(fn,ar) | |
{ | |
var r = [ ]; | |
for(var i=0,l=ar.length;i<l;i++) | |
{ | |
var x = ar[i]; | |
var x2 = fn(x,i); | |
if(Array.isArray(x2)) | |
x2.forEach(r.push); | |
else | |
if((x2 !== null) && x2 !== undefined) | |
r.push(x2); | |
} | |
return r; | |
} | |
function getRandFirstWord() | |
{ | |
return starts[rand(starts.length)] | |
} | |
function getRandNextWordArray(w,wc) | |
{ | |
w = w.toLowerCase() | |
var nwa = lc(function(ww,i) { | |
if(ww.toLowerCase() == w) | |
return i | |
},words) | |
var result = [] | |
var index = nwa[rand(nwa.length)] // choose a random frag | |
while(wc--) | |
{ | |
nwo = words[index++] | |
result.push(nwo) | |
if(isSentenceEnd(nwo)) | |
return result | |
} | |
return result | |
} | |
function finishSentence(sent) | |
{ | |
while(!isSentenceEnd(arrayLast(sent))) | |
sent = sent.concat(getRandNextWordArray( arrayLast(sent),rand(1,7))) | |
return sent.join(" ") | |
} | |
function getSentence() | |
{ | |
var sent = [getRandFirstWord()] | |
return finishSentence(sent) | |
} | |
function getParagraph(numSentences) | |
{ | |
return range(numSentences) | |
.map(function() { return getSentence() }) | |
.join(" ") | |
} | |
// simplified range function for use | |
function range(n) | |
{ | |
var ret = [] | |
for(var x=0;x<n;x++) ret.push(x) | |
return ret; | |
} | |
function rand(x,y) | |
{ | |
// rand(3) = rand(0,2) | |
if(!y) | |
return Math.floor(Math.random() * x); // simple random | |
else | |
return Math.floor(Math.random() * (y - x + 1)) + x; | |
} | |
// Return last element of array | |
function arrayLast(ar) | |
{ | |
if(ar.length > 0) | |
return ar[ar.length - 1] | |
} | |
function getSpeech(numParagraphs) | |
{ | |
var inner = range(numParagraphs) | |
.map(function() { return getParagraph(rand(2,5)) }) | |
.join("<br/><br/>") | |
return "Hello everyone! - Its so great to be here!<br/><br/>" + inner + "<br/><br/>Thank you!" | |
} | |
console.log(getSpeech(3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment