首页 > 解决方案 > 语音规则引擎 3.2 中的行为变化?

问题描述

我正在使用语音规则引擎从 MathML 生成英文文本。尝试从 v3.1.1 升级到 v3.2.0 时,我看到测试失败,原因我不明白。

我创建了一个简单的两个文件项目来说明这个问题:

包.json

{
  "name": "failure-example",
  "license": "UNLICENSED",
  "private": true,
  "engines": {
    "node": "14.15.5",
    "npm": "6.14.11"
  },
  "scripts": {
    "test": "jest"
  },
  "dependencies": {
    "speech-rule-engine": "3.2.0"
  },
  "devDependencies": {
    "jest": "^26.6.3"
  },
  "jest": {
    "notify": false,
    "silent": true,
    "verbose": true
  }
}

例子.test.js

const sre = require('speech-rule-engine');

beforeAll(() => {
    sre.setupEngine({
        domain: 'mathspeak'
    });
});

test('simple single math', () => {
    expect(JSON.parse(JSON.stringify(sre.engineSetup(), ['domain', 'locale', 'speech', 'style'])))
        .toEqual({
            locale: 'en',
            speech: 'none',
            style: 'default',
            domain: 'mathspeak',
        });
    expect(sre.engineReady())
        .toBeTruthy();
    expect(sre.toSpeech('<math><mrow><msup><mn>3</mn><mn>7</mn></msup></mrow></math>'))
        .toBe('3 Superscript 7');
});

运行npm installnpm run test导致失败,因为 SRE 返回37而不是3 Superscript 7。编辑 package.json 以使用引擎的 v3.1.1 并重新运行导致通过测试。

显然有些事情发生了变化,但我完全错过了我需要做的适应。有没有其他人遇到过这个,或者看到我显然没有?

标签: mathml

解决方案


在 SRE 维护者的帮助下,问题解决了。问题不在 3.2.0 中,而是 jest 没有等待 sre 准备好。由于规则已编译到核心中,因此测试仅在 3.1.1 中偶然正确。以下测试在 3.1.1 中使用上述设置失败,并且未加载语言环境:

expect(sre.toSpeech('<math><mo>=</mo></math>'))
        .toBe('equals');
    Expected: "equals"
    Received: "="

主要原因是 jest 无法加载语言环境文件。设置"silent": false会显示错误:

 Unable to load file: /tmp/tests/node_modules/speech-rule-engine/lib/mathmaps/en.js
      TypeError: Cannot read property 'readFileSync' of null

这个错误的原因是 jest 不知道它在 node.js 中运行。添加:

    "testEnvironment": "node",

到开玩笑的配置package.json会导致预期的行为。


推荐阅读