69 lines
1.8 KiB
Python
69 lines
1.8 KiB
Python
from abc import ABC, abstractmethod
|
|
|
|
|
|
class Entry(ABC):
|
|
ID_TO_ENTRY = {}
|
|
SUBENTRY_ID_TO_ENTRY_ID = {}
|
|
|
|
def __init__(self, target, entry_id):
|
|
if entry_id not in self.ID_TO_ENTRY:
|
|
self.ID_TO_ENTRY[entry_id] = self
|
|
else:
|
|
raise Exception(f"Duplicate entry ID: {entry_id}")
|
|
self.target = target
|
|
self.entry_id = entry_id
|
|
self._page = None
|
|
self._headwords = None
|
|
self._part_of_speech_tags = None
|
|
|
|
@abstractmethod
|
|
def get_global_identifier(self):
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def set_page(self, page):
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def get_page_soup(self):
|
|
raise NotImplementedError
|
|
|
|
def get_headwords(self):
|
|
if self._headwords is not None:
|
|
return self._headwords
|
|
headwords = self._get_headwords()
|
|
self._add_variant_expressions(headwords)
|
|
self._headwords = headwords
|
|
return headwords
|
|
|
|
@abstractmethod
|
|
def _get_headwords(self):
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def _add_variant_expressions(self, headwords):
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def get_part_of_speech_tags(self):
|
|
raise NotImplementedError
|
|
|
|
def get_parent(self):
|
|
if self.entry_id in self.SUBENTRY_ID_TO_ENTRY_ID:
|
|
parent_id = self.SUBENTRY_ID_TO_ENTRY_ID[self.entry_id]
|
|
parent = self.ID_TO_ENTRY[parent_id]
|
|
else:
|
|
parent = None
|
|
return parent
|
|
|
|
def get_first_expression(self):
|
|
headwords = self.get_headwords()
|
|
expressions = next(iter(headwords.values()))
|
|
expression = expressions[0]
|
|
return expression
|
|
|
|
def get_first_reading(self):
|
|
headwords = self.get_headwords()
|
|
reading = next(iter(headwords.keys()))
|
|
return reading
|