feat: generate sentences
This commit is contained in:
@@ -5,6 +5,8 @@ import typing
|
||||
|
||||
from PIL import Image, UnidentifiedImageError
|
||||
|
||||
from glsl_glyphs.sentences import Sentence
|
||||
|
||||
from .glyph import Glyph
|
||||
from .set import GlyphSet
|
||||
|
||||
@@ -21,15 +23,24 @@ class Reader:
|
||||
self.source_sets_path = (
|
||||
pathlib.Path(params.source_sets_path) if params.source_sets_path else None
|
||||
)
|
||||
self.source_sentences_path = (
|
||||
pathlib.Path(params.source_sentences_path)
|
||||
if params.source_sentences_path
|
||||
else None
|
||||
)
|
||||
self.load_sentences = params.source_sentences_path is not None
|
||||
self.log = logging.getLogger(__name__)
|
||||
|
||||
def read(self) -> tuple[list[Glyph], list[GlyphSet]]:
|
||||
def read(self) -> tuple[list[Glyph], list[GlyphSet], list[Sentence]]:
|
||||
glyphs = self.__read_image()
|
||||
sets = self.__read_sets()
|
||||
names = self.__read_names()
|
||||
sets = self.__read_sets()
|
||||
sentences = self.__read_sentences(names)
|
||||
|
||||
for glyph, name in zip(glyphs, names, strict=False):
|
||||
glyph.name = name
|
||||
return glyphs, sets
|
||||
|
||||
return glyphs, sets, sentences
|
||||
|
||||
def __read_image(self) -> list[Glyph]:
|
||||
try:
|
||||
@@ -49,7 +60,9 @@ class Reader:
|
||||
for y in range(img.height // 8):
|
||||
for x in range(img.width // 8):
|
||||
glyphs += [self.__get_img_glyph(img, x, y)]
|
||||
self.log.info("Loaded %d glyphs from %s", len(glyphs), self.source_image_path)
|
||||
self.log.info(
|
||||
"Loaded %d glyphs from %s", len(glyphs), self.source_image_path
|
||||
)
|
||||
except (OSError, UnidentifiedImageError) as e:
|
||||
self.log.error(
|
||||
"Could not read input image at: %s (%s)", self.source_sets_path, e
|
||||
@@ -90,7 +103,9 @@ class Reader:
|
||||
for line in self.source_sets_path.read_text().splitlines()[1:]:
|
||||
index, length, name = line.split(",")
|
||||
sets += [GlyphSet(index=int(index), length=int(length), name=name)]
|
||||
self.log.info("Extracted sets from %s", self.source_sets_path)
|
||||
self.log.info(
|
||||
"Extracted %d sets from %s", len(sets), self.source_sets_path
|
||||
)
|
||||
except ValueError, TypeError:
|
||||
self.log.error(
|
||||
"Could not read sets file at: %s (invalid format)",
|
||||
@@ -109,7 +124,9 @@ class Reader:
|
||||
if self.source_names_path:
|
||||
try:
|
||||
names = self.source_names_path.read_text().splitlines()
|
||||
self.log.info("Extracted names from %s", self.source_names_path)
|
||||
self.log.info(
|
||||
"Extracted %d names from %s", len(names), self.source_names_path
|
||||
)
|
||||
except OSError as e:
|
||||
self.log.warning(
|
||||
"Could not to read names file at: %s (%s)",
|
||||
@@ -119,3 +136,27 @@ class Reader:
|
||||
else:
|
||||
self.log.info("No names file provided")
|
||||
return names
|
||||
|
||||
def __read_sentences(self, names: list[str]) -> list[Sentence]:
|
||||
sentences = []
|
||||
|
||||
if self.source_sentences_path:
|
||||
try:
|
||||
lines = self.source_sentences_path.read_text().splitlines()
|
||||
if len(lines) and len(names) == 0:
|
||||
self.log.error("Cannot extract sentences without names")
|
||||
sys.exit(1)
|
||||
for line in lines:
|
||||
sentences += [Sentence.from_text(line, names)]
|
||||
self.log.info(
|
||||
"Extracted %d sentences from %s",
|
||||
len(sentences),
|
||||
self.source_names_path,
|
||||
)
|
||||
except OSError as e:
|
||||
self.log.warning(
|
||||
"Could not to read names file at: %s (%s)",
|
||||
self.source_names_path,
|
||||
e,
|
||||
)
|
||||
return sentences
|
||||
|
||||
Reference in New Issue
Block a user