Skip to content

Commit

Permalink
Reverse-apply the rewrite rules to the results
Browse files Browse the repository at this point in the history
  • Loading branch information
nai888 committed Dec 31, 2018
1 parent 05a6e78 commit c88fa06
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
41 changes: 26 additions & 15 deletions src/routes/Morph/MorphService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = []

Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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 = []
Expand Down

0 comments on commit c88fa06

Please sign in to comment.