Citations

Show where a value or an answer came from, as a highlight on the page or as a span of the original text.

Every field, check and requirement in a result can carry citations: the places in the documents its answer came from. Show them to the person reviewing a case, for example as a highlight on the page, so they can confirm a value at a glance.

What a citation looks like

Most citations point at a spot in a PDF, an image or a Word file:

{
  "file_id": "file_8f2a",
  "quote": "Netto 3.250,00",
  "pages": [
    {
      "page": 1,
      "polygons": [
        [
          { "x": 0.612, "y": 0.418 },
          { "x": 0.731, "y": 0.418 },
          { "x": 0.731, "y": 0.436 },
          { "x": 0.612, "y": 0.436 }
        ]
      ],
      "bounding_box": { "left": 0.612, "top": 0.418, "width": 0.119, "height": 0.018 }
    }
  ]
}
  • file_id names the file.
  • quote is the cited text. It is optional.
  • pages lists the pages the text is on, in reading order. Each entry has:
    • page, the page number. Pages are the physical pages of the file, counted from 1, not the page numbers printed on them.
    • polygons and bounding_box, which say where on that page the text is. The next section shows how to draw them.

A passage that runs across several pages is one citation. Its pages list every page it covers, and quote holds the text of all of them:

{
  "file_id": "file_8f2a",
  "quote": "Übertrag 1.250,00 Gesamtsaldo 4.500,00",
  "pages": [
    {
      "page": 2,
      "polygons": [
        [
          { "x": 0.08, "y": 0.912 },
          { "x": 0.41, "y": 0.912 },
          { "x": 0.41, "y": 0.93 },
          { "x": 0.08, "y": 0.93 }
        ]
      ],
      "bounding_box": { "left": 0.08, "top": 0.912, "width": 0.33, "height": 0.018 }
    },
    {
      "page": 3,
      "polygons": [
        [
          { "x": 0.08, "y": 0.064 },
          { "x": 0.45, "y": 0.064 },
          { "x": 0.45, "y": 0.082 },
          { "x": 0.08, "y": 0.082 }
        ]
      ],
      "bounding_box": { "left": 0.08, "top": 0.064, "width": 0.37, "height": 0.018 }
    }
  ]
}

Highlight it on the page

xybounding_boxpolygons(0, 1)(1, 1)(1, 0)(0, 0)
Coordinates run from (0, 0) at the top-left corner to (1, 1) at the bottom-right corner.

Coordinates are fractions of the page's width and height, from 0 to 1. The origin is the top-left corner, x grows to the right and y grows downward, as in Amazon Textract.

  • polygons are the areas that hold the text. Each polygon lists its corners clockwise and does not repeat the first corner at the end. Polygons can be tilted or apart from each other, and their order says nothing about reading order.
  • bounding_box is the rectangle around all polygons. Use it when a plain rectangle is enough.

To draw a highlight, multiply each x by the width and each y by the height of the page as you display it. On a page displayed at 1000 × 1000 pixels, the polygon above has its corners at (612, 418), (731, 418), (731, 436) and (612, 436). Its bounding box is 119 × 18 pixels, with its top-left corner at (612, 418).

Which page to draw on

Draw each entry of pages on page page of the file's preview PDF, whatever the original format. The preview already shows each page as displayed, so you need no rules per format. Do not draw on the original Word file or on a PDF you converted yourself.

When the position is unknown

Sometimes a citation names a page but not the position on it. Then that page's polygons is empty, and it has no bounding_box:

{
  "file_id": "file_8f2a",
  "pages": [{ "page": 2, "polygons": [] }]
}

Show the page and, if there is one, the quote. Do not draw a rectangle of your own.

Citations in text files

In a plain-text file, a citation points at the bytes of the original file instead of a page. It never has pages:

{
  "file_id": "file_c411",
  "quote": "Netto €",
  "text_span": { "byte_offset": 6, "byte_length": 9 }
}

The quote starts byte_offset bytes into the file you uploaded and is byte_length bytes long. Both count UTF-8 bytes of the original file, including a byte order mark (BOM) and the original line endings. So they are not positions in a JavaScript string, nor in Markdown or HTML you rendered from the file.

Take a file that holds a BOM followed by A\r\nNetto €\r\n:

BytesContent
0–2The BOM
3A
4–5The line break \r\n
6–14Netto €, where the euro sign takes three bytes

The quote is in bytes 6 to 14. This code reads it from the original bytes:

// Reads the quoted passage of a text citation from the original upload's bytes.
import type { TextSpan } from "@aircredit/sdk/partner/v1";

// The file you uploaded: a byte order mark, `A`, CRLF, `Netto €` and CRLF.
const original = new TextEncoder().encode("A\r\nNetto €\r\n");

// A text citation counts UTF-8 bytes, not characters. The euro sign alone takes three.
const span: TextSpan = { byte_offset: 6, byte_length: 9 };
const quote = original.slice(
  span.byte_offset,
  span.byte_offset + span.byte_length,
);
console.log(new TextDecoder("utf-8", { fatal: true }).decode(quote));

Good to know

  • Keep the original files. The API does not return them.
  • The citations of a result never change. To get updated evidence, create a new analysis.

On this page