add support for mulitgraph positive and negative phones

This commit is contained in:
Sorrel Bri 2019-12-04 19:33:17 -08:00
parent c8c8474157
commit 741af7f76e
2 changed files with 54 additions and 9 deletions

View file

@ -5,17 +5,20 @@ const initState = () => {
} }
const addPhones = (phones, phone) => { const addPhones = (phones, phone) => {
let node = {}
phone.split('').forEach((graph, index) => { phone.split('').forEach((graph, index) => {
if (!phones[graph]) phones[graph] = {} if (index) node[graph] = {}
if (!index && !phones[graph]) phones[graph] = {}
node = index === 0 ? phones[graph] : node[graph];
}) })
return phones; return phones;
} }
const addPositiveFeature = (phones, positivePhone, feature) => { const addFeatureToPhone = (phones, phone, featureKey, featureValue) => {
let node = {} let node = {}
positivePhone.split('').forEach((graph, index) => { phone.split('').forEach((graph, index) => {
node = index === 0 ? node = phones[graph] : node = node[graph]; node = index === 0 ? phones[graph] : node[graph];
if (index === positivePhone.split('').length - 1) node.features = {...node.features, [feature]: true} if (index === phone.split('').length - 1) node.features = {...node.features, [featureKey]: featureValue}
}) })
return phones; return phones;
} }
@ -48,11 +51,12 @@ const stateReducer = (state, action) => {
let newPhoneObject = [ let newPhoneObject = [
...positivePhones, ...negativePhones ...positivePhones, ...negativePhones
].reduce((phoneObject, phone) => addPhones(phoneObject, phone), state.phones) ].reduce((phoneObject, phone) => addPhones(phoneObject, phone), state.phones)
if (positivePhones) positivePhones = positivePhones.reduce( if (positivePhones) positivePhones = positivePhones.reduce(
(phoneObject, positivePhone) => addPositiveFeature(phoneObject, positivePhone, newFeature) (phoneObject, positivePhone) => addFeatureToPhone(phoneObject, positivePhone, newFeature, true)
, newPhoneObject);
if (negativePhones) negativePhones = negativePhones.reduce(
(phoneObject, positivePhone) => addFeatureToPhone(phoneObject, positivePhone, newFeature, false)
, newPhoneObject); , newPhoneObject);
return {...state, features:[...state.features, newFeature], phones: newPhoneObject} return {...state, features:[...state.features, newFeature], phones: newPhoneObject}
} }

View file

@ -9,7 +9,11 @@ describe('Phones', () => {
phones: { n: n_phone } phones: { n: n_phone }
}; };
beforeEach(()=> {
state.features = [ 'nasal' ];
state.phones= { n: n_phone };
})
it('phones returned unaltered', () => { it('phones returned unaltered', () => {
const action = {type: ''}; const action = {type: ''};
expect(stateReducer(state, action)).toBe(state); expect(stateReducer(state, action)).toBe(state);
@ -25,4 +29,41 @@ describe('Phones', () => {
) )
}) })
it('feature addition returns new feature list with negative phones update', () => {
const action = {type: 'ADD_FEATURE', value: {feature: 'sonorant', negativePhones: ['t']}};
expect(stateReducer(state, action)).toEqual(
{...state,
features:[...state.features, action.value.feature],
phones:{...state.phones, t:{features:{sonorant: false}}}
}
);
});
it('feature addition returns new feature list with positive and negative phones update', () => {
const action = {type: 'ADD_FEATURE', value: {feature: 'sonorant', positivePhones: ['n'], negativePhones: ['t']}};
expect(stateReducer(state, action)).toEqual(
{...state,
features:[...state.features, action.value.feature],
phones:{...state.phones,
t:{features:{sonorant: false}},
n:{...state.phones.n, features: {...state.phones.n.features, sonorant: true}}
}
}
);
});
it('feature addition returns new feature list with multi-graph phones updated', () => {
const action = {type: 'ADD_FEATURE', value: {feature: 'aspirated', positivePhones: ['ntʰ'], negativePhones: ['n','t']}};
expect(stateReducer(state, action)).toEqual(
{...state,
features:[...state.features, action.value.feature],
phones:{...state.phones,
t:{features:{aspirated: false}},
n:{...state.phones.n, features: {...state.phones.n.features, aspirated: false},
t: {ʰ:{features:{aspirated:true}}}
}
}
}
);
});
}); });