billba / excitation

2 stars 4 forks source link

Create new module for handling doc int responses to replace bloated `Utility.ts` #74

Open lee0c opened 1 day ago

lee0c commented 1 day ago

What/Why

TODO

Types & Interfaces

interface AnalyzeResult {
  apiVersion: string;
  modelId: string;
  stringIndexType: string;
  content: string;
  delta?: number; // <- added by Preprocess.ts
  pages: Page[];
  tables: Table[];
  paragraphs: Paragraph[];
  styles: Style[];
  contentFormat: string;
  sections: Section[];
  figures: Figure[];
}

export interface Page {
  pageNumber: number;
  angle: number;
  width: number;
  height: number;
  unit: string;
  words: Word[];
  lines: Line[];
  regions?: Region[]; // <- added by Preprocess.ts, see below for definition
  spans: Span[];
  selectionMarks?: SelectionMark[];
}

Additionally, all cases of polygon: number[] have been changed to polygon: polygon4 to use the new type below:

export type polygon4 = [
  number, number,
  number, number,
  number, number,
  number, number
];

// This will be the output of a conversion function to convert polygonC into pure number[] polys
export type polygonN = number[];

// This does mean supporting cases where the lead and
// the tail are non-adjacent (i.e. the selection travels
// to the next line but not far enough to overlap with
// the previous line)
// might need some basic typescript help here to express what I want happening:
// lead always exists, then tail might exist, *then* body might exist (iff tail exists)
export type polygonC =
  | {
      lead: polygon4;
      tail?: polygon4;
    }
  | {
      lead: polygon4;
      body?: polygon4;
      tail: polygon4;
    };

// lowercase polygon is pageless
export type polygon = polygon4 | polygonC

polygon4 is explicitly a 4-pt poly, while polygonC is a complex poly. Still deciding whether Summaries (see below) always explicitly return polygonC or if they can return either.

More interfaces:

// uppercase Polygon has a page
export interface Polygon {
  polygon: polygon;
  page: number;
}

// lowercase point is pageless
export interface point {
  x: number;
  y: number;
}

// uppercase Point has a page
export interface Point {
  point: point;
  page: number;
}

// lowercase range is just number primitives and is pageless
// represents any start, end range
export type range = [number, number]

// uppercase range uses Points and therefore has pages
// specifically represents a CURSOR range not a drawn box
export interface Range {
  start: Point;
  end: Point;
}

// Akin to columns or sections
// not relevant to the Viewer etc, just used in-module
export interface Region {
  polygon: polygon4;
  lineIndices: range;
  wordIndices: range;
}

// an excerpt and its location(s)
export interface Summary {
  excerpt: string;
  polygons: Polygon[];
}

Functions

Preprocess.ts

// in place change to di, adds a field to di.analyzeResult that models the spacing of the document (scale of 0-1?)
export function setDocumentDelta(
  di: DocumentIntelligenceResponse
);

// in place change to di, adds a `Region[]` field to each page of analyzeResult
export function createRegions(
  di: DocumentIntelligenceResponse
);

DocInt.ts (naming?)

// takes in a cursor range (aka, assumed to be contiguous in the document; we're not drawing a box)
// and creates the corresponding Summary
export function rangeToSummary(
  range: Range,
  di: DocumentIntelligenceResponse
): Summary;

// takes in an excerpt and creates the corresponding Summary with the *first* instance of the excerpt in di
export function excerptToSummary(
  excerpt: string,
  di: DocumentIntelligenceResponse
): Summary;
lee0c commented 1 day ago

Actually, I think there's two things going on here:

A pre-processing module that adds two fields to DocumentIntelligenceResponse:

The "real time" module which handles: