Projects

Likhit

Python library

Likhit turns Nepali government documents into clean text. It's an open-source plugin for Microsoft's MarkItDown, published on PyPI, and it's what Jawafdehi uses to transcribe its archive. I started it, and we've built it out as a team at Jawafdehi.

The problem

Copy text out of a Nepali government PDF and you often get garbage. महालेखापरीक्षकको, "of the Auditor General", can come out as dxfn]vfk/LIfssf], as महालेखापरीçकको with a stray Latin letter in the middle, or as लेखापरी�ण with a hole where a joined letter should be. Sometimes letters lose a mark, so कार्यालय becomes कायायलय, or come out spaced apart one by one.

That makes the archive impossible to search, quote or hand to an AI model. And it isn't one problem but several, mixed together in the same documents.

Why it happens

A PDF doesn't store text. It stores which shapes to draw from which font, plus a table that maps each shape back to a character so you can copy it. In these documents that table is often wrong or missing.

Older fonts like Preeti put Devanagari shapes on English keyboard keys, so the text really is English letters underneath. Newer fonts like Kalimati and Lohit get cut down when the PDF is made, and the table and the rules for joining letters are thrown away. Some documents are scans with no text at all, and about a quarter of the police press releases we looked at hide an unreadable "decoy" text layer behind the scanned image. A few even contain the same text twice, once correct and once garbled.

Rebuilding the character map

Instead of guessing the text back, Likhit fixes the table. It opens each font embedded in the PDF, works out which shape is which character from the font's own data, resolves joined letters from its joining rules, and writes a corrected table back into the PDF before extracting any text.

When a font has been stripped so far that it knows nothing about its own shapes, Likhit falls back on reference tables built from the original fonts. For Kalimati and Mangal these match each shape by its outline rather than its position, because the same font turns up cut down in different ways with its shapes in different orders. Anything it still can't map is marked as unknown rather than quietly dropped.

For the older keyboard fonts it uses existing converter maps for Preeti, Kantipur and others, and picks the right one by looking at the text rather than trusting the font's name. Names lie: fonts called "Himalb" turned out to contain Preeti, and following the name had corrupted text 1,438 times across 13 reports.

Picking the best result

Likhit doesn't trust a single method. It tries the standard extraction first, and if the result looks wrong, such as unmapped character codes, English-looking words with no vowels, or broken table lines, it extracts again with its own repairs and keeps whichever result scores better. A result that might have merged numbers from a table loses even if it scores higher, because a wrong figure is worse than an ugly one.

It also keeps the layout readable: two-column pages and notices are read in the right order, table cells that ran together are split again using the positions of the characters and the lines on the page, and every page of output is labelled with its page number in the original.

OCR only when needed

OCR is the last resort, used only when a page is almost entirely one image and its text layer is empty or garbage. Pages are rendered and sent to a vision model behind any OpenAI-compatible endpoint, hosted or local.

Models sometimes reply "this appears to be a blank page" instead of transcribing, so those replies are caught and rejected rather than stored as text. If OCR isn't set up, Likhit returns its best result and lists the exact pages that still need it.

Checking quality

Every transcript can be audited on eight separate kinds of damage, including leftover keyboard-font text, lost marks, damaged numbers and broken spacing. The verdict is the worst of them, not an average. Averaging had let thousands of documents score as clean while real damage hid inside them, like a document that was 97.7% correct Devanagari whose first line was still garbage.

The public benchmark runs real government documents through Likhit with no OCR, hosted OCR and a local model. Of the 16 published documents, 13 convert correctly without calling OCR at all.

Removing personal data

Court and police documents contain citizenship numbers, phone numbers and bank accounts. Likhit removes them by looking for the value next to a label, in Nepali or English, rather than matching any run of digits, which would wreck case numbers and amounts. There are stricter and looser policies, and the strictest is the default.

It can also produce a redacted copy of the PDF itself. The text is actually removed, not just covered with a black box, along with annotations, form fields and metadata. The file is then read back to check nothing survived, and pages it can't redact safely are refused.

Built with

  • Open source
book a chat