cthulhusay.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. R'lyehian language generator. The one and only cthulhu-fhtagn-ator.
  3. Copyright 2017 Davide Alberani <da@erlug.linux.it>
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Summoned from https://www.yog-sothoth.com/wiki/index.php/R'lyehian
  14. var WORDS = ["'ai", "'bthnk", "'fhalma", 'ah', 'athg', 'bug', "ch'", 'chtenff', 'ebumna', 'ee', 'ehye', 'ep', 'fhtagn',
  15. "fm'latgh", 'ftaghu', 'geb', 'gnaiih', "gof'nn", 'goka', 'gotha', "grah'n", "hafh'drn", 'hai', 'hlirgh',
  16. 'hrii', 'hupadgh', 'ilyaa', "k'yarnak", 'kadishtu', "kn'a", "li'hee", 'llll', 'lloig', "lw'nafh", "mnahn'",
  17. "n'gha", "n'ghft", 'nglui', "nilgh'ri", 'nog', 'nw', 'ooboshu', "orr'e", 'phlegeth', "r'luh", 'ron', "s'uhn",
  18. "sgn'wahl", 'shagg', 'shogg', 'shtunggli', 'shugg', "sll'ha", "stell'bsna", "syha'h", 'tharanak', 'throd',
  19. 'uaaah', "uh'e", 'uln', 'vulgtlagln', 'vulgtm', "wgah'n", "y'hah", 'ya', 'zhro'];
  20. // I'm confident that we'll find other conjunctions
  21. var CONJUNCTIONS = ['mg'];
  22. var PREFIXES = ['c', "f'", "h'", 'na', 'nafl', 'ng', 'nnn', "ph'", 'y'];
  23. var SUFFIXES = ['agl', 'nyth', 'og', 'or', 'oth', 'yar'];
  24. var SENTENCE_ENDS = ['!', '?', '.', '.', '.'];
  25. var PUNCTUATIONS = SENTENCE_ENDS.concat([',', ';']);
  26. var _ = require('lodash');
  27. function sampleMany(seq, freq) {
  28. seq = seq || [];
  29. freq = freq || 1;
  30. var selection = [];
  31. var howMany = Math.round(freq * seq.length);
  32. _.times(howMany, function() { selection.push(_.sample(seq)); });
  33. return selection;
  34. }
  35. /** Cthulhu says hi! **/
  36. function cthulhu_say(opts) {
  37. opts = opts || {};
  38. var o = {
  39. words: 10,
  40. conjuncionsFreq: 0.2,
  41. prefixesFreq: 0.25,
  42. suffixesFreq: 0.25,
  43. pluralsFreq: 0.3,
  44. punctuationsFreq: 0.1
  45. }
  46. _.merge(o, opts);
  47. var _range = _.range(o.words);
  48. var sentence = [];
  49. _.times(o.words, function() { sentence.push(_.sample(WORDS)); });
  50. _.each(sampleMany(_range, o.pluralsFreq), function(val, idx) {
  51. sentence[val] = sentence[val] + sentence[val][sentence[val].length-1];
  52. });
  53. _.each(sampleMany(_range, o.prefixesFreq), function(val, idx) {
  54. sentence[val] = _.sample(PREFIXES) + sentence[val];
  55. });
  56. _.each(sampleMany(_range, o.suffixesFreq), function(val, idx) {
  57. sentence[val] = _.sample(SUFFIXES) + sentence[val];
  58. });
  59. _.each(sampleMany(_.range(1, o.words-1), o.conjuncionsFreq), function(val, idx) {
  60. sentence[val] = _.sample(CONJUNCTIONS);
  61. });
  62. _.each(sampleMany(_.range(1, o.words-1), o.punctuationsFreq), function(val, idx) {
  63. var punctuation = _.sample(PUNCTUATIONS);
  64. if (_.includes(PUNCTUATIONS, sentence[val][sentence[val].length-1]) || _.includes(CONJUNCTIONS, _.lowerCase(sentence[val]))) {
  65. return;
  66. }
  67. sentence[val] = sentence[val] + punctuation;
  68. if (_.includes(SENTENCE_ENDS, punctuation)) {
  69. sentence[val+1] = _.capitalize(sentence[val+1]);
  70. }
  71. });
  72. var sentenceText = _.join(sentence, ' ');
  73. if (sentenceText) {
  74. sentenceText = _.upperFirst(sentenceText);
  75. if (o.punctuationsFreq) {
  76. sentenceText = sentenceText + _.sample(SENTENCE_ENDS);
  77. }
  78. }
  79. return sentenceText;
  80. }
  81. module.exports.cthulhu_say = cthulhu_say;
  82. if (require.main === module) {
  83. console.log(cthulhu_say());
  84. }