Table of Contents

Class Alto

Namespace
Egami.Ocr
Assembly
Egami.dll

Reads and writes ALTO (Analyzed Layout and Text Object), the XML format of library digitization that ABBYY, Tesseract and Transkribus write: pages (Page), lines (TextLine) and words (String) with their positions, WC confidence and LANG.

public static class Alto
Inheritance
Alto
Inherited Members

Examples

// ALTO from ABBYY, Tesseract or a library's digitization: positions in 1/10 mm or 1/1200 inch become pixels at Dpi.
IReadOnlyList<OcrResult> pages = Alto.Parse(File.ReadAllText("page.xml"), new OcrReadOptions { Dpi = 300 });
Console.WriteLine($"{pages[0].ImageSize}: {pages[0].Text}");

// Turn it into hOCR, or write ALTO 4 in pixels.
using FileStream hocr = File.Create("page.hocr");
Hocr.Write(hocr, pages);
using FileStream alto = File.Create("page-v4.xml");
Alto.Write(alto, pages);

Remarks

The reader takes ALTO 1 to 4 by element names, in any namespace. Positions in 1/10 mm or 1/1200 inch become pixels at Dpi. A HYP hyphen joins the word before it. No DTD or schema is ever fetched. The writer writes ALTO 4 in pixels.

Methods

Parse(string, OcrReadOptions?)

Reads every page of ALTO text.

public static IReadOnlyList<OcrResult> Parse(string alto, OcrReadOptions? options = null)

Parameters

alto string

The ALTO document.

options OcrReadOptions

The limits and the resolution for physical units, or null for Default.

Returns

IReadOnlyList<OcrResult>

One result per Page, in document order.

Examples

// ALTO from ABBYY, Tesseract or a library's digitization: positions in 1/10 mm or 1/1200 inch become pixels at Dpi.
IReadOnlyList<OcrResult> pages = Alto.Parse(File.ReadAllText("page.xml"), new OcrReadOptions { Dpi = 300 });
Console.WriteLine($"{pages[0].ImageSize}: {pages[0].Text}");

// Turn it into hOCR, or write ALTO 4 in pixels.
using FileStream hocr = File.Create("page.hocr");
Hocr.Write(hocr, pages);
using FileStream alto = File.Create("page-v4.xml");
Alto.Write(alto, pages);

Exceptions

ArgumentNullException

alto is null.

CorruptDataException

The text is not well-formed XML, or has no alto root element.

LimitExceededException

The text breaks a limit of options.

Read(Stream, OcrReadOptions?)

Reads every page of an ALTO file.

public static IReadOnlyList<OcrResult> Read(Stream stream, OcrReadOptions? options = null)

Parameters

stream Stream

The file, from its current position. It is left open.

options OcrReadOptions

The limits and the resolution for physical units, or null for Default.

Returns

IReadOnlyList<OcrResult>

One result per Page, in file order.

Examples

// ALTO from ABBYY, Tesseract or a library's digitization: positions in 1/10 mm or 1/1200 inch become pixels at Dpi.
IReadOnlyList<OcrResult> pages = Alto.Parse(File.ReadAllText("page.xml"), new OcrReadOptions { Dpi = 300 });
Console.WriteLine($"{pages[0].ImageSize}: {pages[0].Text}");

// Turn it into hOCR, or write ALTO 4 in pixels.
using FileStream hocr = File.Create("page.hocr");
Hocr.Write(hocr, pages);
using FileStream alto = File.Create("page-v4.xml");
Alto.Write(alto, pages);

Exceptions

ArgumentNullException

stream is null.

CorruptDataException

The file is not well-formed XML, or has no alto root element.

LimitExceededException

The file breaks a limit of options.

Write(Stream, IEnumerable<OcrResult>)

Writes pages as an ALTO 4 document in UTF-8, in pixels, one Page per result.

public static void Write(Stream stream, IEnumerable<OcrResult> pages)

Parameters

stream Stream

Where to write, from its current position. It is left open.

pages IEnumerable<OcrResult>

The pages.

Examples

// ALTO from ABBYY, Tesseract or a library's digitization: positions in 1/10 mm or 1/1200 inch become pixels at Dpi.
IReadOnlyList<OcrResult> pages = Alto.Parse(File.ReadAllText("page.xml"), new OcrReadOptions { Dpi = 300 });
Console.WriteLine($"{pages[0].ImageSize}: {pages[0].Text}");

// Turn it into hOCR, or write ALTO 4 in pixels.
using FileStream hocr = File.Create("page.hocr");
Hocr.Write(hocr, pages);
using FileStream alto = File.Create("page-v4.xml");
Alto.Write(alto, pages);

Exceptions

ArgumentNullException

stream or pages is null.