Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let p = new Phrase("The dog likes cheese.")
- p.generatePun().then((response) => {
- console.log(response)
- })
- const util = require("./util.js");
- const wordsApi = require("./rhymeApi.js");
- const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
- class Phrase {
- constructor(string, badwords = ["the", "on"]) {
- this.tokens = string.split(" ");
- this.badwords = badwords;
- }
- generatePun() {
- let nonBadWordsIndices = [];
- for (let i = 0; i < this.tokens.length; i++) {
- if (!this.badwords.includes(this.tokens[i].toLowerCase())) {
- nonBadWordsIndices.push(i)
- }
- }
- let replaceIndex = util.getRandomElement(nonBadWordsIndices);
- let wordToBePunned = this.tokens[replaceIndex];
- const isCapitalized = wordToBePunned[0] === wordToBePunned[0].toUpperCase();
- const isAllCaps = wordToBePunned === wordToBePunned.toUpperCase();
- const punctuation= wordToBePunned[wordToBePunned.length - 1];
- const isPunctuated = !letters.includes(punctuation);
- if (isPunctuated) {
- wordToBePunned = wordToBePunned.slice(0, wordToBePunned.length - 1)
- }
- return wordsApi.getRhyme(wordToBePunned).then((rhymes) => {
- let rhyme = util.getRandomElement(rhymes).word;
- if (isCapitalized) {
- rhyme = rhyme.replace(/bw/g, l => l.toUpperCase())
- }
- if (isAllCaps) {
- rhyme = rhyme.toUpperCase();
- }
- if (isPunctuated) {
- rhyme = rhyme + punctuation
- }
- let punnedPhrase = this.tokens.slice();
- punnedPhrase.splice(replaceIndex, 1, rhyme);
- return punnedPhrase.join(" ")
- }).catch((err) => {
- return "Error in generating pun: " + err;
- });
- }
- toString() {
- return this.tokens.join(" ");
- }
- }
- module.exports = Phrase;
- let getRandomElement = (array) => {
- return array[Math.floor(Math.random()*array.length)];
- }
- //returns random int in [0, stop)
- let getRandomIntInRange = (stop) => {
- return Math.floor(Math.random()*stop)
- }
- module.exports = {
- getRandomElement,
- getRandomIntInRange
- }
- const rp = require('request-promise');
- const options = {
- method: 'GET',
- uri: 'https://api.datamuse.com',
- json: true
- }
- let getRhyme = (word) => {
- let rhymeOptions = Object.assign({}, options);
- rhymeOptions.uri += '/words?rel_rhy=' + word;
- return rp(rhymeOptions)
- }
- module.exports = {
- getRhyme
- }
Add Comment
Please, Sign In to add comment