Skip to content

Commit

Permalink
fixes to lexicon
Browse files Browse the repository at this point in the history
  • Loading branch information
dhowe committed Nov 16, 2023
1 parent 3bf3004 commit a0c8021
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 75 deletions.
23 changes: 13 additions & 10 deletions src/lexicon.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class Lexicon {
});
}

async rhymesSync(theWord, opts = {}) {
rhymesSync(theWord, opts = {}) {

this.parseArgs(opts);

Expand All @@ -165,8 +165,8 @@ class Lexicon {
// randomize list order if 'shuffle' is true
if (opts.shuffle) words = this.RiTa.randomizer.shuffle(words);

let result = [];
for (let i = 0; i < words.length; i++) {
let result = [];let i = 0
for (; i < words.length; i++) {

let word = words[i], data = dict[word];

Expand All @@ -187,11 +187,14 @@ class Lexicon {
let phones = data ? data[0] : this.rawPhones(word);

// check for the rhyme
if (phones.endsWith(phone)) result.push(word);
if (phones.endsWith(phone)) {
result.push(word);
}

if (result.length === opts.limit) break;
if (result.length === opts.limit) {
break;
}
}

return result;
}

Expand All @@ -209,7 +212,7 @@ class Lexicon {
: this.similarByType(word, opts));
}

async randomWord(regex, opts) {
randomWord(regex, opts) {

// no arguments, just return
if (!regex && !opts) {
Expand All @@ -230,12 +233,12 @@ class Lexicon {
opts.shuffle = true;
opts.limit = 1;

let result = await this.search(regex, opts);
let result = this.searchSync(regex, opts);

// relax our pos constraints if we got nothing
if (result.length < 1 && opts.hasOwnProperty('pos')) {
opts.strictPos = false;
result = await this.search(regex, opts);
result = this.searchSync(regex, opts);
}

// we've still got nothing, throw
Expand Down Expand Up @@ -449,7 +452,7 @@ class Lexicon {
opts.minDistance = opts.minDistance || 1;
opts.numSyllables = opts.numSyllables || 0;
opts.maxLength = opts.maxLength || Number.MAX_SAFE_INTEGER;
opts.minLength = opts.minLength || (opts.limit > 1 ? 3 : 4); // 4 for randomWord
opts.minLength = opts.minLength || 3;//(opts.limit > 1 ? 3 : 4); // 4 for randomWord

if (typeof opts.limit !== 'number' || opts.limit < 1) {
opts.limit = Number.MAX_SAFE_INTEGER;
Expand Down
20 changes: 18 additions & 2 deletions src/rita.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ class RiTa {
return string ? string[0].toUpperCase() + string.substring(1) : '';
}

static async randomWord(opts) {
return await RiTa.lexicon.randomWord(...arguments);
static randomWord(opts) {
return RiTa.lexicon.randomWord(...arguments);
}

static async rhymes(word, opts) {
Expand Down Expand Up @@ -215,6 +215,22 @@ class RiTa {

////////////////////////////// niapa /////////////////////////////

static rhymesSync(word, opts) {
return RiTa.lexicon.rhymesSync(...arguments);
}

static searchSync(word, opts) {
return RiTa.lexicon.rhymesSync(...arguments);
}

static similarByTypeSync(word, opts) {
return RiTa.lexicon.similarByTypeSync(...arguments);
}

static alliterationsSync(word, opts) {
return RiTa.lexicon.alliterationsSync(...arguments);
}

static randi(opts) {
return Math.floor(RiTa.randomizer.random(...arguments));
}
Expand Down
Loading

0 comments on commit a0c8021

Please sign in to comment.