Skip to content

Instantly share code, notes, and snippets.

Created July 31, 2016 19:52
Show Gist options
  • Save anonymous/ccb30d3eb8dd61d7855fd77c713e6316 to your computer and use it in GitHub Desktop.
Save anonymous/ccb30d3eb8dd61d7855fd77c713e6316 to your computer and use it in GitHub Desktop.
https://repl.it/C61A/304 created by sethopia
var myTestText = 'In pre-revolutionary France, the president of a Parlement evolved into a powerful magistrate, a member of the so-called noblesse de robe ("nobility of the gown"), with considerable judicial as well as administrative authority. The name referred to her primary role of presiding over trials and other hearings. In the 17th and 18th centuries, seats in the Parlements, including presidencies, became effectively hereditary, since the holder of the office could ensure that it would pass to an heir by paying the crown a special tax known as the paulette. The post of "first president" (premier président), however, could only be held by the King\'s nominees. The Parlements were abolished by the French Revolution. In modern France the chief judge of a court is known as its president (président de la cour).'
function buildMarkovChain(str) {
var cleanStr = str.toLowerCase().replace(/[,.?()"]/g,''); // remove punctuation
// var cleanStr = str.toLowerCase().replace(/[^a-zA-Z0-9]/g,' '); // this approach removes accented characters
var allWords = [];
allWords = cleanStr.split(/\s/g); // make array allWords with all words in str
var corpusArr = [];
corpusArr = allWords.filter(function(value, index){
return allWords.indexOf(value) === index; // then make corpusArr, an array of unique words in str
});
var corpusObj = {};
// loop through the unique corpusArr words
for (var i = 0; i < corpusArr.length; i++) {
// for each corpusArr word, loop through its indices in allWords
var thisWord = corpusArr[i]; // define thisWord for easy future reference
var indexOfThisWord = 0; // indexOfThisWord tracks the position of thisWord in allWords, starting at the 0th index
var wordsAfterThisWord = []; // wordsAfterThisWord will hold all of the words that come after thisWord in allWords
while (indexOfThisWord > -1) { // keep looping while we're still finding matches
indexOfThisWord = allWords.indexOf(thisWord,indexOfThisWord); // set indexOfThisWord to the 1st occurence of thisWord
if (indexOfThisWord + 1 === allWords.length) { // checks if we're at the last word in the string ...
wordsAfterThisWord = []; // ... and creates an empty array
} else {
var wordToAddToNextWords = allWords[indexOfThisWord + 1]; // picks out the word that follows thisWord in the string
if (wordsAfterThisWord.indexOf(wordToAddToNextWords) === -1) { // check to make sure we're not pushing a duplicate
wordsAfterThisWord.push(wordToAddToNextWords); // if not, then push this into wordsAfterThisWord array
}
}
indexOfThisWord = allWords.indexOf(thisWord,indexOfThisWord + 1); // find the next occurence
}
corpusObj[thisWord] = wordsAfterThisWord;
}
return corpusObj;
}
var markovChain = buildMarkovChain(myTestText);
function writeLine(markovObj, numWords) {
// generate a random starting key in markovObj
var words = Object.keys(markovObj);
nextWord = words[Math.floor(Math.random()*words.length)];
// loop numWords times and build lineToReturn
if (numWords === 0) return "";
var lineToReturn = nextWord + " ";
for (var i = 1; i < numWords; i++) {
var randomVal = Math.floor(Math.random()*markovObj[nextWord].length);
nextWord = (markovObj[nextWord][randomVal]) ? markovObj[nextWord][randomVal] : words[Math.floor(Math.random()*words.length)];
lineToReturn += nextWord + " ";
}
return lineToReturn;
}
function writePoem(markovObj, numLines) {
var poem = "";
for (var i = 0; i < numLines; i++) {
var numWords = Math.floor(Math.random()* 5 + 3);
poem += writeLine(markovObj, numWords) + "\n";
}
return poem;
}
console.log(writePoem(markovChain,35));
Native Browser JavaScript
>>> centuries seats in the gown with considerable
parlement evolved into a parlement evolved
with considerable judicial as well as the
pass to an heir by
post of first president premier président however
and 18th centuries seats in modern france
considerable judicial as administrative authority the
in modern france the
office could only be
were abolished by paying the so-called noblesse
well as its president président
other hearings in modern france the crown
parlement evolved into a parlement evolved
first president premier président de robe
presiding over trials and
nobility of first president of the
parlement evolved into a special tax known
member of a member of
member of the gown with
post of a parlement evolved into
powerful magistrate a member of first
were abolished by the crown a member
an heir by the name
member of first president of
be held by paying the
de la cour judge
be held by the parlements
france the office could only be
the name referred to her primary role
chief judge of first
judge of first president président de
and other hearings in modern france
hearings in the
pre-revolutionary france the 17th and
de robe nobility of the
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment