Skip to content

Commit

Permalink
optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
dhowe committed Aug 9, 2020
1 parent b545dfb commit 1862fd3
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 111 deletions.
1 change: 0 additions & 1 deletion src/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class Analyzer {

// if no phones yet, try the lts-engine
if (!rawPhones) {

let ltsPhones = RiTa.lts && RiTa.lts.computePhones(word);
if (ltsPhones && ltsPhones.length > 0) {
if (!RiTa.SILENT && !RiTa.SILENCE_LTS && !silentLts
Expand Down
40 changes: 24 additions & 16 deletions src/lexicon.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class Lexicon {
return result;
}

opts._analyzer = RiTa._analyzer();
let _silent = opts.silent;
opts.silent = true;
for (let i = 0; i < words.length; i++) {
let word = words[i];
// check word length and syllables
Expand All @@ -53,7 +56,7 @@ class Lexicon {
if (phone === c2) result.push(word);
if (result.length === opts.limit) break;
}

opts.silent = _silent;
return result;
}

Expand All @@ -64,6 +67,10 @@ class Lexicon {
const dict = this._dict(true), words = Object.keys(dict);
const phone = this._lastStressedPhoneToEnd(theWord);
if (!phone) return [];

opts._analyzer = RiTa._analyzer();
let _silent = opts.silent;
opts.silent = true;
let result = [];
for (let i = 0; i < words.length; i++) {
let word = words[i];
Expand All @@ -79,7 +86,7 @@ class Lexicon {
if (dict[word][0].endsWith(phone)) result.push(word);
if (result.length === opts.limit) break;
}

opts.silent = _silent;
return result;
}

Expand All @@ -91,7 +98,9 @@ class Lexicon {
const dict = this._dict(true);
let words = Object.keys(dict);
const ran = Math.floor(RiTa.randInt(words.length));

opts._analyzer = RiTa._analyzer();
let _silent = opts.silent;
opts.silent = true;
for (let k = 0; k < words.length; k++) {
let j = (ran + k) % words.length;
let word = words[j], rdata = dict[word];
Expand All @@ -102,7 +111,7 @@ class Lexicon {
let result = this.matchPos(word, rdata, opts, true);
if (result) return result;
}

opts.silent = _silent;
throw Error('No random word with options: ' + JSON.stringify(opts));
}

Expand Down Expand Up @@ -137,33 +146,33 @@ class Lexicon {
}

this.parseArgs(opts);
let analyzer = /(stresses|phones)/.test(opts.type) ? RiTa._analyzer() : 0;

opts._analyzer = RiTa._analyzer();
let _silent = opts.silent;
opts.silent = true;

let result = [];
let tmp = RiTa.SILENCE_LTS;
RiTa.SILENCE_LTS = true;
for (let i = 0; i < words.length; i++) {
let word = words[i];
if (!this.checkCriteria(word, dict[word], opts)) continue;
if (opts.targetPos) {
word = this.matchPos(word, dict[word], opts);
if (!word) continue;
}
if (opts.type === 'stresses') {
let stresses = analyzer.analyzeWord(word).stresses;
if (opts.type === 'stresses') { // slow
let stresses = opts._analyzer.analyzeWord(word, opts).stresses;
if (regex.test(stresses)) result.push(word);
}
else if (opts.type === 'phones') {
let phones = analyzer.analyzeWord(word).phones;
let phones = opts._analyzer.analyzeWord(word, opts).phones;
if (regex.test(phones)) result.push(word);
}
else {
if (regex.test(word)) result.push(word);
}
if (result.length === opts.limit) break;
}
RiTa.SILENCE_LTS = tmp;

opts.silent = _silent;
return result;
}

Expand Down Expand Up @@ -240,6 +249,7 @@ class Lexicon {

matchPos(word, rdata, opts, strict) {
let posArr = rdata[1].split(' ');
let analyzer = opts._analyzer;

// check the pos here (based on strict flag)
if ((strict && opts.targetPos !== posArr[0]) ||
Expand All @@ -257,10 +267,8 @@ class Lexicon {

// verify we haven't changed syllable count
if (result !== word && opts.numSyllables) {
let tmp = RiTa.SILENCE_LTS;
RiTa.SILENCE_LTS = true;
let num = RiTa.syllables(result).split(RiTa.SYLLABLE_BOUNDARY).length;
RiTa.SILENCE_LTS = tmp;
let syls = analyzer.analyzeWord(result).syllables;
let num = syls.split(RiTa.SYLLABLE_BOUNDARY).length;
// reject if syllable count has changed
if (num !== opts.numSyllables) return;
}
Expand Down
48 changes: 24 additions & 24 deletions src/rita.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class RiTa {
return RiTa.lexicon().alliterations(...arguments);
}

static analyze(text) {
return RiTa._analyzer().analyze(text);
static analyze() {
return RiTa._analyzer().analyze(...arguments);
}

static concordance() { // DOC:
Expand Down Expand Up @@ -91,8 +91,8 @@ class RiTa {
return RiTa.tagger.isAdverb(word);
}

static isAlliteration(word1, word2) {
return RiTa.lexicon().isAlliteration(word1, word2);
static isAlliteration() {
return RiTa.lexicon().isAlliteration(...arguments);
}

static isNoun(word) {
Expand All @@ -108,8 +108,8 @@ class RiTa {
(RiTa.tokenize(sentence)[0].toLowerCase());
}

static isRhyme(word1, word2) {
return RiTa.lexicon().isRhyme(word1, word2);
static isRhyme() {
return RiTa.lexicon().isRhyme(...arguments);
}

static isVerb(word) {
Expand All @@ -120,8 +120,8 @@ class RiTa {
return RiTa.concorder.kwic(...arguments);
}

static pastParticiple(verb) {
return RiTa.conjugator.pastParticiple(verb);
static pastParticiple() {
return RiTa.conjugator.pastParticiple(...arguments);
}

static phones() {
Expand All @@ -141,8 +141,8 @@ class RiTa {
return RiTa.inflector.pluralize(...arguments);
}

static presentParticiple(verb) {
return RiTa.conjugator.presentParticiple(verb);
static presentParticiple() {
return RiTa.conjugator.presentParticiple(...arguments);
}

static random() {
Expand All @@ -153,12 +153,12 @@ class RiTa {
return Math.floor(RiTa.random(...arguments));
}

static randomOrdering(num) {
return RiTa.randomizer.randomOrdering(num);
static randomOrdering() {
return RiTa.randomizer.randomOrdering(...arguments);
}

static randomSeed(theSeed) {
return RiTa.randomizer.seed(theSeed);
static randomSeed() {
return RiTa.randomizer.seed(...arguments);
}

static randomWord() {
Expand All @@ -177,40 +177,40 @@ class RiTa {
return RiTa.lexicon().search(...arguments);
}

static sentences(text) {
return RiTa.tokenizer.sentences(text);
static sentences() {
return RiTa.tokenizer.sentences(...arguments);
}

static spellsLike() { // DOC:
return RiTa.lexicon().spellsLike(...arguments);
}

static singularize(word) {
static singularize() {
return RiTa.inflector.singularize(...arguments);
}

static soundsLike() { // DOC:
return RiTa.lexicon().soundsLike(...arguments);
}

static stem(word) {
return RiTa.stemmer.stem(word);
static stem() {
return RiTa.stemmer.stem(...arguments);
}

static stresses() {
return RiTa._analyzer().analyze(...arguments).stresses;
}

static syllables(text) {
static syllables() {
return RiTa._analyzer().analyze(...arguments).syllables;
}

static tokenize(text) {
return RiTa.tokenizer.tokenize(text);
static tokenize() {
return RiTa.tokenizer.tokenize(...arguments);
}

static untokenize(words) {
return RiTa.tokenizer.untokenize(words);
static untokenize() {
return RiTa.tokenizer.untokenize(...arguments);
}

/////////////////////////////////////////////////////////////////
Expand Down
13 changes: 4 additions & 9 deletions test/analyzer-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,17 @@ describe('RiTa.Analyzer', () => {
if (typeof module !== 'undefined') require('./before');

it('Should correctly call analyze.lts', () => {
let silent = RiTa.SILENCE_LTS;
//RiTa.SILENCE_LTS = true;
let feats;
feats = RiTa.analyze("cloze");
feats = RiTa.analyze("cloze", { silent: 1 });
expect(feats.pos).eq("nn");
expect(feats.tokens).eq("cloze");
expect(feats.syllables).eq("k-l-ow-z");
RiTa.SILENCE_LTS = silent;
//RiTa.SILENCE_LTS = silent;
});

it('Should correctly call syllables.lts', () => {
let silent = RiTa.SILENCE_LTS;
RiTa.SILENCE_LTS = true;
let result = RiTa.syllables('The Laggin');
let result = RiTa.syllables('The Laggin', {silent:1});
expect(result).eq('dh-ah l-ae/g-ih-n', 'got \'' + result + "'");
RiTa.SILENCE_LTS = silent;
});

it('Should correctly call analyze', () => {
Expand All @@ -43,7 +38,7 @@ describe('RiTa.Analyzer', () => {
expect(feats.tokens).eq("chevrolet");
expect(feats.syllables).eq(hasLex ? "sh-eh-v/r-ow/l-ey" : 'ch-eh-v/r-ow/l-ah-t');
});

it('Should correctly call stresses', () => {

let result, answer, word;
Expand Down
Loading

0 comments on commit 1862fd3

Please sign in to comment.