Skip to content

Commit

Permalink
Apply rewrite rules to the lexicon
Browse files Browse the repository at this point in the history
  • Loading branch information
nai888 committed Dec 31, 2018
1 parent e50d4a9 commit ec7daa1
Showing 1 changed file with 93 additions and 43 deletions.
136 changes: 93 additions & 43 deletions src/routes/Morph/MorphService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = []

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

Expand All @@ -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 = []
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit ec7daa1

Please sign in to comment.