訂明本例驗收任務
假設錄音逐字稿只用:code 後一至八個逐位英文數字詞,分號,再 quantity 及一個數字詞。只容許小寫 zero 至 nine。編碼接成文字並保留前零,數量為整數;其他格式待覆核。
- 比對編碼與數量欄格式
- 編碼逐位接成文字數字
- 另轉數量詞為整數
詞語不符或分段不清楚,保留逐字稿覆核。
使用已填個案
| 個案 | 已提供輸入 | 預期結構化結果 | 判斷及原因 |
|---|---|---|---|
| 三位編碼及數量 | {"line":"code one zero five; quantity two"} | {"code":"105","quantity":2} | one zero five 組成編碼文字,two 才是數量。 |
| 編碼以零開頭 | {"line":"code zero one; quantity zero"} | {"code":"01","quantity":0} | 01 保留兩字元;數量已觀察為零。 |
| 不在格式內的數字詞 | {"line":"code ten; quantity two"} | {"error":"grammar"} | ten 不是容許的單位數字詞,不能自行拆。 |
逐步處理 unsupported-ten 個案,須得到甚麼結果?原因是甚麼?
查看參考答案
{"error":"grammar"}。ten 不是容許的單位數字詞,不能自行拆。
執行完整本機驗算
import assert from 'node:assert/strict';
function evaluate(x){
const words=['zero','one','two','three','four','five','six','seven','eight','nine'];
const m=/^code ([a-z ]+); quantity ([a-z]+)$/.exec(x.line);if(!m)return {error:'grammar'};
const digits=m[1].split(' ');if(digits.length<1||digits.length>8||digits.some(w=>!words.includes(w))||!words.includes(m[2]))return {error:'grammar'};
return {code:digits.map(w=>words.indexOf(w)).join(''),quantity:words.indexOf(m[2])};
}
const cases = [
{
"id": "three-code-digits",
"input": {
"line": "code one zero five; quantity two"
},
"expected": {
"code": "105",
"quantity": 2
}
},
{
"id": "leading-zero-code",
"input": {
"line": "code zero one; quantity zero"
},
"expected": {
"code": "01",
"quantity": 0
}
},
{
"id": "unsupported-ten",
"input": {
"line": "code ten; quantity two"
},
"expected": {
"error": "grammar"
}
}
];
for (const c of cases) assert.deepEqual(evaluate(c.input), c.expected, c.id);
console.log(JSON.stringify({checked:cases.length,caseIds:cases.map(c=>c.id)}));
換語言或數字讀法須另訂可用格式。此例只解析已有文字,沒有轉錄聲音或量度語音辨識準確度。