Page MenuHomec4science

word_position.py
No OneTemporary

File Metadata

Created
Wed, Jun 5, 23:08

word_position.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" This class can be used to represent a word position.
"""
# Copyright (C) University of Basel 2019 {{{1
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/> 1}}}
__author__ = "Christian Steiner"
__maintainer__ = __author__
__copyright__ = 'University of Basel'
__email__ = "christian.steiner@unibas.ch"
__status__ = "Development"
__license__ = "GPL v3"
__version__ = "0.0.1"
from lxml import etree as ET
from .matrix import Matrix
from .positional_object import PositionalObject
from .writing_process import WritingProcess
class WordPosition(PositionalObject):
"""
This class represents a word position.
Args:
id (int): word id
matrix (datatypes.Matrix): matrix containing information about conversion.
height (float): height of word
width (float): width of word
x (float): x position of word
y (float): y position of word
tag (str) location of the word position: 'WordPosition.TRANSKRIPTION' (default) or 'WordPosition.FAKSIMILE'
"""
TRANSKRIPTION = 'transkription-position'
FAKSIMILE = 'faksimile-position'
XML_TAG = 'faksimile-position'
THRESHOLD_Y = 10
def __init__(self, id=0, node=None, text=None, height=0.0, width=0.0, x=0.0, y=0.0, matrix=None, tag=TRANSKRIPTION):
super(WordPosition, self).__init__(id=id, node=node, height=height, width=width, x=x, y=y, matrix=matrix, tag=tag)
self.intKeys.append('writing_process_id')
self.writing_process_id = -1
self.text = text
if node is not None:
self.writing_process_id = int(node.get('writing-process-id'))\
if bool(node.get('writing-process-id')) else -1
@classmethod
def copy_list_of_cls(cls, word_positions):
"""Return a copy of word_positions.
"""
return [ cls(id=wp.id, height=wp.height, width=wp.width, x=wp.left, y=wp.top, matrix=wp.transform)\
for wp in word_positions ]
def isOnTranskription(self):
"""Returns whether position is on transkription.
"""
return self.tag == self.TRANSKRIPTION
def isOnFaksimile(self):
"""Returns whether position is on transkription.
"""
return self.tag == self.FAKSIMILE
def __lt__(self, other):
if abs((self.top+self.bottom)/2 - (other.top+other.bottom)/2) < self.THRESHOLD_Y:
return self.left < other.left;
else:
return (self.top+self.bottom)/2 < (other.top+other.bottom)/2;
def __gt__(self, other):
return not self.__lt__(other)

Event Timeline