1

I have health documents which are currently in the hOCR format. I want to convert them to an xml format.

Is there a Mac/Linux - specific software which can do that?

2

A Mac/Linux/Windows specific software might exist, but I wrote my own PHP script to get all the data out of the hOCR file into a database, and from there I can create whatever format I want with the data.

hOCR should be xhtml already. The php script below reads the hOCR data file (produced by Tesseract OCR) word by word (with word data, like page, line, word nr, word itself, coordinates...) into a SQLite3 database, and from there I can do whatever I want with the data, like querying and insert it into some xml tags as required, or look for words from the next line with same starting x coordinates, etc.

The php code (5.4+) below contains a lot of unnecessary stuff like comments etc., but it reads the content of a hOCR file into a SQLite3 db file quite fast.

Depending on Your language (here mostly Finnish, ie. ISO-8859-15) You might want to change some of the regex etc. And the file name to be processed is given as the first argument to this script.

<?php
// Splitting a hOCR file to rows with page, area, line, word info per word in a doc
// Function to replace all separator chars on row with one thing, so that one explode per row will do

$dbh_hocr=new SQlite3("hOCRtemp.db");

$createTable=$dbh_hocr->exec("create table if not exists invWords (id INTEGER PRIMARY KEY, FileName TEXT, PageNr INTEGER, LineNr INTEGER, WordNr INTEGER, WordName TEXT, X1 INTEGER, Y1 INTEGER, X2 INTEGER, Y2 INTEGER)");
$clearTheTable=$dbh_hocr->exec("delete from invWords");

$thisDocWords=array();
// The file name is in argv[1]
$fileNameHere=$argv[1];

$fileToUse=file_get_contents($fileNameHere);
$pages=explode("<div class='ocr_page'", $fileToUse);
foreach($pages as $ocrCarea)
    {
    $thisOcrCarea=explode("<div class='ocr_carea'", $ocrCarea);
    foreach($thisOcrCarea as $ocrPar)
        {
        $thisOcrPar=explode("<div class='ocr_par'", $ocrPar);
        foreach($thisOcrPar as $ocrLine)
            {
            $thisOcrLine=explode("<span class='ocr_line'", $ocrLine);
            foreach($thisOcrLine as $ocrWord)
                {
                $thisOcrWord=explode("<span class='ocrx_word'", $ocrWord);
                foreach($thisOcrWord as $item)
                    {
                    if(substr_count($item, "block_")>0)
                        {
                        $printYes=0;
                        $msg="Not";
                        }   
                    if(substr_count($item, "page_")>0)
                        {

                        $printYes=1;
                        $msg="Sivurivi ";

                        $thisItem=explode("page_", $item);
                        $thisItem=$thisItem[1];
                        $thisPageNr=explode("'", $thisItem);
                        $pageItem=strip_tags($thisPageNr[0]); // Jeh. It works.

                        }
                    if(substr_count($item, "line_")>0)
                        {
                        $printYes=1;
                        $msg="Rivirivi ";
                        $thisLineItem=explode("line_", $item);
                        $thisLineItem=$thisLineItem[1];
                        $thisLineNr=explode("'", $thisLineItem);
                        $lineItem=strip_tags($thisLineNr[0]);
                        }
                    if(substr_count($item, "word_")>0)
                        {
                        $printYes=1;
                        $msg="Sanarivi ";
                        $thisWordItem=explode("word_", $item);
                        $thisWordItem=$thisWordItem[1];
                        $thisWordNr=explode("'", $thisWordItem);
                        $item=$thisWordNr[0];
                        $restOfRow=$thisWordNr[1];
                        $lopRiv=explode("\"", $restOfRow);
                        $boxCoordsPart=$lopRiv[1];
                        $boxCoordsPart=explode(" ", $boxCoordsPart);
                        $boxCoordX1=$boxCoordsPart[1];
                        $boxCoordY1=$boxCoordsPart[2];
                        $boxCoordX2=$boxCoordsPart[3];
                        $boxCoordY2=$boxCoordsPart[4];
                        $theWordHere=strip_tags(substr($lopRiv[2], 1));
                        if(trim($theWordHere!=""))
                            {
                            $theWordHere=trim(str_replace("\n", "", $theWordHere));
                            }
                        else
                            {
                            $theWordHere=str_replace("\n", "", $theWordHere);   
                            }
                        $theWordHere=iconv('UTF-8', 'ISO-8859-15', $theWordHere);
                        $theWordHere=preg_replace("/[^0-9A-ZÅÄÖa-zåäö\-\/, .]/", "", $theWordHere);
                        $thisWordData=$pageItem."#".$lineItem."#".$item."#".$theWordHere."#".$boxCoordX1."#".$boxCoordY1."#".$boxCoordX2."#".$boxCoordY2."\r\n";
                        array_push($thisDocWords, $thisWordData);
                        }

                    }
                }
            }
        }
    }


// TESTING
$dbh_hocr->exec('BEGIN');

$stmt=$dbh_hocr->prepare("insert into invWords (FileName, PageNr, LineNr, WordNr, WordName, X1, Y1, X2, Y2) values (:FileName, :PageNr, :LineNr, :WordNr, :WordName, :X1, :Y1, :X2, :Y2)");

foreach($thisDocWords as $thisWordRowArr)
    {

    $thisWordRowArr=explode("#", $thisWordRowArr);

    $pageNumber=$thisWordRowArr[0];
    $lineNumber=$thisWordRowArr[1];
    $wordNumber=$thisWordRowArr[2];
    $wordName=$thisWordRowArr[3];
    $wordName=trim($wordName);
    $theFirstCharInWordName=substr(trim($wordName), 0, 1);
    if(!preg_match('/^[A-Za-zÅÄÖåäö0-9]/', $theFirstCharInWordName))
        {
        $wordName=substr($wordName, 1);
        }
    $theLastChar=substr($wordName, -1);

    if(!preg_match('/^[A-Za-zÅÄÖåäö0-9]/', $theLastChar))
        {
        $lengthOfWordNameToUse=strlen($wordName)-1;
        $wordName=substr($wordName, 0, $lengthOfWordNameToUse);
        }

    if($lineNumber<15 && substr($wordName, 0, 7)=="VUOKRAA")
        {
        $wordName="VUOKRAAJA";
        }

    $x1=$thisWordRowArr[4];
    $y1=$thisWordRowArr[5];
    $x2=$thisWordRowArr[6];
    $y2=$thisWordRowArr[7];


$stmt->bindValue(':FileName', $fileNameHere);
$stmt->bindValue(':PageNr', $pageNumber);
$stmt->bindValue(':LineNr', $lineNumber);
$stmt->bindValue(':WordNr', $wordNumber);
$stmt->bindValue(':WordName', $wordName);
$stmt->bindValue(':X1', $x1);
$stmt->bindValue(':Y1', $y1);
$stmt->bindValue(':X2', $x2);
$stmt->bindValue(':Y2', $y2);
$stmt->execute();
    } // END FOREACH

$dbh_hocr->exec('COMMIT');
?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.