From ec7daa16ffc9c1044cffcbba1e62f06a3c34ec18 Mon Sep 17 00:00:00 2001 From: "Ian A. Cook" <6866411+nai888@users.noreply.github.com> Date: Mon, 31 Dec 2018 11:53:26 -0600 Subject: [PATCH] Apply rewrite rules to the lexicon --- src/routes/Morph/MorphService.js | 136 +++++++++++++++++++++---------- 1 file changed, 93 insertions(+), 43 deletions(-) diff --git a/src/routes/Morph/MorphService.js b/src/routes/Morph/MorphService.js index 85de99e6..9014736c 100644 --- a/src/routes/Morph/MorphService.js +++ b/src/routes/Morph/MorphService.js @@ -12,9 +12,13 @@ class MorphService { this.setStorage = this.setStorage.bind(this) this.save = this.save.bind(this) this.open = this.open.bind(this) - this.splitCategories = this.splitCategories.bind(this) this.splitRewriteRules = this.splitRewriteRules.bind(this) + this.rewriteCats = this.rewriteCats.bind(this) + this.splitCategories = this.splitCategories.bind(this) this.splitSoundChanges = this.splitSoundChanges.bind(this) + this.rewriteChanges = this.rewriteChanges.bind(this) + this.rewriteLex = this.rewriteLex.bind(this) + this.unrewriteLex = this.unrewriteLex.bind(this) this.applyChanges = this.applyChanges.bind(this) this.idChanged = this.idChanged.bind(this) this.morph = this.morph.bind(this) @@ -125,8 +129,45 @@ class MorphService { } } + // Split the rewrite rules into an array of objects + splitRewriteRules (rules) { + let splitRules = [] + let errors = [] + + for (let i = 0; i < rules.length; i++) { + const split = rules[i].split('=') + if (split.length > 2) { + // If the string was split too many times, it had too many = + errors.push(`The rewrite rule ${rules[i]} has too many = signs.`) + } else if (split.length < 2) { + if (rules[i].length === 0) { + // If it was a blank line, ignore it + continue + } else { + // If the string wasn't split at all, it was missing a = + errors.push(`The rewrite rule ${rules[i]} is missing an = sign.`) + } + } else { + // Split the rewrite rule + splitRules.push({ rewriteFrom: split[0], rewriteTo: split[1] }) + } + } + + // If there are any errors that were logged, return the errors. Otherwise, return the split categories. + return errors.length ? errors : splitRules + } + + // Apply the rewrite rules to the categories + rewriteCats (cat, rules) { + for (let i = 0; i < rules.length; i++) { + const rewriteFrom = new RegExp(rules[i].rewriteFrom, 'g') + cat[1] = cat[1].replace(rewriteFrom, rules[i].rewriteTo) + } + return cat + } + // Split the categories into an array of objects - splitCategories (cats) { + splitCategories (cats, rules) { let assignments = [] let errors = [] @@ -151,8 +192,9 @@ class MorphService { } needs to be assigned to a single-character variable.` ) } else { + const rwSplit = this.rewriteCats(split, rules) // Split the variable from its assignments - assignments.push(split) + assignments.push(rwSplit) } } @@ -168,34 +210,6 @@ class MorphService { return errors.length ? errors : splitCategories } - // Split the rewrite rules into an array of objects - splitRewriteRules (rules) { - let splitRules = [] - let errors = [] - - for (let i = 0; i < rules.length; i++) { - const split = rules[i].split('=') - if (split.length > 2) { - // If the string was split too many times, it had too many = - errors.push(`The rewrite rule ${rules[i]} has too many = signs.`) - } else if (split.length < 2) { - if (rules[i].length === 0) { - // If it was a blank line, ignore it - continue - } else { - // If the string wasn't split at all, it was missing a = - errors.push(`The rewrite rule ${rules[i]} is missing an = sign.`) - } - } else { - // Split the rewrite rule - splitRules.push({ rewriteFrom: split[0], rewriteTo: split[1] }) - } - } - - // If there are any errors that were logged, return the errors. Otherwise, return the split categories. - return errors.length ? errors : splitRules - } - // Split the sound change rules into an array of objects splitSoundChanges (changes) { let splitChanges = [] @@ -260,19 +274,50 @@ class MorphService { return errors.length ? errors : splitChanges } - applyChanges (cats, rules, changes, lexicon, rwOutput) { - let results = [ - { - input: 'lector', - output: 'leitor' - }, - { - input: 'doctor', - output: 'doutor' + // Apply the rewrite rules to the sound change rules + rewriteChanges (changes, rules) { + const newChanges = JSON.parse(JSON.stringify(changes)) + return newChanges + } + + // Apply the rewrite rules to the lexicon + rewriteLex (lex, rules) { + let newLex = [] + + for (let i = 0; i < lex.length; i++) { + let word = lex[i] + + for (let j = 0; j < rules.length; j++) { + const regRule = new RegExp(rules[j].rewriteFrom, 'g') + word = word.replace(regRule, rules[j].rewriteTo) } - ] - return results + newLex.push(word) + } + + return newLex + } + + // Reverse apply the rewrite rules to the output + unrewriteLex (results, rules) { + const newResults = JSON.parse(JSON.stringify(results)) + + return newResults + } + + applyChanges (cats, rules, changes, lexicon, rwOutput) { + const rwLexicon = this.rewriteLex(lexicon, rules) + let results = [] + + for (let i = 0; i < lexicon.length; i++) { + results.push({ + input: lexicon[i], + output: rwLexicon[i] + }) + } + + // If 'rewrite on output' is selected, rewrite the results. Otherwise, just return the results as-is. + return rwOutput ? this.unrewriteLex(results, rules) : results } idChanged (data, results) { @@ -300,10 +345,15 @@ class MorphService { morph (data) { const newData = JSON.parse(JSON.stringify(data)) - const categories = this.splitCategories(newData.categories) + // Process the input const rewriteRules = this.splitRewriteRules(newData.rewriteRules) + const categories = this.splitCategories(newData.categories, rewriteRules) const soundChanges = this.splitSoundChanges(newData.soundChanges) + // Apply the rewrite rules + const rwChanges = this.rewriteChanges(soundChanges, rewriteRules) + console.log(rwChanges) + // Return the errors if there are any let allErrors = [] if (typeof categories[0] === 'string')