/** * TRP classes for (generic document) low-level content objects */ import { ApiBlockType, ApiRelationshipType } from "./api-models/base"; import { ApiLineBlock, ApiSelectionElementBlock, ApiSelectionStatus, ApiSignatureBlock, ApiTextType, ApiWordBlock } from "./api-models/content"; import { ApiBlock } from "./api-models/document"; import { ActionOnUnexpectedBlockType, ApiBlockWrapper, Constructor, IApiBlockWrapper, IBlockManager, IBlockTypeFilterOpts, IHostedApiBlockWrapper, IRenderable, IRenderOpts, IWithParentPage, IWithText, PageHostedApiBlockWrapper } from "./base"; import { Geometry, IWithGeometry } from "./geometry"; /** * TRP.js parsed object for an individual word of text * * Wraps an Amazon Textract `WORD` block in the underlying API response. */ export declare class Word extends ApiBlockWrapper implements IRenderable, IWithGeometry { _geometry: Geometry; constructor(block: ApiWordBlock); /** * 0-100 based confidence of the OCR model in extracting the text of this word */ get confidence(): number; set confidence(newVal: number); /** * Position of the word on the input image / page */ get geometry(): Geometry; /** * Text extracted by the OCR model */ get text(): string; /** * Whether the text appears hand-written or computer-generated */ get textType(): ApiTextType; set textType(newVal: ApiTextType); /** * The semantic `html()` representation of a `Word` is just the (HTML-escaped) text */ html(opts?: IRenderOpts): string; /** * The basic human-readable `str()` representation of a `Word` is just the `.text` */ str(): string; } /** * Interface for objects that have child items representing actual "content" * * Typically used for containers of low-level content items like `Word`s and `SelectionElement`s. * In some cases (like Layout), higher-level containers (of items like `LINE` may use the same * interface. * * For objects guaranteed to contain only `Word`s (no `SelectionElement`s), prefer `IWithWords` * instead. */ export interface IWithContent & IRenderable> extends IWithText { /** * Number of content items in this object */ get nContentItems(): number; /** * Return the text content of this element, with additional options * * Unlike the plain `.text` property, this method supports filtering which block types are * included and controlling behaviour when unexpected ones are encountered. * * @param opts Optional configuration for filtering rendering to certain content types */ getText(opts?: IBlockTypeFilterOpts): string; /** * Iterate through the Content items in this object * * Optionally filter certain content Block types by specifying `opts` * * @example * for (const item of cell.iterContent()) { * console.log(item.text); * } * @example * [...cell.iterContent({ skipBlockTypes: [ApiBlockType.SelectionElement] })].forEach( * (item) => console.log(item.text) * ); */ iterContent(opts?: IBlockTypeFilterOpts): Iterable; /** * List the Content items in this object */ listContent(opts?: IBlockTypeFilterOpts): Array; } /** * Configuration options for WithContent mixin (see `buildWithContent`) */ export interface IWithContentMixinOptions { /** * What types of direct Child Block to consider as "content" * * Defaults to [SELECTION_ELEMENT, SIGNATURE, WORD] as per `buildWithContent` */ contentTypes?: ApiBlockType[]; /** * Action to take on encountering a child Block of unexpected BlockType * * Set "error" to throw an error, "warn" to log a warning, or falsy to skip silently. */ onUnexpectedBlockType?: ActionOnUnexpectedBlockType; /** * Other types of direct child block that are expected but non-content * * This is optional to specify, but setting it up will provide more useful behaviour when using * strict `onUnexpectedBlockType`s settings. */ otherExpectedChildTypes?: ApiBlockType[] | null; } /** * Mixin factory for elements that have child Content (such as `Word`s and/or `SelectionElement`s) * * While it's possible to apply a TS mixin to a generic base class (with expressions like * `extends MyMixin(BaseClass)`), mixins cannot alter the base class' constructor * signature so they can't introduce additional type arguments (generic aspects) of their own. This * double-call mixin factory pattern provides a workaround *only* for cases where we're able to * specify the mixin type arguments at the point it's applied: Enabling a somewhat generic * definition of "content" that consumer classes can dictate. * * For objects guaranteed to contain only `Word` items, prefer `WithWords` instead. * * See: https://stackoverflow.com/a/48492205/13352657 * * @param contentBlockTypes API block types to be included when listing child "Content". Set `[]` * to disable this filter and preserve all items */ export declare function buildWithContent & IRenderable>({ contentTypes, onUnexpectedBlockType, otherExpectedChildTypes, }?: IWithContentMixinOptions): >>(SuperClass: T) => { new (...args: any[]): { getText(opts?: IBlockTypeFilterOpts): string; iterContent({ includeBlockTypes, onUnexpectedBlockType, skipBlockTypes, }?: IBlockTypeFilterOpts): Iterable; listContent({ includeBlockTypes, onUnexpectedBlockType, skipBlockTypes, }?: IBlockTypeFilterOpts): Array; readonly nContentItems: number; /** * A default text representation getter that concatenates child content separated by spaces * * Objects (like `Line`) that define their own representation of the overall text or need to * join content with something other than a space (like a newline) should override this. */ get text(): string; readonly id: string; readonly blockType: ApiBlockType; readonly childBlockIds: string[]; relatedBlockIdsByRelType(relType: ApiRelationshipType | ApiRelationshipType[]): string[]; _dict: TBlock; readonly dict: TBlock; parentPage: TPage; iterRelatedItemsByRelType(relType: ApiRelationshipType | ApiRelationshipType[], opts?: IBlockTypeFilterOpts | undefined): Iterable>; listRelatedItemsByRelType(relType: ApiRelationshipType | ApiRelationshipType[], opts?: IBlockTypeFilterOpts | undefined): IApiBlockWrapper[]; }; } & T; /** * Configuration options for WithWords mixin */ export interface IWithWordsMixinOptions { /** * Action to take on encountering a child Block of unexpected BlockType * * Set "error" to throw an error, "warn" to log a warning, or falsy to skip silently. */ onUnexpectedBlockType?: ActionOnUnexpectedBlockType; /** * Other types of direct child block that are expected but non-content * * This is optional to specify, but setting it up will provide more useful behaviour when using * strict `onUnexpectedBlockType`s settings. */ otherExpectedChildTypes?: ApiBlockType[] | null; } /** * Interface for objects that have child `Word`s (such as LINEs of text) * * For objects that might contain `SelectionElement`s as well (such as table cells), use * `IWithContent` instead. */ export interface IWithWords extends IWithText { /** * Number of text Words in this object */ get nWords(): number; /** * Return the text content of this element, with additional options * * Unlike the plain `.text` property, this method supports controlling error behaviour when * non-WORD blocks are encountered. * * @param opts Optional configuration for filtering rendering to certain content types */ getText(opts?: IBlockTypeFilterOpts): string; /** * Iterate through the text `Word` items in this object * * Optionally control what happens when unexpected (non-Word) block types are encountered, by * specifying filter `opts`. * * @example * for (const word of line.iterWords()) { * console.log(word.text); * } * @example * [...line.iterWords()].forEach( * (word) => console.log(word.text) * ); */ iterWords(opts?: IBlockTypeFilterOpts): Iterable; /** * List the text `Word`s in this object * * Optionally control what happens when unexpected (non-Word) block types are encountered, by * specifying filter `opts`. */ listWords(opts?: IBlockTypeFilterOpts): Word[]; /** * Fetch a particular text `Word` in this object by index from 0 to `.nWords - 1` * @param ix 0-based index in the word list * @throws if the index is out of bounds */ wordAtIndex(ix: number): Word; } /** * Mixin for page-hosted API block wrappers with CHILD relations to WORD objects * * Adds dynamic functionality to list and traverse the Word objects contained in the content, and a * basic implementation for getting the overall `.text`. * * @param SuperClass The class to extend * @param opts Configuration options for how to handle other (non-WORD) child blocks */ export declare function WithWords & IWithParentPage>>(SuperClass: T, { onUnexpectedBlockType, otherExpectedChildTypes }?: IWithWordsMixinOptions): { new (...args: any[]): { getText(opts?: IBlockTypeFilterOpts): string; iterWords({ includeBlockTypes, onUnexpectedBlockType, skipBlockTypes, }?: IBlockTypeFilterOpts): Iterable; listWords({ includeBlockTypes, onUnexpectedBlockType, skipBlockTypes, }?: IBlockTypeFilterOpts): Word[]; readonly nWords: number; /** * A default text representation getter that concatenates child `Word`s separated by spaces * * Objects (like `Line`) that define their own representation of the overall text or need to * join words with something other than a space (like a newline) should override this. */ get text(): string; wordAtIndex(ix: number): Word; readonly id: string; readonly blockType: ApiBlockType; readonly childBlockIds: string[]; relatedBlockIdsByRelType(relType: ApiRelationshipType | ApiRelationshipType[]): string[]; _dict: TBlock; readonly dict: TBlock; parentPage: TPage; iterRelatedItemsByRelType(relType: ApiRelationshipType | ApiRelationshipType[], opts?: IBlockTypeFilterOpts | undefined): Iterable>; listRelatedItemsByRelType(relType: ApiRelationshipType | ApiRelationshipType[], opts?: IBlockTypeFilterOpts | undefined): IApiBlockWrapper[]; }; } & T; declare const LineGeneric_base: { new (...args: any[]): { getText(opts?: IBlockTypeFilterOpts | undefined): string; iterWords({ includeBlockTypes, onUnexpectedBlockType, skipBlockTypes, }?: IBlockTypeFilterOpts): Iterable; listWords({ includeBlockTypes, onUnexpectedBlockType, skipBlockTypes, }?: IBlockTypeFilterOpts): Word[]; readonly nWords: number; /** * A default text representation getter that concatenates child `Word`s separated by spaces * * Objects (like `Line`) that define their own representation of the overall text or need to * join words with something other than a space (like a newline) should override this. */ get text(): string; wordAtIndex(ix: number): Word; readonly id: string; readonly blockType: ApiBlockType; readonly childBlockIds: string[]; relatedBlockIdsByRelType(relType: ApiRelationshipType | ApiRelationshipType[]): string[]; _dict: ApiBlock; readonly dict: ApiBlock; parentPage: IBlockManager; iterRelatedItemsByRelType(relType: ApiRelationshipType | ApiRelationshipType[], opts?: IBlockTypeFilterOpts | undefined): Iterable>; listRelatedItemsByRelType(relType: ApiRelationshipType | ApiRelationshipType[], opts?: IBlockTypeFilterOpts | undefined): IApiBlockWrapper[]; }; } & typeof PageHostedApiBlockWrapper; /** * Generic base class for a Line, as the parent Page is not defined here. * * If you're consuming this library, you probably just want to use `document.ts/Line`. */ export declare class LineGeneric extends LineGeneric_base implements IRenderable, IWithGeometry> { _geometry: Geometry>; constructor(block: ApiLineBlock, parentPage: TPage); get confidence(): number; set confidence(newVal: number); /** * Position of the text line on the input image / page */ get geometry(): Geometry>; /** * Text content of the LINE * * This uses the pre-calculated `.Text` property on the Block, rather than concatenating WORDs */ get text(): string; /** * Fetch the text in this line with filtering options * * Note that since `LINE.Text` comes directly from Textract, filtering out `ApiBlockType.Word` * won't have any effect here. * * @param opts Optional configuration for filtering rendering to certain content types */ getText(opts?: IBlockTypeFilterOpts): string; /** * The semantic `html()` representation of a `Line` is just the (HTML-escaped) text * * Note that since `LINE.Text` comes directly from Textract, filtering out `ApiBlockType.Word` * won't have any effect here. */ html(opts?: IRenderOpts): string; str(): string; } /** * TRP.js parsed object for a selection element, such as a checkbox or radio selector * * Wraps an Amazon Textract `SELECTION_ELEMENT` block in the underlying API response. */ export declare class SelectionElement extends ApiBlockWrapper implements IRenderable, IWithGeometry { _geometry: Geometry; constructor(block: ApiSelectionElementBlock); /** * 0-100 based confidence of the model detecting this selection element and its status */ get confidence(): number; set confidence(newVal: number); /** * Position of the selection element on the input image / page */ get geometry(): Geometry; /** * `true` if SELECTED, `false` if NOT_SELECTED, or raise an error if some unexpected value */ get isSelected(): boolean; /** * Whether the element is selected/ticked/checked/etc, or not */ get selectionStatus(): ApiSelectionStatus; set selectionStatus(newVal: ApiSelectionStatus); /** * The semantic `html()` representation of a `SelectionElement` uses an `` element * * We render a checkbox, but `disable` it to prevent accidental edits when viewing reports */ html(opts?: IRenderOpts): string; /** * The human-readable `str()` representation of a sel. element is just the `.selectionStatus` */ str(): string; /** * The "text content" of a sel. element is just the `.selectionStatus` */ get text(): string; } /** * TRP.js parsed object for a detected signature * * Wraps an Amazon Textract `SIGNATURE` block in the underlying API response. */ export declare class Signature extends ApiBlockWrapper implements IRenderable, IWithGeometry { _geometry: Geometry; constructor(block: ApiSignatureBlock); /** * 0-100 based confidence of the model detecting this selection element and its status */ get confidence(): number; set confidence(newVal: number); /** * Position of the selection element on the input image / page */ get geometry(): Geometry; /** * The semantic `html()` representation of a `SelectionElement` uses an `` element * * We render a checkbox, but `disable` it to prevent accidental edits when viewirg reports */ html(opts?: IRenderOpts): string; /** * The human-readable `str()` representation of a signature is a placeholder * * Looks like: * * /-------------\ * | [SIGNATURE] | * \-------------/ */ str(): string; /** * The "text content" of a signature element is empty */ get text(): ""; } export {};