add support for mulitgraph positive and negative phones
This commit is contained in:
parent
c8c8474157
commit
741af7f76e
2 changed files with 54 additions and 9 deletions
|
@ -5,17 +5,20 @@ const initState = () => {
|
|||
}
|
||||
|
||||
const addPhones = (phones, phone) => {
|
||||
let node = {}
|
||||
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;
|
||||
}
|
||||
|
||||
const addPositiveFeature = (phones, positivePhone, feature) => {
|
||||
const addFeatureToPhone = (phones, phone, featureKey, featureValue) => {
|
||||
let node = {}
|
||||
positivePhone.split('').forEach((graph, index) => {
|
||||
node = index === 0 ? node = phones[graph] : node = node[graph];
|
||||
if (index === positivePhone.split('').length - 1) node.features = {...node.features, [feature]: true}
|
||||
phone.split('').forEach((graph, index) => {
|
||||
node = index === 0 ? phones[graph] : node[graph];
|
||||
if (index === phone.split('').length - 1) node.features = {...node.features, [featureKey]: featureValue}
|
||||
})
|
||||
return phones;
|
||||
}
|
||||
|
@ -48,11 +51,12 @@ const stateReducer = (state, action) => {
|
|||
let newPhoneObject = [
|
||||
...positivePhones, ...negativePhones
|
||||
].reduce((phoneObject, phone) => addPhones(phoneObject, phone), state.phones)
|
||||
|
||||
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);
|
||||
|
||||
return {...state, features:[...state.features, newFeature], phones: newPhoneObject}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,10 @@ describe('Phones', () => {
|
|||
phones: { n: n_phone }
|
||||
};
|
||||
|
||||
beforeEach(()=> {
|
||||
state.features = [ 'nasal' ];
|
||||
state.phones= { n: n_phone };
|
||||
})
|
||||
|
||||
it('phones returned unaltered', () => {
|
||||
const action = {type: ''};
|
||||
|
@ -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}}}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue