2023-07-08 21:49:03 +00:00
|
|
|
from abc import abstractmethod, ABC
|
2023-05-06 21:55:00 +00:00
|
|
|
from bot.data import load_yomichan_inflection_categories
|
|
|
|
|
|
|
|
|
2023-07-27 04:48:24 +00:00
|
|
|
class BaseTerminator(ABC):
|
2023-05-06 21:55:00 +00:00
|
|
|
def __init__(self, target):
|
|
|
|
self._target = target
|
2023-04-23 05:34:46 +00:00
|
|
|
self._glossary_cache = {}
|
2023-05-01 22:31:28 +00:00
|
|
|
self._image_dir = None
|
2023-05-06 21:55:00 +00:00
|
|
|
categories = load_yomichan_inflection_categories()
|
|
|
|
self._inflection_categories = categories[target.value]
|
2023-05-01 22:31:28 +00:00
|
|
|
|
|
|
|
def set_image_dir(self, image_dir):
|
|
|
|
self._image_dir = image_dir
|
2023-04-23 01:26:54 +00:00
|
|
|
|
|
|
|
def make_terms(self, entry):
|
|
|
|
terms = []
|
|
|
|
headwords = entry.get_headwords()
|
|
|
|
for reading, expressions in headwords.items():
|
|
|
|
for expression in expressions:
|
|
|
|
definition_tags = self._definition_tags(entry)
|
|
|
|
inflection_rules = self._inflection_rules(entry, expression)
|
|
|
|
score = -len(terms)
|
|
|
|
glossary = self._glossary(entry)
|
|
|
|
sequence = self._sequence(entry)
|
2023-04-23 05:17:42 +00:00
|
|
|
term_tags = self._term_tags(entry)
|
2023-04-23 01:26:54 +00:00
|
|
|
term = [
|
|
|
|
expression, reading, definition_tags, inflection_rules,
|
|
|
|
score, glossary, sequence, term_tags
|
|
|
|
]
|
|
|
|
terms.append(term)
|
|
|
|
|
|
|
|
for x in self._link_glossary_parameters(entry):
|
|
|
|
(subentries, definition_tags) = x
|
|
|
|
if len(subentries) == 0:
|
|
|
|
continue
|
|
|
|
score = -len(terms)
|
|
|
|
glossary = self.__links_glossary(subentries)
|
|
|
|
term = [
|
|
|
|
expression, reading, definition_tags, inflection_rules,
|
|
|
|
score, glossary, sequence, term_tags
|
|
|
|
]
|
|
|
|
terms.append(term)
|
|
|
|
|
|
|
|
for subentries in self._subentry_lists(entry):
|
|
|
|
for subentry in subentries:
|
|
|
|
for term in self.make_terms(subentry):
|
|
|
|
terms.append(term)
|
|
|
|
return terms
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def __links_glossary(subentries):
|
|
|
|
glossary = []
|
|
|
|
for subentry in subentries:
|
|
|
|
exp = subentry.get_first_expression()
|
|
|
|
gloss = {
|
|
|
|
"type": "structured-content",
|
|
|
|
"content": {
|
|
|
|
"tag": "a",
|
|
|
|
"href": f"?query={exp}&wildcards=off",
|
|
|
|
"content": exp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glossary.append(gloss)
|
|
|
|
return glossary
|
2023-07-08 21:49:03 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def _definition_tags(self, entry):
|
2023-07-27 04:48:24 +00:00
|
|
|
raise NotImplementedError
|
2023-07-08 21:49:03 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def _inflection_rules(self, entry, expression):
|
2023-07-27 04:48:24 +00:00
|
|
|
raise NotImplementedError
|
2023-07-08 21:49:03 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def _glossary(self, entry):
|
2023-07-27 04:48:24 +00:00
|
|
|
raise NotImplementedError
|
2023-07-08 21:49:03 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def _sequence(self, entry):
|
2023-07-27 04:48:24 +00:00
|
|
|
raise NotImplementedError
|
2023-07-08 21:49:03 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def _term_tags(self, entry):
|
2023-07-27 04:48:24 +00:00
|
|
|
raise NotImplementedError
|
2023-07-08 21:49:03 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def _link_glossary_parameters(self, entry):
|
2023-07-27 04:48:24 +00:00
|
|
|
raise NotImplementedError
|
2023-07-08 21:49:03 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def _subentry_lists(self, entry):
|
2023-07-27 04:48:24 +00:00
|
|
|
raise NotImplementedError
|