system.js 6.3 KB

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