Skip to content

Commit dceab1a

Browse files
authored
Merge pull request #826 from Speech-Rule-Engine/feature/link_counter
Feature/link counter
2 parents 90a6c1d + ca69e45 commit dceab1a

File tree

4 files changed

+69
-22
lines changed

4 files changed

+69
-22
lines changed

ts/common/cli.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ export class Cli {
314314
.option('-T, --speechStructure', 'Return speech structure only.', () =>
315315
processor('speechStructure')
316316
)
317+
.option('-W, --workerStructure', 'Return worker speech structure only.', () =>
318+
processor('workerSpeechStructure')
319+
)
317320
.option(
318321
'-t, --latex',
319322
'Accepts LaTeX input for certain locale/modality combinations.',

ts/common/engine.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export class SREError extends Error {
5757
*
5858
*/
5959
export class Engine {
60+
6061
public options: Options = new Options();
6162

6263
/**

ts/common/processor_factory.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,5 +426,67 @@ set(
426426
})
427427
);
428428

429+
430+
export type OptionsList = { [key: string]: string };
431+
type SpeechList = { [id: string]: { [mod: string]: string } };
432+
433+
export type WorkerStructure = {
434+
speech?: SpeechList;
435+
braille?: SpeechList;
436+
mactions?: SpeechList;
437+
options?: OptionsList;
438+
translations?: OptionsList;
439+
label?: string;
440+
postfix?: string;
441+
braillelabel?: string;
442+
ssml?: string;
443+
};
444+
445+
// The new speech structure for the webworker integration.
446+
// TODO: Cleanup and remove duplication with system.ts
447+
set(
448+
new Processor('workerSpeechStructure', {
449+
processor: function (expr) {
450+
const mml = DomUtil.parseInput(expr);
451+
let sxml;
452+
try {
453+
const rebuilt = new RebuildStree(mml);
454+
sxml = rebuilt.stree.xml();
455+
} catch(_e) {
456+
sxml = Semantic.xmlTree(mml, Engine.getInstance().options);
457+
}
458+
Engine.getInstance().options.automark = true;
459+
const json: WorkerStructure = {};
460+
assembleSpeechStructure(json, mml, sxml);
461+
return json;
462+
},
463+
print: function (descrs) {
464+
return JSON.stringify(descrs);
465+
},
466+
pprint: function (descrs) {
467+
return JSON.stringify(descrs, null, 2);
468+
}
469+
})
470+
);
471+
472+
473+
export function assembleSpeechStructure(
474+
json: WorkerStructure,
475+
mml: Element,
476+
sxml: Element,
477+
options: OptionsList = {}
478+
) {
479+
json.options = options;
480+
json.mactions = SpeechGeneratorUtil.connectMactionSelections(mml, sxml);
481+
json.speech = SpeechGeneratorUtil.computeSpeechStructure(sxml);
482+
const root = (sxml.childNodes[0] as Element)?.getAttribute('id');
483+
json.label = json.speech[root]['speech-none'] + '';
484+
json.ssml = json.speech[root]['speech-ssml'];
485+
json.translations = Object.assign({}, LOCALE.MESSAGES.navigate);
486+
const links = DomUtil.querySelectorAllByAttr(sxml, 'href').length;
487+
if (links) {
488+
json.label += `, ${links} ${(links === 1) ? 'link' : 'links'}`;
489+
}
490+
}
429491
// ./bin/sre -T -P -k ssml -d clearspeak < ../sre-resources/samples/quadratic-line.xml
430492
// echo "<math><mi>a</mi><mi>b</mi></math>" | ./bin/sre -T -P -d clearspeak

ts/common/system.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import * as EngineConst from './engine_const.js';
2626
import { KeyCode } from './event_util.js';
2727
import * as FileUtil from './file_util.js';
2828
import * as ProcessorFactory from './processor_factory.js';
29+
import { OptionsList, WorkerStructure } from './processor_factory.js';
2930
import { SystemExternal } from './system_external.js';
3031
import { Variables } from './variables.js';
3132
import { standardLoader } from '../speech_rules/math_map.js';
@@ -412,21 +413,6 @@ export function toSpeechStructure(expr: string): string {
412413
/**
413414
* Web worker related API methods.
414415
*/
415-
import { LOCALE } from '../l10n/locale.js';
416-
417-
type OptionsList = { [key: string]: string };
418-
type SpeechList = { [id: string]: { [mod: string]: string } };
419-
420-
type WorkerStructure = {
421-
speech?: SpeechList;
422-
braille?: SpeechList;
423-
mactions?: SpeechList;
424-
options?: OptionsList;
425-
translations?: OptionsList;
426-
label?: string;
427-
braillelabel?: string;
428-
ssml?: string;
429-
};
430416

431417
/**
432418
* Compute speech structure for the expression.
@@ -537,13 +523,7 @@ async function assembleWorkerStructure(
537523
await setupEngine(options);
538524
Engine.getInstance().options.automark = true;
539525
const json: WorkerStructure = {};
540-
json.options = options;
541-
json.mactions = SpeechGeneratorUtil.connectMactionSelections(mml, sxml);
542-
json.speech = SpeechGeneratorUtil.computeSpeechStructure(sxml);
543-
const root = (sxml.childNodes[0] as Element)?.getAttribute('id');
544-
json.label = json.speech[root]['speech-none'];
545-
json.ssml = json.speech[root]['speech-ssml'];
546-
json.translations = Object.assign({}, LOCALE.MESSAGES.navigate);
526+
ProcessorFactory.assembleSpeechStructure(json, mml, sxml, options);
547527
if (options.braille === 'none') {
548528
return json;
549529
}
@@ -553,6 +533,7 @@ async function assembleWorkerStructure(
553533
domain: 'default',
554534
style: 'default'
555535
});
536+
const root = (sxml.childNodes[0] as Element)?.getAttribute('id');
556537
json.braille = SpeechGeneratorUtil.computeBrailleStructure(sxml);
557538
json.braillelabel = json.braille[root]['braille-none'];
558539
return json;

0 commit comments

Comments
 (0)