Skip to content
Snippets Groups Projects
Commit 63fdca1a authored by Vít Starý Novotný's avatar Vít Starý Novotný
Browse files

Take word-level language annotations in HOCR output into account

parent 40472280
No related branches found
No related tags found
No related merge requests found
# -*- coding:utf-8 -*-
from collections import defaultdict
import csv
from itertools import repeat, chain
import json
......@@ -692,13 +693,20 @@ def _read_page_languages_json(f):
def _read_page_languages_hocr(f):
html5_parser = etree.HTMLParser(huge_tree=True)
xml_document = etree.parse(f, html5_parser)
languages = dict()
languages = defaultdict(lambda: 0.0)
def get_confidence(element):
return float(len(''.join(element.itertext())))
for paragraph in xml_document.xpath('//p[@lang]'):
language_code = paragraph.attrib['lang']
confidence = float(len(''.join(paragraph.itertext())))
if language_code not in languages:
languages[language_code] = 0.0
languages[language_code] += confidence
paragraph_language_code = paragraph.attrib['lang']
paragraph_confidence = get_confidence(paragraph)
for word in paragraph.xpath('//span[@class="ocrx_word" and @lang]'):
word_language_code = word.attrib['lang']
word_confidence = get_confidence(word)
languages[word_language_code] += word_confidence
paragraph_confidence -= word_confidence
languages[paragraph_language_code] += paragraph_confidence
return languages
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment