diff --git a/CHANGELOG.md b/CHANGELOG.md index 81619b5d..68c472ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,9 +8,11 @@ The format of this changelog is based on [Keep a Changelog](https://keepachangel ### Added 0.4.1 -- Calculate number of Morph results that are different from last time. -- Correctly display differences in Morph results or not depending on the option selected. - Display any errors that are found in the Morph input. +- Calculate number of Morph results that are different from last time. +- Italicize those results changed by the rules while bolding those results different from the last run if that option is selected. +- Apply the rewrite rules to the input. +- Reverse-apply the rewrite rules to the results if selected. ## [v0.4.0](https://github.com/nai888/langua/compare/v0.3.1...v0.4.0) - 2018-12-30 diff --git a/src/routes/Morph/MorphService.js b/src/routes/Morph/MorphService.js index 8c27dc91..ae94d47c 100644 --- a/src/routes/Morph/MorphService.js +++ b/src/routes/Morph/MorphService.js @@ -15,8 +15,8 @@ class MorphService { 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.splitSoundChanges = this.splitSoundChanges.bind(this) this.rewriteLex = this.rewriteLex.bind(this) this.unrewriteLex = this.unrewriteLex.bind(this) this.applyChanges = this.applyChanges.bind(this) @@ -210,8 +210,14 @@ class MorphService { return errors.length ? errors : splitCategories } + // Apply the rewrite rules to the sound change rules + rewriteChanges (changes, rules) { + const newChanges = JSON.parse(JSON.stringify(changes)) + return newChanges + } + // Split the sound change rules into an array of objects - splitSoundChanges (changes) { + splitSoundChanges (changes, rules) { let splitChanges = [] let errors = [] @@ -270,14 +276,10 @@ class MorphService { } } - // If there are any errors that were logged, return the errors. Otherwise, return the split categories. - return errors.length ? errors : splitChanges - } + const rwChanges = this.rewriteChanges(splitChanges, rules) - // Apply the rewrite rules to the sound change rules - rewriteChanges (changes, rules) { - const newChanges = JSON.parse(JSON.stringify(changes)) - return newChanges + // If there are any errors that were logged, return the errors. Otherwise, return the split categories. + return errors.length ? errors : rwChanges } // Apply the rewrite rules to the lexicon @@ -298,10 +300,20 @@ class MorphService { return newLex } - // Reverse apply the rewrite rules to the output + // Reverse-apply the rewrite rules to the output unrewriteLex (results, rules) { const newResults = JSON.parse(JSON.stringify(results)) + for (let i = 0; i < newResults.length; i++) { + for (let j = 0; j < rules.length; j++) { + const regRule = new RegExp(rules[j].rewriteTo, 'g') + newResults[i].output = newResults[i].output.replace( + regRule, + rules[j].rewriteFrom + ) + } + } + return newResults } @@ -348,11 +360,10 @@ class MorphService { // 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) + const soundChanges = this.splitSoundChanges( + newData.soundChanges, + rewriteRules + ) // Return the errors if there are any let allErrors = []