-- Copyright 2006-2007 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz -- or its licensors, as applicable. -- -- You may not use this file except under the terms of the accompanying license. -- -- Licensed under the Apache License, Version 2.0 (the "License"); you -- may not use this file except in compliance with the License. You may -- obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 -- -- Unless required by applicable law or agreed to in writing, software -- distributed under the License is distributed on an "AS IS" BASIS, -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -- See the License for the specific language governing permissions and -- limitations under the License. -- -- Project: ocroscript -- File: echo.lua -- Purpose: echo output -- Responsible: mezhirov -- Reviewer: -- Primary Repository: -- Web Sites: www.iupr.org, www.dfki.de, www.ocropus.org ------------------------------------------------------------------------------- local function imitate_class(class, base) class.__base__ = base class.new = function(self, ...) local object = {} self:__init__(object, ...) return object end class.__init__ = function(class, object, ...) if class.__base__ then class.__base__:__init__(object, ...) end for key, value in pairs(class) do object[key] = value end if object.create then object:create(...) end end setmetatable(class,{__call = class.new}) -- make C() be the same as C:new() end ------------------------------------------------------------------------------- local function output_hOCR_meta(header, meta, default) local t = header[meta] if not t then t = default end print(string.format(' ', meta, t)) end local function output_hOCR_title(header, default) local t = header.title if not t then t = default end print(string.format(' %s', t)) end local function output_hOCR_char(c --[[integer]]) if c == string.byte('&') then io.write('&') elseif c == string.byte('<') then io.write('<') elseif c == string.byte('>') then io.write('>') elseif c > 127 then io.write(string.format('&#%d;', c)) else io.write(string.char(c)) end end ------------------------------------------------------------------------------- TreeNode = {} function TreeNode:append(node) table.insert(self, node) end imitate_class(TreeNode) ------------------------------------------------------------------------------- DocumentNode = {} function DocumentNode:hocr_output() local header = self.header or {} print'' print'' print'' print ' ' print " " print " " print ' ' -- KT replaces: -- print '' -- print '' -- output_hOCR_meta(header, 'system', 'OCRopus' .. get_version_string()) -- KT write our own stuff later here -- output_hOCR_meta(header, 'capabilities', 'ocr_line ocr_page') -- output_hOCR_meta(header, 'langs', 'en') -- output_hOCR_meta(header, 'scripts', 'Latn') -- output_hOCR_meta(header, 'microformats', '') -- output_hOCR_title(header, 'OCR Output') for i = 1, #self do self[i]:hocr_output(self) end print "

" -- KT: a bit of a makeshift solution print "
" print "
" end imitate_class(DocumentNode, TreeNode) ------------------------------------------------------------------------------- -- Fields: -- description -- width -- height -- paragraphs -- headings -- time_report PageNode = {} local pagecounter = 1 -- count the pages function PageNode:hocr_output(document --[[unused, but might be used later]]) print "" -- here is maybe the solution to the problem: each new page starts with paragraph = false -- we should introduce a counter which only falsifies this if it is the first time local paragraphs = self.paragraphs if not paragraphs then paragraphs = {} end local headings = self.headings if not headings then headings = {} end print(string.format('', pagecounter)) if pagecounter==1 then local paragraph = false end if self.width and self.height then print(string.format( --kt: was'
', '', self.description, pagecounter, self.width, self.height)) else print(string.format('', -- self.description)) end for line=1, #self do if paragraphs[line] --[[formerly: page:isParagraph(line)]] then -- this if closes paragraphs if paragraph then print '

' end -- kt print('

') print('

') paragraph = true end local text = nustring() if not headings[line] then self[line]:hocr_output(self) else -- here starts the headings bit if paragraph then -- close the paragraph print '

' end if line == 1 or not headings[line - 1] then print '' end self[line]:hocr_output() -- print the headings line if line == #self or not headings[line + 1] then print '' -- kt Adding beginning of paragraph print '

' paragraph = true -- KT added this end -- kt setting this true. What happens? -- paragraph = true -- paragraph = false end -- kt for loop ends here: end -- KT we need to find another way if paragraph then -- close final paragraph -- -- print '

' end if self.time_report then print(string.format('', self.time_report)) end --KT: ending of page? -- print '
' local n = 1 pagecounter = pagecounter + n end imitate_class(PageNode, TreeNode) ------------------------------------------------------------------------------- LineNode = {} function LineNode:create(bbox, text) self.bbox = bbox self.text = text end function LineNode:hocr_output(page) local line = self.text local bbox = self.bbox if not line or line:length() == 0 then return end local bbox_string = '' if page and bbox then bbox_string = string.format(' bbox="%d %d %d %d"', -- kt changed string format bbox.x0, -- left page.height - bbox.y1, -- top bbox.x1, -- right page.height - bbox.y0) -- bottom end -- KT: removed, put it into lb milestone -- io.write(string.format('', bbox_string)) for i = 0, line:length() - 2 do output_hOCR_char(line:at(i):ord()) end if remove_hyphens and line:length() > 0 and line.at(line:length() - 1).ord() == string.byte('-') then -- KT replace -- io.write '' io.write(string.format('', bbox_string)) else if line:length() > 0 then output_hOCR_char(line:at(line:length()-1):ord()) end -- print '' print(string.format(' ', bbox_string)) end end imitate_class(LineNode, TreeNode)