Document AI 驗證和修正功能會運用通用運算式語言 (CEL),在文件處理工作流程中彈性驗證及操控資料。Document AI 提供一系列自訂函式、巨集和行為修改項目,專為文件實體資料量身打造。
在 CEL 運算式中存取實體
所有運算式都會根據名為 doc 的根變數進行評估,這個變數包含屬於文件的片語或屬性的實體。這些實體與所擷取文件的實體結構密切相關。
雖然擷取的實體包含許多屬性,但只有三個屬性可用於 CEL 評估。
mention_text:擷取實體中顯示的原始擷取文字。預設為空字串。normalized_value:擷取實體中出現的正規化提及文字。預設為空值。請參閱正規化一文瞭解詳情。bounding_poly:特殊物件,包含文件中擷取實體位置的表示法,用於對齊檢查。預設為空值。
資料模型
doc 內擷取實體的確切結構取決於兩項因素。第一個是結構是否為具體值 (例如數字或純文字),或是複雜物件。第二個因素是發生類型為單一或多重。詳情請參閱 OccurrenceType 的說明。
驗證資料模型的主要功能是,系統會自動為結構定義中定義但未從文件中擷取的任何實體填入預設值。這項設計可讓您略過 CEL 運算式中的大部分明確空值檢查,大幅簡化驗證運算式。您只需要明確編寫空值檢查,確保所選實體確實已擷取。
葉節點實體範例
以下各節說明如何存取葉節點實體中的實體,也就是沒有巢狀子實體的實體。葉節點實體會直接保留值。
單一出現次數的葉節點實體
這是最基本的情況,使用 OccurrenceType 的 OPTIONAL_ONCE 或 REQUIRED_ONCE。實體會以包含三個標準屬性的物件表示。
存取這些值的範例如下:doc.invoice_date.normalized_value。
結構如下:
"invoice_date": {
"mention_text": "1",
"normalized_value": 1.0,
"bounding_poly": bounding_poly_object
}
預設值:
"invoice_date": {
"mention_text": "",
"normalized_value": null,
"bounding_poly": null
}
多次出現的葉節點實體
這個情況適用於可能多次出現的葉節點實體,且具有 OccurrenceType、OPTIONAL_MULTIPLE 或 REQUIRED_MULTIPLE。舉例來說,在付款期限清單中,這會以「物件」表示,每個屬性會保留所有出現次數的對應「清單」值。因此,mention_text、normalized_value 和 bounding_poly 等屬性可能有多個實體。
存取這些值的範例如下:doc.payment_due_dates.normalized_value[0]。
結構如下:
"payment_due_dates": {
"mention_text": ["Mar 1, 2024", "Apr 1, 2024"],
"normalized_value": [null, proto.timestamp(2024-04-01)],
// Note: If a value is not normalized, it is stored as a null.
"bounding_poly": [bounding_poly_object,bounding_poly_object]
}
預設值:
"payment_due_dates": {
"mention_text": [],
"normalized_value": []
"bounding_poly": []
}
巢狀實體
巢狀實體是其他實體的容器,這些實體就是巢狀實體的「子項」。
巢狀實體,出現一次
如果巢狀實體只出現一次 (例如單一 receiver_address),則會以「物件」表示,其中的鍵是子實體的名稱。
存取這些值的範例如下:doc.receiver_address.city.mention_text。
結構如下:
"receiver_address": {
"street": {
"mention_text": "123 Main St",
"normalized_value": "123 Main St",
"bounding_poly": bounding_poly_object
}
}
預設值:
"receiver_address": {
"street": {
"mention_text": "",
"normalized_value": null,
"bounding_poly": null
}
}
多次出現的巢狀實體
如果巢狀實體可多次出現,則會以物件清單的形式表示。清單中的每個物件都代表巢狀實體的完整例項,並包含其子項。
存取這些值的範例如下:doc.line_items[1].description.normalized_value。
結構如下:
"line_items": [
{
"description": { "mention_text": "Product A", ... },
"quantity": { "mention_text": "2", ... }
},
{
"description": { "mention_text": "Service B", ... },
"quantity": { "mention_text": "5", ... }
}
]
預設值:
"line_items": []
標準化值轉換表
這個表格會顯示所選架構實體資料類型如何轉換為 CEL 資料類型。normalized_value
| 結構定義資料型別 | CEL 資料類型 |
|---|---|
| 幣別、地址 | string |
| 數字、金額 | double |
| 日期時間 | proto.Timestamp |
| 核取方塊、 簽名 | bool
|
| 純文字 | 不適用 |
運算式範例
以下提供幾個 CEL 運算式範例。
// Leaf entity with a single occurrence: Get the invoice ID string
doc.invoice_id.normalized_value == "INV-12345"
// Leaf entity with multiple occurrences: Get the first payment term from the list
doc.payments.mention_text[0].matches('^\d+$')
// Nested entity with one occurrence: Access a child entity of a single nested entity
doc.receiver_address.name.normalized_value.star
tsWith("John")
// Nested entity with multiple occurrences: Access a child of a specific item in a list of nested entities
doc