跳至內容
Astera Technology

分段結果重複同一文件時,找回文件覆蓋

每份文件保留首段後才取欄位數,找出重複段落佔用的搜尋位置。

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

訂明本例驗收任務

假設知識清單每段有段落編號及所屬文件。每份文件保留首次遇到的段落,沿用原次序,再取指定文件欄位數;數量不可為負數或小數。不要按段落文字相同來合併。

  1. 讀取每段的所屬文件編號
  2. 每份文件保留首段
  3. 其後才套文件位置上限

欠所屬文件編號,先補資料再合併。

使用已填個案

原創假設輸入及確切答案鍵
個案已提供輸入預期結構化結果判斷及原因
同文件段落佔多個位置{"limit":3,"hits":[{"id":"A1","parent":"A"},{"id":"A2","parent":"A"},{"id":"A3","parent":"A"},{"id":"B1","parent":"B"},{"id":"C1","parent":"C"}]}{"kept":["A1","B1","C1"],"uniqueParents":3}合併後三個位置代表三份文件。
只留一個文件位置{"limit":1,"hits":[{"id":"B2","parent":"B"},{"id":"A1","parent":"A"},{"id":"B1","parent":"B"}]}{"kept":["B2"],"uniqueParents":2}首先遇到 B,便保留 B;不按段落名稱另排。
兩段都欠所屬文件編號{"limit":2,"hits":[{"id":"A1"},{"id":"B1"}]}{"error":"parent-association"}欠關係便拒絕,兩個 undefined 所屬不能合成一份來源。
位置上限有小數{"limit":1.5,"hits":[]}{"error":"slot-limit"}位置數是整數,直接拒絕,不自行取整。
下載可編輯 CSV ↓

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

查看參考答案

{"error":"slot-limit"}。位置數是整數,直接拒絕,不自行取整。

執行完整本機驗算

另存為 .mjs,以 Node.js 執行;只驗提供的個案
import assert from 'node:assert/strict';

function evaluate(x){
 if(!Number.isInteger(x.limit)||x.limit<0)return {error:'slot-limit'};
 if(x.hits.some(h=>!h||typeof h.parent!=='string'||!h.parent.trim()))return {error:'parent-association'};
 const seen=new Set(),kept=[];for(const h of x.hits)if(!seen.has(h.parent)){seen.add(h.parent);kept.push(h.id);}
 return {kept:kept.slice(0,x.limit),uniqueParents:seen.size};
}

const cases = [
  {
    "id": "many-one-parent",
    "input": {
      "limit": 3,
      "hits": [
        {
          "id": "A1",
          "parent": "A"
        },
        {
          "id": "A2",
          "parent": "A"
        },
        {
          "id": "A3",
          "parent": "A"
        },
        {
          "id": "B1",
          "parent": "B"
        },
        {
          "id": "C1",
          "parent": "C"
        }
      ]
    },
    "expected": {
      "kept": [
        "A1",
        "B1",
        "C1"
      ],
      "uniqueParents": 3
    }
  },
  {
    "id": "one-slot",
    "input": {
      "limit": 1,
      "hits": [
        {
          "id": "B2",
          "parent": "B"
        },
        {
          "id": "A1",
          "parent": "A"
        },
        {
          "id": "B1",
          "parent": "B"
        }
      ]
    },
    "expected": {
      "kept": [
        "B2"
      ],
      "uniqueParents": 2
    }
  },
  {
    "id": "missing-parent-ids",
    "input": {
      "limit": 2,
      "hits": [
        {
          "id": "A1"
        },
        {
          "id": "B1"
        }
      ]
    },
    "expected": {
      "error": "parent-association"
    }
  },
  {
    "id": "fractional-slots",
    "input": {
      "limit": 1.5,
      "hits": []
    },
    "expected": {
      "error": "slot-limit"
    }
  }
];
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)}));
下載可執行例子 ↓

保留原段落清單及合併後清單,讓同事看到找回哪些文件位置。這只整理文件分布,未保證保留的文件都有用。

相關資源