訂明本例驗收任務
假設草稿檢查器只標小寫單詞 guaranteed,而且只計成對雙引號外的字。英文字母為詞邊界;引號內的教學例子可保留,反斜線轉義下一字元。引號未閉或轉義未完列格式錯誤,不能當無問題;不作法律判斷。
- 記錄引號及轉義狀態
- 遮去成對引號內文字
- 找出已訂的引號外單詞
引號格式錯誤須另列,不當草稿乾淨。
使用已填個案
| 個案 | 已提供輸入 | 預期結構化結果 | 判斷及原因 |
|---|---|---|---|
| 引號內教學字眼 | {"text":"Avoid \"guaranteed\" in a promise."} | {"starts":[]} | 唯一一處在成對引號內,本例容許。 |
| 引號外字眼 | {"text":"It is guaranteed."} | {"starts":[6]} | 引號外字從零起計位置六開始。 |
| 引號未閉 | {"text":"Avoid \"guaranteed"} | {"error":"quote-grammar"} | 引號未完,不能把字當已接受教學例子藏起。 |
逐步處理 unclosed-quote 個案,須得到甚麼結果?原因是甚麼?
查看參考答案
{"error":"quote-grammar"}。引號未完,不能把字當已接受教學例子藏起。
執行完整本機驗算
import assert from 'node:assert/strict';
function evaluate(x){
let quoted=false,escaped=false;const unquoted=[];for(const ch of x.text){if(escaped){unquoted.push(quoted?' ':ch);escaped=false;}else if(ch.charCodeAt(0)===92){escaped=true;unquoted.push(' ');}else if(ch==='"'){quoted=!quoted;unquoted.push(' ');}else unquoted.push(quoted?' ':ch);}
if(quoted||escaped)return {error:'quote-grammar'};
const text=unquoted.join(''),hits=[];const pattern=/(?<![A-Za-z])guaranteed(?![A-Za-z])/g;for(const m of text.matchAll(pattern))hits.push(m.index);return {starts:hits};
}
const cases = [
{
"id": "quoted-example-only",
"input": {
"text": "Avoid \"guaranteed\" in a promise."
},
"expected": {
"starts": []
}
},
{
"id": "unquoted-promise",
"input": {
"text": "It is guaranteed."
},
"expected": {
"starts": [
6
]
}
},
{
"id": "unclosed-quote",
"input": {
"text": "Avoid \"guaranteed"
},
"expected": {
"error": "quote-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)}));
保留標出位置給同事查看。此檢查只用一個指定詞及引號規則,未判斷其他承諾是否可接受。