From b282512ffb68b697217d6a36f21417136f631ace Mon Sep 17 00:00:00 2001 From: dhowe Date: Sun, 9 Jun 2024 12:33:48 +0100 Subject: [PATCH] remove async call in Conjugator constructor --- src/conjugator.js | 15 +++++++++------ src/lexicon.js | 1 + src/randgen.js | 15 ++++++++------- src/rita_dict.js | 15 ++++++--------- test/conjugator-tests.js | 8 ++++++++ test/lexicon-tests.js | 5 +++++ 6 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/conjugator.js b/src/conjugator.js index 2cb2614..7cacb9d 100644 --- a/src/conjugator.js +++ b/src/conjugator.js @@ -1,3 +1,5 @@ +import { log } from "@tensorflow/tfjs"; + /** * @memberof module:rita */ @@ -39,12 +41,13 @@ class Conjugator { constructor(parent) { this.RiTa = parent; this._reset(); - this.RiTa.search({ pos: 'v', limit: -1, minLength: -1 }) - .then(res => { - this.allVerbs = res; - this.verbsEndingInE = res.filter(v => v.endsWith("e")); - this.verbsEndingInDouble = res.filter(v => /([^])\1$/.test(v)); - }); + let { data } = parent.lexicon; + this.allVerbs = Object.keys(data).filter(word => data[word][1].split(' ').includes('vb')); + //console.log('Conjugator', 'allVerbs', this.allVerbs.slice(0, 100) + '...'); + // this.allVerbs.slice(0,100).forEach(v => console.log(v, data[v])); + // console.log(this.RiTa.searchSync({ pos: 'v', limit: -1, minLength: -1 }).length, 'vs', this.allVerbs.length); + this.verbsEndingInE = this.allVerbs.filter(v => v.endsWith("e")); + this.verbsEndingInDouble = this.allVerbs.filter(v => /([^])\1$/.test(v)); } // TODO: add handling of past tense modals. diff --git a/src/lexicon.js b/src/lexicon.js index b34b577..87c0f7e 100644 --- a/src/lexicon.js +++ b/src/lexicon.js @@ -424,6 +424,7 @@ class Lexicon { if (tpos && tpos.length) { opts.pluralize = (tpos === "nns"); opts.conjugate = (tpos[0] === "v" && tpos.length > 2); + // simple pos conversion if (tpos[0] === "n") tpos = "nn"; else if (tpos[0] === "v") tpos = "vb"; else if (tpos === "r") tpos = "rb"; diff --git a/src/randgen.js b/src/randgen.js index 5af2ead..0bc9fb4 100644 --- a/src/randgen.js +++ b/src/randgen.js @@ -55,7 +55,7 @@ class SeededRandom { */ pselect(probs) { if (!probs || !probs.length) throw Error('arg required'); - let point = this._rndf(), cutoff = 0; + let point = Math.random(), cutoff = 0; for (let i = 0; i < probs.length - 1; ++i) { cutoff += probs[i]; if (point < cutoff) return i; @@ -75,12 +75,13 @@ class SeededRandom { } /* - Returns a normalised probability distribution (summing to 1) for arbitrary positive weights - If temperature is provided this is basically the softmax, otherwise it simple normalisation - Temperature parameter: range is between 0 and +Infinity (excluding both). - Lower values move the highest-weighted output toward a probability of 1.0. - Higher values tend to even out all the probabilities - */ + * Returns a normalised probability distribution (summing to 1) for arbitrary positive weights + * If temperature is provided this is basically the softmax, otherwise it is simple normalisation + * + * Temperature parameter: range is between 0 and +Infinity (excluding both). + * Lower values move the highest-weighted output toward a probability of 1.0. + * Higher values tend to even out all the probabilities + */ ndist(weights, temp) { let probs = [], sum = 0; if (!temp) { // no temp here diff --git a/src/rita_dict.js b/src/rita_dict.js index d3d5ebd..849fbed 100644 --- a/src/rita_dict.js +++ b/src/rita_dict.js @@ -1,4 +1,4 @@ - + /** @type {object} */ export default { "a": ["ey1", "dt"], @@ -9680,7 +9680,7 @@ export default { "imputation": ["ih-m p-y-ah t-ey1 sh-ah-n", "nn"], "impute": ["ih-m-p-y-uw1-t", "vb"], "imputed": ["ih-m p-y-uw1 t-ih-d", "vbn jj"], - "in": ["ih-n", "in nn rb rp rbr"], + "in": ["ih-n", "in rb rp rbr"], "inability": ["ih-n ah b-ih1 l-ih t-iy", "nn"], "inaccessible": ["ih-n ah-k s-eh1 s-ah b-ah-l", "jj"], "inaccuracy": ["ih-n ae1 k-y-er ah s-iy", "nn"], @@ -13378,7 +13378,7 @@ export default { "oust": ["aw1-s-t", "vb"], "ousted": ["aw1 s-t-ih-d", "vbn vbd jj"], "ouster": ["aw1 s-t-er", "nn"], - "out": ["aw1-t", "in jj nn rb rp"], + "out": ["aw1-t", "in jj rb rp"], "outage": ["aw1 t-ah-jh", "nn"], "outback": ["aw1-t b-ae-k", "nn"], "outbid": ["aw1-t b-ih-d", "vb vbd vbn vbp"], @@ -19905,9 +19905,8 @@ export default { "tourism": ["t-uh1 r-ih z-ah-m", "nn"], "tourist": ["t-uh1 r-ah-s-t", "nn"], "tournament": ["t-uh1-r n-ah m-ah-n-t", "nn"], - "tourney": ["t-er1 n-iy", "nn"], "tout": ["t-aw1-t", "vb nn"], - "tow": ["t-ow1", "nn vb"], + "tow": ["t-ow1", "vb"], "toward": ["t-ah w-ao1-r-d", "in"], "towards": ["t-ah w-ao1-r-d-z", "in"], "towel": ["t-aw1 ah-l", "nn"], @@ -19928,7 +19927,6 @@ export default { "trachea": ["t-r-ey1 k-iy ah", "nn"], "tracing": ["t-r-ey1 s-ih-ng", "vbg jj nn"], "track": ["t-r-ae1-k", "nn vbp vb"], - "trackage": ["t-r-ae1 k-ih-jh", "nn"], "tracked": ["t-r-ae1-k-t", "vbn vbd jj"], "tract": ["t-r-ae1-k-t", "nn"], "tractor": ["t-r-ae1-k t-er", "nn"], @@ -19948,7 +19946,7 @@ export default { "tragedy": ["t-r-ae1 jh-ah d-iy", "nn"], "tragic": ["t-r-ae1 jh-ih-k", "jj nn"], "tragically": ["t-r-ae1 jh-ih-k l-iy", "rb"], - "tragicomic": ["t-r-ae jh-ih k-aa1 m-ih-k", "jj nn"], + "tragicomic": ["t-r-ae jh-ih k-aa1 m-ih-k", "jj"], "trail": ["t-r-ey1-l", "nn vbp vb"], "trailer": ["t-r-ey1 l-er", "nn"], "train": ["t-r-ey1-n", "nn vb vbp"], @@ -20106,11 +20104,10 @@ export default { "trim": ["t-r-ih1-m", "vb jj nn"], "trimester": ["t-r-ay m-eh1 s-t-er", "nn"], "trimmed": ["t-r-ih1-m-d", "vbn rbr vbd jj"], - "trimmer": ["t-r-ih1 m-er", "jjr"], + "trimmer": ["t-r-ih1 m-er", "nn jjr"], "trinket": ["t-r-ih1-ng k-ah-t", "nn"], "trio": ["t-r-iy1 ow", "nn"], "trip": ["t-r-ih1-p", "nn vb"], - "tripartite": ["t-r-ay p-aa1-r t-ay-t", "jj"], "tripe": ["t-r-ay1-p", "nn"], "triple": ["t-r-ih1 p-ah-l", "jj nn rb vb"], "triplet": ["t-r-ih1 p-l-ah-t", "nn"], diff --git a/test/conjugator-tests.js b/test/conjugator-tests.js index d4912e9..398553b 100644 --- a/test/conjugator-tests.js +++ b/test/conjugator-tests.js @@ -1,11 +1,18 @@ import { expect } from 'chai'; import { RiTa } from './index.js'; +import Conjugator from '../src/conjugator.js'; let hasLex = true; describe('Conjugator', function () { + it('Should be constructed', function () { + let conjugator = new Conjugator(RiTa); + expect(conjugator).to.be.an('object'); + expect(conjugator.verbsEndingInE).to.be.an('array'); + }); + it('Should call pastPart', function () { expect(RiTa.pastPart("pen")).eq("penned"); // rita #150 @@ -528,6 +535,7 @@ describe('Conjugator', function () { if (!hasLex) this.skip(); + expect(RiTa.conjugator.unconjugate("accessing")).eq("access"); expect(RiTa.conjugator.unconjugate("trepanning")).eq("trepan"); // 3rd person singular (regular) diff --git a/test/lexicon-tests.js b/test/lexicon-tests.js index fd76355..8b8c92f 100644 --- a/test/lexicon-tests.js +++ b/test/lexicon-tests.js @@ -9,6 +9,11 @@ describe('Lexicon', function () { this.timeout(10000); it('Should call hasWord', function () { + + expect(RiTa.hasWord("accessing")).to.be.true; + expect(RiTa.hasWord("Accessing")).to.be.true; + expect(RiTa.hasWord("accessed")).to.be.true; + expect(RiTa.hasWord("Accessed")).to.be.true; for (let i = 0; i < 10; i++) { expect(RiTa.hasWord(RiTa.randomWord())).to.be.true;