跳至內容
Astera Technology

核對草稿禁用句檢查器的漏報與誤報

用有限引號及轉義規則查指定禁用詞,錯引號不能當乾淨草稿。

下載完整個案資料及參考答案 ↓

訂明本例驗收任務

假設草稿檢查器只標小寫單詞 guaranteed,而且只計成對雙引號外的字。英文字母為詞邊界;引號內的教學例子可保留,反斜線轉義下一字元。引號未閉或轉義未完列格式錯誤,不能當無問題;不作法律判斷。

  1. 記錄引號及轉義狀態
  2. 遮去成對引號內文字
  3. 找出已訂的引號外單詞

引號格式錯誤須另列,不當草稿乾淨。

使用已填個案

原創假設輸入及確切答案鍵
個案已提供輸入預期結構化結果判斷及原因
引號內教學字眼{"text":"Avoid \"guaranteed\" in a promise."}{"starts":[]}唯一一處在成對引號內,本例容許。
引號外字眼{"text":"It is guaranteed."}{"starts":[6]}引號外字從零起計位置六開始。
引號未閉{"text":"Avoid \"guaranteed"}{"error":"quote-grammar"}引號未完,不能把字當已接受教學例子藏起。
下載可編輯 CSV ↓

逐步處理 unclosed-quote 個案,須得到甚麼結果?原因是甚麼?

查看參考答案

{"error":"quote-grammar"}。引號未完,不能把字當已接受教學例子藏起。

執行完整本機驗算

另存為 .mjs,以 Node.js 執行;只驗提供的個案
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)}));
下載可執行例子 ↓

保留標出位置給同事查看。此檢查只用一個指定詞及引號規則,未判斷其他承諾是否可接受。

相關資源