Skip to content

Commit

Permalink
remove async call in Conjugator constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
dhowe committed Jun 9, 2024
1 parent 8cf863e commit b282512
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 22 deletions.
15 changes: 9 additions & 6 deletions src/conjugator.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { log } from "@tensorflow/tfjs";

/**
* @memberof module:rita
*/
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/lexicon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
15 changes: 8 additions & 7 deletions src/randgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
15 changes: 6 additions & 9 deletions src/rita_dict.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

/** @type {object} */
export default {
"a": ["ey1", "dt"],
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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"],
Expand All @@ -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"],
Expand All @@ -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"],
Expand Down Expand Up @@ -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"],
Expand Down
8 changes: 8 additions & 0 deletions test/conjugator-tests.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions test/lexicon-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b282512

Please sign in to comment.