Skip to content

Instantly share code, notes, and snippets.

@joelalejandro
Created October 17, 2016 06:26
Show Gist options
  • Select an option

  • Save joelalejandro/b466b4c20c7ceaf22175cb3fd98e5c8b to your computer and use it in GitHub Desktop.

Select an option

Save joelalejandro/b466b4c20c7ceaf22175cb3fd98e5c8b to your computer and use it in GitHub Desktop.
function getRhymes(word, tweets = this.tweets) {
const rhymingContext = this.settings.rhyming_plugins[this.settings.detournement_context.language];
let rhymingPackage = require(rhymingContext.package);
if (rhymingContext.requires_new) {
rhymingPackage = new rhymingPackage();
}
const sanitizedWord = word.replace(/[^a-záéíóúñü]+/gi, '');
try {
const rhymingRawOutput = rhymingPackage[rhymingContext.input].call(null, sanitizedWord);
let rhymingOutput = {};
if (rhymingContext.output.type === 'object' && typeof rhymingRawOutput === 'object') {
Object.keys(rhymingContext.output.mapping).forEach((destination) => {
rhymingOutput[destination] = rhymingRawOutput[rhymingContext.output.mapping[destination]];
});
}
if (rhymingOutput.rhyme === '') {
console.log('Rhyme not found');
return [];
} else {
console.log('Rhyme found', rhymingOutput);
let filtered = tweets.filter(tweet => {
return new RegExp(`${rhymingOutput.rhyme}(\.)?$`, 'i').test(this.sanitize(tweet));
});
console.log(`Rhyming tweets: ${filtered.length}`);
return filtered;
}
} catch (e) {
console.log('Invalid word: ' + sanitizedWord);
return [];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment