Guest User

Untitled

a guest
Feb 17th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. let p = new Phrase("The dog likes cheese.")
  2. p.generatePun().then((response) => {
  3. console.log(response)
  4. })
  5.  
  6. const util = require("./util.js");
  7. const wordsApi = require("./rhymeApi.js");
  8.  
  9. const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  10.  
  11. class Phrase {
  12. constructor(string, badwords = ["the", "on"]) {
  13. this.tokens = string.split(" ");
  14. this.badwords = badwords;
  15. }
  16.  
  17. generatePun() {
  18.  
  19. let nonBadWordsIndices = [];
  20. for (let i = 0; i < this.tokens.length; i++) {
  21. if (!this.badwords.includes(this.tokens[i].toLowerCase())) {
  22. nonBadWordsIndices.push(i)
  23. }
  24. }
  25.  
  26.  
  27.  
  28. let replaceIndex = util.getRandomElement(nonBadWordsIndices);
  29. let wordToBePunned = this.tokens[replaceIndex];
  30.  
  31. const isCapitalized = wordToBePunned[0] === wordToBePunned[0].toUpperCase();
  32. const isAllCaps = wordToBePunned === wordToBePunned.toUpperCase();
  33. const punctuation= wordToBePunned[wordToBePunned.length - 1];
  34. const isPunctuated = !letters.includes(punctuation);
  35.  
  36. if (isPunctuated) {
  37. wordToBePunned = wordToBePunned.slice(0, wordToBePunned.length - 1)
  38. }
  39.  
  40. return wordsApi.getRhyme(wordToBePunned).then((rhymes) => {
  41. let rhyme = util.getRandomElement(rhymes).word;
  42. if (isCapitalized) {
  43. rhyme = rhyme.replace(/bw/g, l => l.toUpperCase())
  44. }
  45. if (isAllCaps) {
  46. rhyme = rhyme.toUpperCase();
  47. }
  48. if (isPunctuated) {
  49.  
  50. rhyme = rhyme + punctuation
  51. }
  52.  
  53.  
  54.  
  55. let punnedPhrase = this.tokens.slice();
  56. punnedPhrase.splice(replaceIndex, 1, rhyme);
  57. return punnedPhrase.join(" ")
  58.  
  59. }).catch((err) => {
  60. return "Error in generating pun: " + err;
  61. });
  62.  
  63. }
  64.  
  65. toString() {
  66. return this.tokens.join(" ");
  67. }
  68. }
  69.  
  70. module.exports = Phrase;
  71.  
  72. let getRandomElement = (array) => {
  73. return array[Math.floor(Math.random()*array.length)];
  74. }
  75.  
  76. //returns random int in [0, stop)
  77. let getRandomIntInRange = (stop) => {
  78. return Math.floor(Math.random()*stop)
  79. }
  80.  
  81.  
  82. module.exports = {
  83. getRandomElement,
  84. getRandomIntInRange
  85. }
  86.  
  87. const rp = require('request-promise');
  88.  
  89. const options = {
  90. method: 'GET',
  91. uri: 'https://api.datamuse.com',
  92. json: true
  93. }
  94.  
  95.  
  96. let getRhyme = (word) => {
  97. let rhymeOptions = Object.assign({}, options);
  98. rhymeOptions.uri += '/words?rel_rhy=' + word;
  99. return rp(rhymeOptions)
  100. }
  101.  
  102.  
  103.  
  104.  
  105. module.exports = {
  106. getRhyme
  107. }
Add Comment
Please, Sign In to add comment