system.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  2. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. import { Engine, EnginePromise, SREError } from './engine.js';
  11. import { setup } from './engine_setup.js';
  12. import * as EngineConst from './engine_const.js';
  13. import * as FileUtil from './file_util.js';
  14. import * as ProcessorFactory from './processor_factory.js';
  15. import { SystemExternal } from './system_external.js';
  16. import { Variables } from './variables.js';
  17. import { standardLoader } from '../speech_rules/math_map.js';
  18. export const version = Variables.VERSION;
  19. export function setupEngine(feature) {
  20. return __awaiter(this, void 0, void 0, function* () {
  21. return setup(feature);
  22. });
  23. }
  24. export function engineSetup() {
  25. const engineFeatures = ['mode'].concat(Engine.STRING_FEATURES, Engine.BINARY_FEATURES);
  26. const engine = Engine.getInstance();
  27. const features = {};
  28. engineFeatures.forEach(function (x) {
  29. features[x] = engine[x];
  30. });
  31. features.json = SystemExternal.jsonPath;
  32. features.xpath = SystemExternal.WGXpath;
  33. features.rules = engine.ruleSets.slice();
  34. return features;
  35. }
  36. export function engineReady() {
  37. return __awaiter(this, void 0, void 0, function* () {
  38. return setupEngine({}).then(() => EnginePromise.getall());
  39. });
  40. }
  41. export const localeLoader = standardLoader;
  42. export function toSpeech(expr) {
  43. return processString('speech', expr);
  44. }
  45. export function toSemantic(expr) {
  46. return processString('semantic', expr);
  47. }
  48. export function toJson(expr) {
  49. return processString('json', expr);
  50. }
  51. export function toDescription(expr) {
  52. return processString('description', expr);
  53. }
  54. export function toEnriched(expr) {
  55. return processString('enriched', expr);
  56. }
  57. export function number(expr) {
  58. return processString('number', expr);
  59. }
  60. export function ordinal(expr) {
  61. return processString('ordinal', expr);
  62. }
  63. export function numericOrdinal(expr) {
  64. return processString('numericOrdinal', expr);
  65. }
  66. export function vulgar(expr) {
  67. return processString('vulgar', expr);
  68. }
  69. function processString(processor, input) {
  70. return ProcessorFactory.process(processor, input);
  71. }
  72. export const file = {};
  73. file.toSpeech = function (input, opt_output) {
  74. return processFile('speech', input, opt_output);
  75. };
  76. file.toSemantic = function (input, opt_output) {
  77. return processFile('semantic', input, opt_output);
  78. };
  79. file.toJson = function (input, opt_output) {
  80. return processFile('json', input, opt_output);
  81. };
  82. file.toDescription = function (input, opt_output) {
  83. return processFile('description', input, opt_output);
  84. };
  85. file.toEnriched = function (input, opt_output) {
  86. return processFile('enriched', input, opt_output);
  87. };
  88. export function processFile(processor, input, opt_output) {
  89. switch (Engine.getInstance().mode) {
  90. case EngineConst.Mode.ASYNC:
  91. return processFileAsync(processor, input, opt_output);
  92. case EngineConst.Mode.SYNC:
  93. return processFileSync(processor, input, opt_output);
  94. default:
  95. throw new SREError(`Can process files in ${Engine.getInstance().mode} mode`);
  96. }
  97. }
  98. function processFileSync(processor, input, opt_output) {
  99. const expr = inputFileSync_(input);
  100. const result = ProcessorFactory.output(processor, expr);
  101. if (opt_output) {
  102. try {
  103. SystemExternal.fs.writeFileSync(opt_output, result);
  104. }
  105. catch (_err) {
  106. throw new SREError('Can not write to file: ' + opt_output);
  107. }
  108. }
  109. return result;
  110. }
  111. function inputFileSync_(file) {
  112. let expr;
  113. try {
  114. expr = SystemExternal.fs.readFileSync(file, { encoding: 'utf8' });
  115. }
  116. catch (_err) {
  117. throw new SREError('Can not open file: ' + file);
  118. }
  119. return expr;
  120. }
  121. function processFileAsync(processor, file, output) {
  122. return __awaiter(this, void 0, void 0, function* () {
  123. const expr = yield SystemExternal.fs.promises.readFile(file, {
  124. encoding: 'utf8'
  125. });
  126. const result = ProcessorFactory.output(processor, expr);
  127. if (output) {
  128. try {
  129. SystemExternal.fs.promises.writeFile(output, result);
  130. }
  131. catch (_err) {
  132. throw new SREError('Can not write to file: ' + output);
  133. }
  134. }
  135. return result;
  136. });
  137. }
  138. export function walk(expr) {
  139. return ProcessorFactory.output('walker', expr);
  140. }
  141. export function move(direction) {
  142. return ProcessorFactory.keypress('move', direction);
  143. }
  144. export function exit(opt_value) {
  145. const value = opt_value || 0;
  146. EnginePromise.getall().then(() => process.exit(value));
  147. }
  148. export const localePath = FileUtil.localePath;
  149. if (SystemExternal.documentSupported) {
  150. setupEngine({ mode: EngineConst.Mode.HTTP }).then(() => setupEngine({}));
  151. }
  152. else {
  153. setupEngine({ mode: EngineConst.Mode.SYNC }).then(() => setupEngine({ mode: EngineConst.Mode.ASYNC }));
  154. }