Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ts/common/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ export class Cli {
.option('-T, --speechStructure', 'Return speech structure only.', () =>
processor('speechStructure')
)
.option('-W, --workerStructure', 'Return worker speech structure only.', () =>
processor('workerSpeechStructure')
)
.option(
'-t, --latex',
'Accepts LaTeX input for certain locale/modality combinations.',
Expand Down
1 change: 1 addition & 0 deletions ts/common/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class SREError extends Error {
*
*/
export class Engine {

public options: Options = new Options();

/**
Expand Down
62 changes: 62 additions & 0 deletions ts/common/processor_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,5 +426,67 @@ set(
})
);


export type OptionsList = { [key: string]: string };
type SpeechList = { [id: string]: { [mod: string]: string } };

export type WorkerStructure = {
speech?: SpeechList;
braille?: SpeechList;
mactions?: SpeechList;
options?: OptionsList;
translations?: OptionsList;
label?: string;
postfix?: string;
braillelabel?: string;
ssml?: string;
};

// The new speech structure for the webworker integration.
// TODO: Cleanup and remove duplication with system.ts
set(
new Processor('workerSpeechStructure', {
processor: function (expr) {
const mml = DomUtil.parseInput(expr);
let sxml;
try {
const rebuilt = new RebuildStree(mml);
sxml = rebuilt.stree.xml();
} catch(_e) {
sxml = Semantic.xmlTree(mml, Engine.getInstance().options);
}
Engine.getInstance().options.automark = true;
const json: WorkerStructure = {};
assembleSpeechStructure(json, mml, sxml);
return json;
},
print: function (descrs) {
return JSON.stringify(descrs);
},
pprint: function (descrs) {
return JSON.stringify(descrs, null, 2);
}
})
);


export function assembleSpeechStructure(
json: WorkerStructure,
mml: Element,
sxml: Element,
options: OptionsList = {}
) {
json.options = options;
json.mactions = SpeechGeneratorUtil.connectMactionSelections(mml, sxml);
json.speech = SpeechGeneratorUtil.computeSpeechStructure(sxml);
const root = (sxml.childNodes[0] as Element)?.getAttribute('id');
json.label = json.speech[root]['speech-none'] + '';
json.ssml = json.speech[root]['speech-ssml'];
json.translations = Object.assign({}, LOCALE.MESSAGES.navigate);
const links = DomUtil.querySelectorAllByAttr(sxml, 'href').length;
if (links) {
json.label += `, ${links} ${(links === 1) ? 'link' : 'links'}`;
}
}
// ./bin/sre -T -P -k ssml -d clearspeak < ../sre-resources/samples/quadratic-line.xml
// echo "<math><mi>a</mi><mi>b</mi></math>" | ./bin/sre -T -P -d clearspeak
25 changes: 3 additions & 22 deletions ts/common/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import * as EngineConst from './engine_const.js';
import { KeyCode } from './event_util.js';
import * as FileUtil from './file_util.js';
import * as ProcessorFactory from './processor_factory.js';
import { OptionsList, WorkerStructure } from './processor_factory.js';
import { SystemExternal } from './system_external.js';
import { Variables } from './variables.js';
import { standardLoader } from '../speech_rules/math_map.js';
Expand Down Expand Up @@ -412,21 +413,6 @@ export function toSpeechStructure(expr: string): string {
/**
* Web worker related API methods.
*/
import { LOCALE } from '../l10n/locale.js';

type OptionsList = { [key: string]: string };
type SpeechList = { [id: string]: { [mod: string]: string } };

type WorkerStructure = {
speech?: SpeechList;
braille?: SpeechList;
mactions?: SpeechList;
options?: OptionsList;
translations?: OptionsList;
label?: string;
braillelabel?: string;
ssml?: string;
};

/**
* Compute speech structure for the expression.
Expand Down Expand Up @@ -537,13 +523,7 @@ async function assembleWorkerStructure(
await setupEngine(options);
Engine.getInstance().options.automark = true;
const json: WorkerStructure = {};
json.options = options;
json.mactions = SpeechGeneratorUtil.connectMactionSelections(mml, sxml);
json.speech = SpeechGeneratorUtil.computeSpeechStructure(sxml);
const root = (sxml.childNodes[0] as Element)?.getAttribute('id');
json.label = json.speech[root]['speech-none'];
json.ssml = json.speech[root]['speech-ssml'];
json.translations = Object.assign({}, LOCALE.MESSAGES.navigate);
ProcessorFactory.assembleSpeechStructure(json, mml, sxml, options);
if (options.braille === 'none') {
return json;
}
Expand All @@ -553,6 +533,7 @@ async function assembleWorkerStructure(
domain: 'default',
style: 'default'
});
const root = (sxml.childNodes[0] as Element)?.getAttribute('id');
json.braille = SpeechGeneratorUtil.computeBrailleStructure(sxml);
json.braillelabel = json.braille[root]['braille-none'];
return json;
Expand Down
Loading