rita 3.0.8 → 3.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -10,7 +10,7 @@ RiTa is implemented in JavaScript and Java, with a common [API](https://github.c
10
10
  * Smart lexicon search for words matching part-of-speech, syllable, stress and rhyme patterns
11
11
  * Fast, heuristic algorithms for inflection, conjugation, stemming, tokenization, and more
12
12
  * Letter-to-sound engine for feature analysis of arbitrary words (with/without lexicon)
13
- * Integration of the [RiScript](https://observablehq.com/@dhowe/riscript) scripting language, designed for writers
13
+ * Integration of the [RiScript](https://observablehq.com/@dhowe/riscript) scripting language, designed for writers, now built with the blazing fast [Chevrotain](https://chevrotain.io/) parser
14
14
  * New options for generation via grammars and Markov chains
15
15
  * Published in ESM, CommonJS and as an IIFE
16
16
 
@@ -32,7 +32,7 @@ let data = RiTa.analyze("The elephant took a bite!");
32
32
  console.log(data);
33
33
 
34
34
  // to load a grammar
35
- let grammar = RiTa.grammar(jsonRules);
35
+ let grammar = RiTa.grammar(rulesObjectOrJSON);
36
36
  console.log(grammar.expand());
37
37
  ```
38
38
 
@@ -158,7 +158,7 @@ Some of the following extensions may also be useful:
158
158
 
159
159
  Here you can see the tests in the VSCode _Testing_ view
160
160
 
161
- <img width="800" alt="vscode-tests" src="https://user-images.githubusercontent.com/737638/166626482-bd28d9fa-b300-4b26-b2d7-7ecb6cf31866.png">
161
+ <img width="800" alt="vscode-tests" src="https://github.com/dhowe/ritajs/assets/737638/103665fb-3dc8-448b-9ed3-d706be6797fc">
162
162
 
163
163
  &nbsp;
164
164
 
package/dist/rita.cjs CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var __defProp = Object.defineProperty;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
3
  var __publicField = (obj, key, value) => {
4
4
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
@@ -4789,6 +4789,9 @@ var Util = class _Util {
4789
4789
  static isNum(n) {
4790
4790
  return !isNaN(parseFloat(n)) && isFinite(n);
4791
4791
  }
4792
+ static numOpt(opts, name, def = 0) {
4793
+ return _Util.isNum(_optionalChain([opts, 'optionalAccess', _ => _[name]])) ? opts[name] : def;
4794
+ }
4792
4795
  };
4793
4796
  var RE2 = class {
4794
4797
  constructor(regex2, offset, suffix) {
@@ -26971,41 +26974,8 @@ var Lexicon = class {
26971
26974
  }
26972
26975
  return false;
26973
26976
  }
26974
- /*findStem(word) { // JC: what is this function doing?
26975
-
26976
- let part = word;
26977
- let dict = this.data;
26978
- let words = Object.keys(dict);
26979
-
26980
- while (part.length > 1) {
26981
-
26982
- let pattern = new RegExp('^' + part);
26983
- let guess = words.filter(w => pattern.test(w));
26984
- if (guess && guess.length) {
26985
-
26986
- // always look for shorter words first
26987
- guess.sort((a, b) => a.length - b.length);
26988
-
26989
- // look for words stem(b)===a
26990
- for (let i = 0; i < guess.length; i++) {
26991
- if (this.RiTa.stem(guess[i]) === word) {
26992
- return guess[i];
26993
- }
26994
- }
26995
- }
26996
- part = part.slice(0, -1);
26997
- }
26998
-
26999
- return undefined;
27000
- }*/
27001
- async alliterations(theWord, opts = {}) {
27002
- return new Promise((resolve, reject) => {
27003
- try {
27004
- resolve(this.alliterationsSync(theWord, opts));
27005
- } catch (e) {
27006
- reject(e);
27007
- }
27008
- });
26977
+ async alliterations(word, options = {}) {
26978
+ return this._promise(this.alliterationsSync, [word, options]);
27009
26979
  }
27010
26980
  alliterationsSync(theWord, opts = {}) {
27011
26981
  this._parseArgs(opts);
@@ -27052,14 +27022,8 @@ var Lexicon = class {
27052
27022
  }
27053
27023
  return result;
27054
27024
  }
27055
- async rhymes(theWord, opts = {}) {
27056
- return new Promise((resolve, reject) => {
27057
- try {
27058
- resolve(this.rhymesSync(theWord, opts));
27059
- } catch (e) {
27060
- reject(e);
27061
- }
27062
- });
27025
+ async rhymes(word, options = {}) {
27026
+ return this._promise(this.rhymesSync, [word, options]);
27063
27027
  }
27064
27028
  rhymesSync(theWord, opts = {}) {
27065
27029
  this._parseArgs(opts);
@@ -27097,13 +27061,7 @@ var Lexicon = class {
27097
27061
  return result;
27098
27062
  }
27099
27063
  async spellsLike(word, options = {}) {
27100
- return new Promise((resolve, reject) => {
27101
- try {
27102
- resolve(this.spellsLikeSync(word, options));
27103
- } catch (e) {
27104
- reject(e);
27105
- }
27106
- });
27064
+ return this._promise(this.spellsLikeSync, [word, options]);
27107
27065
  }
27108
27066
  spellsLikeSync(word, options = {}) {
27109
27067
  if (!word || !word.length)
@@ -27112,13 +27070,7 @@ var Lexicon = class {
27112
27070
  return this._byTypeSync(word, options);
27113
27071
  }
27114
27072
  async soundsLike(word, options = {}) {
27115
- return new Promise((resolve, reject) => {
27116
- try {
27117
- resolve(this.soundsLikeSync(word, options));
27118
- } catch (e) {
27119
- reject(e);
27120
- }
27121
- });
27073
+ return this._promise(this.soundsLikeSync, [word, options]);
27122
27074
  }
27123
27075
  soundsLikeSync(word, opts = {}) {
27124
27076
  if (!word || !word.length)
@@ -27137,9 +27089,10 @@ var Lexicon = class {
27137
27089
  }
27138
27090
  }
27139
27091
  opts = opts || {};
27140
- opts.strictPos = true;
27141
- opts.shuffle = true;
27142
27092
  opts.limit = 1;
27093
+ opts.shuffle = true;
27094
+ opts.strictPos = true;
27095
+ opts.minLength = util_default.numOpt(opts, "minLength", 4);
27143
27096
  let result = this.searchSync(regex2, opts);
27144
27097
  if (result.length < 1 && opts.hasOwnProperty("pos")) {
27145
27098
  opts.strictPos = false;
@@ -27152,13 +27105,7 @@ var Lexicon = class {
27152
27105
  return result[0];
27153
27106
  }
27154
27107
  async search(pattern, options) {
27155
- return new Promise((resolve, reject) => {
27156
- try {
27157
- resolve(this.searchSync(pattern, options));
27158
- } catch (e) {
27159
- reject(e);
27160
- }
27161
- });
27108
+ return this._promise(this.searchSync, [pattern, options]);
27162
27109
  }
27163
27110
  searchSync(pattern, options) {
27164
27111
  let words = Object.keys(this.data);
@@ -27209,15 +27156,6 @@ var Lexicon = class {
27209
27156
  return dict ? Object.keys(dict).length : 0;
27210
27157
  }
27211
27158
  //////////////////////////// helpers /////////////////////////////////
27212
- // async similarByType(pattern, options) {
27213
- // return new Promise((resolve, reject) => {
27214
- // try {
27215
- // resolve(this.similarByTypeSync(pattern, options));
27216
- // } catch (e) {
27217
- // reject(e);
27218
- // }
27219
- // });
27220
- // }
27221
27159
  _byTypeSync(theWord, opts) {
27222
27160
  this._parseArgs(opts);
27223
27161
  const dict = this.data;
@@ -27302,14 +27240,13 @@ var Lexicon = class {
27302
27240
  // Handles: pos, limit, numSyllables, minLength, maxLength
27303
27241
  // potentially appends pluralize, conjugate, targetPos
27304
27242
  _parseArgs(opts) {
27305
- opts.limit = opts.limit || 10;
27306
- opts.minDistance = opts.minDistance || 1;
27307
- opts.numSyllables = opts.numSyllables || 0;
27308
- opts.maxLength = opts.maxLength || Number.MAX_SAFE_INTEGER;
27309
- opts.minLength = opts.minLength || 3;
27310
- if (typeof opts.limit !== "number" || opts.limit < 1) {
27243
+ opts.limit = util_default.numOpt(opts, "limit", 10);
27244
+ opts.minDistance = util_default.numOpt(opts, "minDistance", 1);
27245
+ opts.numSyllables = util_default.numOpt(opts, "numSyllables", 0);
27246
+ opts.maxLength = util_default.numOpt(opts, "maxLength", Number.MAX_SAFE_INTEGER);
27247
+ opts.minLength = util_default.numOpt(opts, "minLength", 3);
27248
+ if (opts.limit < 1)
27311
27249
  opts.limit = Number.MAX_SAFE_INTEGER;
27312
- }
27313
27250
  let tpos = opts.pos || false;
27314
27251
  if (tpos && tpos.length) {
27315
27252
  opts.pluralize = tpos === "nns";
@@ -27352,13 +27289,7 @@ var Lexicon = class {
27352
27289
  }
27353
27290
  async _bySoundAndLetter(word, opts) {
27354
27291
  let types = ["sound", "letter"];
27355
- let promises = types.map((type) => new Promise((resolve, reject) => {
27356
- try {
27357
- resolve(this._byTypeSync(word, { ...opts, type }));
27358
- } catch (e) {
27359
- reject(e);
27360
- }
27361
- }));
27292
+ let promises = types.map((type) => this._promise(this._byTypeSync, [word, { ...opts, type }]));
27362
27293
  let results = await Promise.allSettled(promises);
27363
27294
  let [bySound, byLetter] = results.map((r) => r.value);
27364
27295
  if (bySound.length < 1 || byLetter.length < 1)
@@ -27407,6 +27338,15 @@ var Lexicon = class {
27407
27338
  return w.endsWith("ness") || w.endsWith("ism") || pos.indexOf("vbg") > 0 || util_default.MASS_NOUNS.includes(w);
27408
27339
  }
27409
27340
  // helpers ---------------------------------------------------------------
27341
+ _promise(fun, args) {
27342
+ return new Promise((resolve, reject) => {
27343
+ try {
27344
+ resolve(fun.apply(this, args));
27345
+ } catch (e) {
27346
+ reject(e);
27347
+ }
27348
+ });
27349
+ }
27410
27350
  _parseRegex(regex2, opts) {
27411
27351
  if (typeof regex2 === "string") {
27412
27352
  if (opts && opts.type === "stresses") {
@@ -42685,7 +42625,7 @@ RiTa2.SILENT = false;
42685
42625
  RiTa2.SILENCE_LTS = false;
42686
42626
  RiTa2.CDN = "https://www.unpkg.com/rita/";
42687
42627
  RiTa2.PHONES = ["aa", "ae", "ah", "ao", "aw", "ay", "b", "ch", "d", "dh", "eh", "er", "ey", "f", "g", "hh", "ih", "iy", "jh", "k", "l", "m", "n", "ng", "ow", "oy", "p", "r", "s", "sh", "t", "th", "uh", "uw", "v", "w", "y", "z", "zh"];
42688
- RiTa2.VERSION = "3.0.8";
42628
+ RiTa2.VERSION = "3.0.10";
42689
42629
  RiTa2.HAS_LEXICON = typeof __NOLEX__ === "undefined";
42690
42630
  RiTa2.FIRST = 1;
42691
42631
  RiTa2.SECOND = 2;