refactor results reducer for cleanliness

This commit is contained in:
Sorrel Bri 2020-02-21 16:00:34 -08:00
parent 31fab0ea57
commit eb551a3fce

View file

@ -25,6 +25,8 @@ type ruleBundle = {
newFeatures: string newFeatures: string
} }
const getProperty = property => object => object[property]
const findFeaturesFromLexeme = (phones: {}, lexeme:string): [] => { const findFeaturesFromLexeme = (phones: {}, lexeme:string): [] => {
let featureBundle = [] let featureBundle = []
let lastIndex = lexeme.length - 1; let lastIndex = lexeme.length - 1;
@ -185,19 +187,20 @@ const isPhonemeBoundByRule = phonemeFeatures => (ruleFeature, index) => {
const isEnvironmentBoundByRule = (phonemeFeatures, ruleFeatures) => { const isEnvironmentBoundByRule = (phonemeFeatures, ruleFeatures) => {
if (!ruleFeatures) return true; if (!ruleFeatures) return true;
return ruleFeatures.filter(isPhonemeBoundByRule(phonemeFeatures)).length === ruleFeatures.length return ruleFeatures.filter(isPhonemeBoundByRule(phonemeFeatures)).length === ruleFeatures.length;
? true : false;
} }
const getEntries = object => Object.entries(object);
const isObjectWithPropertyInArray = (array, property) => candidate => array.map(getProperty(property)).includes(candidate[property]);
const transformFeatureValues = features => ([newFeature, newValue]) => features[newFeature][newValue ? 'positive': 'negative'];
const reduceFeatureValues = (newPhoneme, [newFeature, newValue]) => ({ ...newPhoneme, [newFeature]: newValue })
const transformPhoneme = (phoneme, newFeatures, features) => { const transformPhoneme = (phoneme, newFeatures, features) => {
if (!newFeatures) return {} if (!newFeatures) return {}
const newPhonemeFeatures = Object.entries(newFeatures) const newPhonemeFeatures = getEntries(newFeatures).reduce(reduceFeatureValues, {...phoneme.features});
.reduce((newPhoneme, [newFeature, newValue]) => ({ ...newPhoneme, [newFeature]: newValue }) const newPhonemeCandidates = getEntries(newPhonemeFeatures).map(transformFeatureValues(features));
, {...phoneme.features});
const newPhonemeCandidates = Object.entries(newPhonemeFeatures)
.map(([newFeature, newValue]) => features[newFeature][newValue ? 'positive': 'negative']);
return newPhonemeCandidates return newPhonemeCandidates
.reduce((candidates, value, index, array) => candidates.filter(candidate => value.map(val => val.grapheme).includes(candidate.grapheme)) .reduce((candidates, candidatesSubset, index, array) => candidates.filter(isObjectWithPropertyInArray(candidatesSubset, 'grapheme'))
, newPhonemeCandidates[newPhonemeCandidates.length - 1])[0]; , newPhonemeCandidates[newPhonemeCandidates.length - 1])[0];
} }
@ -249,33 +252,32 @@ const transformLexicon = lexiconBundle =>
)) ))
const getGraphemeFromEntry = ([_, phoneme]) => phoneme.grapheme const getGraphemeFromEntry = ([_, phoneme]) => phoneme.grapheme
const stringifyResults = passResults => { const stringifyLexeme = (lexeme) => lexeme.map(getProperty('grapheme')).join('')
const lexicon = passResults.lexicon.map(lexeme => lexeme.map(phoneme => phoneme.grapheme).join('')) const stringifyResults = ({lexicon, ...passResults}) => ({...passResults, lexicon: lexicon.map(stringifyLexeme)})
return {...passResults, lexicon }
}
export const run = (state: stateType, action: resultsAction): stateType => { export const run = (state: stateType, action: resultsAction): stateType => {
// TODO iterate through each epoch // TODO iterate through each epoch
try { try {
const passResults = state.epochs.reduce((results, epoch, _) => { const passResults = state.epochs.reduce((results, epoch, _) => {
const { phones, features, lexicon } = state;
const { phones, features } = state; let lexiconBundle;
const lexicon = epoch.parent ? results.find(result => result.pass === epoch.parent).lexicon : state.lexicon if ( epoch.parent ) {
const ruleBundle = decomposeRules(epoch, phones); lexiconBundle = results.find(result => result.pass === epoch.parent).lexicon
const lexiconBundle = epoch.parent ? lexicon : formBundleFromLexicon(lexicon)(phones);
const passResults = transformLexicon(lexiconBundle)(ruleBundle)(features)
const pass = {
pass: epoch.name,
lexicon: passResults
} }
if (!epoch.parent) {
lexiconBundle = formBundleFromLexicon(lexicon)(phones);
}
const ruleBundle = decomposeRules(epoch, phones);
const passResults = transformLexicon(lexiconBundle)(ruleBundle)(features)
const pass = { pass: epoch.name, lexicon: passResults }
if ( epoch.parent ) pass.parent = epoch.parent; if ( epoch.parent ) pass.parent = epoch.parent;
return [...results, pass]; return [...results, pass];
}, []); }, []);
const results = passResults.map(stringifyResults); const results = passResults.map(stringifyResults);
return {...state, results } return {...state, results }
} catch (err) { } catch (err) {
console.log(err) return {...state, errors: err };
return err;
} }
} }