Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/ragaeeb/kokokor/llms.txt

Use this file to discover all available pages before exploring further.

Overview

This example demonstrates the simplest use case of Kokokor: taking raw OCR observations and reconstructing them into properly formatted paragraphs.

Basic Usage

import { reconstructParagraphs } from 'kokokor';

const result = reconstructParagraphs({
  observations: [
    { text: 'This is the first', bbox: { x: 100, y: 100, width: 200, height: 20 } },
    { text: 'line of text.', bbox: { x: 310, y: 100, width: 150, height: 20 } },
    { text: 'This is a new paragraph.', bbox: { x: 100, y: 150, width: 300, height: 20 } },
  ],
  page: {
    width: 2480,
    height: 3508,
    dpiX: 300,
    dpiY: 300,
  },
});

console.log(result.text);

Expected Output

This is the first line of text.
This is a new paragraph.

How It Works

1

Line Grouping

Kokokor groups observations on the same vertical line based on their y-coordinates and heights. Observations with similar vertical positions are merged into single lines.
2

Horizontal Sorting

Within each line, observations are sorted by x-coordinate to ensure proper reading order (left-to-right for LTR text).
3

Paragraph Detection

Lines are grouped into paragraphs by analyzing vertical spacing. Significant vertical gaps indicate paragraph breaks.
4

Text Formatting

The final text is formatted with appropriate line breaks between paragraphs.

Complete Working Example

import { reconstructParagraphs } from 'kokokor';

// OCR data with multiple lines and paragraphs
const ocrData = {
  observations: [
    // First paragraph, first line
    { text: 'Kokokor is a lightweight', bbox: { x: 100, y: 100, width: 280, height: 20 } },
    { text: 'TypeScript library', bbox: { x: 390, y: 100, width: 200, height: 20 } },
    // First paragraph, second line
    { text: 'designed to reconstruct', bbox: { x: 100, y: 125, width: 260, height: 20 } },
    { text: 'paragraphs from OCR.', bbox: { x: 370, y: 125, width: 220, height: 20 } },
    
    // Second paragraph (note larger vertical gap)
    { text: 'It handles complex layouts,', bbox: { x: 100, y: 180, width: 300, height: 20 } },
    { text: 'poetry, and RTL text.', bbox: { x: 410, y: 180, width: 240, height: 20 } },
  ],
  page: {
    width: 2480,
    height: 3508,
    dpiX: 300,
    dpiY: 300,
  },
};

const result = reconstructParagraphs(ocrData);

console.log(result.text);
// Output:
// Kokokor is a lightweight TypeScript library
// designed to reconstruct paragraphs from OCR.
//
// It handles complex layouts, poetry, and RTL text.

// Access intermediate results
console.log('Lines:', result.lines.length); // 3 lines
console.log('Paragraphs:', result.paragraphs.length); // 2 paragraphs

Key Points

Kokokor automatically handles:
  • Line grouping based on vertical proximity
  • Word ordering within lines
  • Paragraph detection using spacing analysis
  • DPI normalization for consistent results across different resolutions

Next Steps

Arabic Text

Learn how to process RTL text with the isRTL option

Poetry Documents

Handle documents with mixed prose and poetry

Multi-column

Work with complex layouts and footnotes

API Reference

Explore the complete API