Replace hardcoded values in the diacritic detection code with config variables.

This commit is contained in:
Andrey Golovizin 2014-09-12 13:00:28 +02:00
parent 67c4edbe0a
commit 0e44ad8f6a
2 changed files with 36 additions and 19 deletions

View file

@ -20,6 +20,23 @@ from confire import Configuration as BaseConfiguration
class Configuration(BaseConfiguration): class Configuration(BaseConfiguration):
min_body_height = 10 min_body_height = 10
diacritic_box_left = -3
diacritic_box_right = +3
diacritic_box_top = -10
diacritic_box_bottom = +10
apostrophe_box_left = -5
apostrophe_box_right = +7
apostrophe_box_top = -5
apostrophe_box_bottom = +10
apostrophe_min_height = +3
apostrophe_max_distance = +4
dot_box_left = -3
dot_box_right = +3
dot_box_top = +1
dot_box_bottom = +10
@classmethod @classmethod
def load_file(cls, filename): def load_file(cls, filename):
class MyConfiguration(cls): class MyConfiguration(cls):

View file

@ -246,8 +246,6 @@ class Line(PageObject):
class Glyph(PageObject): class Glyph(PageObject):
MIN_BODY_HEIGHT = 10
def __init__(self, line, image, elevation): def __init__(self, line, image, elevation):
super().__init__(line.document, image) super().__init__(line.document, image)
self.elevation = elevation self.elevation = elevation
@ -255,7 +253,7 @@ class Glyph(PageObject):
def is_body(self): def is_body(self):
"""Return True if the glyph is definitely not diacritic.""" """Return True if the glyph is definitely not diacritic."""
return self.height >= self.MIN_BODY_HEIGHT return self.height >= self.config.min_body_height
def optical_distance(self, other): def optical_distance(self, other):
distance = other.left - self.right distance = other.left - self.right
@ -267,32 +265,34 @@ class Glyph(PageObject):
if not self.is_body(): if not self.is_body():
return False return False
#TODO remove hardcoded sizes
# diacritic above the letter # diacritic above the letter
if glyph.fits( if glyph.fits(
self.left - 3, self.left + self.config.diacritic_box_left,
self.top - 10, self.top + self.config.diacritic_box_top,
self.right + 3, self.right + self.config.diacritic_box_right,
self.top + 10, self.top + self.config.diacritic_box_bottom,
): ):
return True return True
# apostrophe, like in ť # apostrophe, like in ť
if glyph.fits( if (
self.right - 5, glyph.fits(
self.top - 5, self.right + self.config.apostrophe_box_left,
self.right + 7, self.top + self.config.apostrophe_box_top,
self.top + 10, self.right + self.config.apostrophe_box_right,
) and glyph.height > 3 and self.optical_distance(glyph) < 4: self.top + self.config.apostrophe_box_bottom,
)
and glyph.height > self.config.apostrophe_min_height
and self.optical_distance(glyph) < self.config.apostrophe_max_distance
):
return True return True
# dot in ? and ! # dot in ? and !
if glyph.fits( if glyph.fits(
self.left - 3, self.left + self.config.dot_box_left,
self.bottom + 1, self.bottom + self.config.dot_box_top,
self.right + 3, self.right + self.config.dot_box_right,
self.bottom + 10, self.bottom + self.config.dot_box_bottom,
): ):
return True return True