Created
April 13, 2015 16:15
-
-
Save dariusk/57a30fbdd4eefd7215cb to your computer and use it in GitHub Desktop.
Generating rhyming couplets
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
var _ = require('underscore'); | |
var RiTa = require('rita'); | |
var lexicon = new RiTa.RiLexicon(); | |
var rita = RiTa.RiTa; | |
var animals = require('./animals.json').animals; | |
Array.prototype.pick = function() { | |
return this[Math.floor(Math.random()*this.length)]; | |
}; | |
animals = _.map(animals, function(animal) { | |
// only include animals with the stress we're looking for | |
return { | |
name: animal, | |
stress: rita.getStresses(animal) | |
} | |
}); | |
function get101Phrase(words) { | |
var results = _.chain(words) | |
.map(function(el) { | |
var stress = rita.getStresses(el); | |
var pos = rita.getPosTags(el)[0]; | |
if (stress === '1') { | |
el = ['very ','rather ','really '].pick() + el; | |
stress = '1/0/1'; | |
} | |
else if (stress === '0/1') { | |
el = ['so ','not ','quite '].pick() + el; | |
stress = '1/0/1'; | |
} | |
return { | |
word: el, | |
stress: stress | |
}; | |
}) | |
// just get the meter we want | |
.filter(function(el) { | |
return el.stress === '1/0/1'; | |
}) | |
// just return the word, not the stress | |
.map(function(el) { | |
return el.word; | |
}) | |
.value(); | |
return results; | |
} | |
function getAnimalsByStress(stress) { | |
// filter our list of animals | |
var results = _.filter(animals, function(animal) { | |
// only include animals with the stress we're looking for | |
return animal.stress === stress; | |
}); | |
// map it so we're just returning a list of names, and not a list of name/stress objects | |
return _.map(results, function(result) { | |
return result.name; | |
}); | |
} | |
function a(word) { | |
var result = 'a'; | |
var first = rita.getPhonemes(word)[0]; | |
if (first === 'a' | |
|| first === 'e' | |
|| first === 'i' | |
|| first === 'o' | |
|| first === 'u') { | |
result = 'an'; | |
} | |
return result + ' ' + word; | |
} | |
/* | |
var animal_101 = getAnimalsByStress('1/0/1').pick(); | |
var animal_1 = getAnimalsByStress('1').pick(); | |
var couplet = 'The body of ' + a(animal_101) + ', the wisdom of ' + a(animal_1); | |
console.log(couplet); | |
*/ | |
//console.log(rita.getPosTags('apple')); | |
function getAdjRhymes(word) { | |
var rhymes = lexicon.rhymes(word); | |
return _.filter(rhymes, function(rhyme) { | |
// only return rhymes that are tagged 'jj' (adjectives) | |
return rita.getPosTags(rhyme)[0] === 'jj'; | |
}); | |
} | |
function makeCouplet() { | |
var animal_101 = getAnimalsByStress('1/0/1').pick(); | |
var animal_1 = getAnimalsByStress('1').pick(); | |
var couplet = 'The body of ' + a(animal_101) + ', the wisdom of ' + a(animal_1); | |
var adjRhymes = getAdjRhymes(animal_1); | |
if (adjRhymes.length === 0) { | |
return makeCouplet(); | |
} | |
var rhymes = get101Phrase(adjRhymes); | |
if (rhymes.length === 0) { | |
return makeCouplet(); | |
} | |
couplet += '\nToday you join with Gryffindor since you are ' + rhymes.pick(); | |
return couplet; | |
} | |
console.log(makeCouplet()); | |
function getWordsByStress(words, stress) { | |
return _.filter(words, function(word) { | |
// only include animals with the stress we're looking for | |
return rita.getStresses(word) === stress; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment