UNPKG

speech-rule-engine

Version:

A standalone speech rule engine for XML structures, based on the original engine from ChromeVox.

122 lines (121 loc) 3.29 kB
import { DynamicCstr, DynamicProperties } from '../rule_engine/dynamic_cstr.js'; export const PREFERENCES = new DynamicProperties({ AbsoluteValue: ['Auto', 'AbsEnd', 'Cardinality', 'Determinant'], Bar: ['Auto', 'Conjugate'], Caps: ['Auto', 'SayCaps'], CombinationPermutation: ['Auto', 'ChoosePermute'], Currency: ['Auto', 'Position', 'Prefix'], Ellipses: ['Auto', 'AndSoOn'], Enclosed: ['Auto', 'EndEnclose'], Exponent: [ 'Auto', 'AfterPower', 'Ordinal', 'OrdinalPower', 'Exponent' ], Fraction: [ 'Auto', 'EndFrac', 'FracOver', 'General', 'GeneralEndFrac', 'Ordinal', 'Over', 'OverEndFrac', 'Per' ], Functions: [ 'Auto', 'None', 'Reciprocal' ], ImpliedTimes: ['Auto', 'MoreImpliedTimes', 'None'], Log: ['Auto', 'LnAsNaturalLog'], Matrix: [ 'Auto', 'Combinatoric', 'EndMatrix', 'EndVector', 'SilentColNum', 'SpeakColNum', 'Vector' ], MultiLineLabel: [ 'Auto', 'Case', 'Constraint', 'Equation', 'Line', 'None', 'Row', 'Step' ], MultiLineOverview: ['Auto', 'None'], MultiLinePausesBetweenColumns: ['Auto', 'Long', 'Short'], MultsymbolDot: ['Auto', 'Dot'], MultsymbolX: ['Auto', 'By', 'Cross'], Paren: [ 'Auto', 'CoordPoint', 'Interval', 'Silent', 'Speak', 'SpeakNestingLevel' ], Prime: ['Auto', 'Angle', 'Length'], Roots: ['Auto', 'PosNegSqRoot', 'PosNegSqRootEnd', 'RootEnd'], SetMemberSymbol: ['Auto', 'Belongs', 'Element', 'Member', 'In'], Sets: ['Auto', 'SilentBracket', 'woAll'], TriangleSymbol: ['Auto', 'Delta'], Trig: [ 'Auto', 'ArcTrig', 'TrigInverse', 'Reciprocal' ], VerticalLine: ['Auto', 'Divides', 'Given', 'SuchThat'] }); const AUTO = 'Auto'; export function fromPreference(pref) { const pairs = pref.split(':'); const preferences = {}; const properties = PREFERENCES.getProperties(); const validKeys = Object.keys(properties); for (let i = 0, key; (key = pairs[i]); i++) { const pair = key.split('_'); if (validKeys.indexOf(pair[0]) === -1) { continue; } const value = pair[1]; if (value && value !== AUTO && properties[pair[0]].indexOf(value) !== -1) { preferences[pair[0]] = pair[1]; } } return preferences; } export function toPreference(pref) { const keys = Object.keys(pref); const str = []; for (let i = 0; i < keys.length; i++) { str.push(keys[i] + '_' + pref[keys[i]]); } return str.length ? str.join(':') : DynamicCstr.DEFAULT_VALUE; } export function findPreference(prefs, kind) { if (prefs === 'default') { return AUTO; } const parsed = fromPreference(prefs); return parsed[kind] || AUTO; } export function addPreference(prefs, kind, value) { if (prefs === 'default') { return kind + '_' + value; } const parsed = fromPreference(prefs); parsed[kind] = value; return toPreference(parsed); }