From 41fdda4955a394f26eeedd8578be4e86daddc259 Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Mon, 16 Jan 2023 17:55:20 +0100 Subject: [PATCH] [WIP] random text segments --- test/fixtures/page_generation/page.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/test/fixtures/page_generation/page.py b/test/fixtures/page_generation/page.py index 392f6e5..ace215e 100644 --- a/test/fixtures/page_generation/page.py +++ b/test/fixtures/page_generation/page.py @@ -1,5 +1,6 @@ import random import textwrap +from functools import lru_cache from typing import Tuple, Union, Iterable, List import albumentations as A @@ -69,7 +70,7 @@ Image_t = Union[Image.Image, np.ndarray] # ], # p=0.5, # ) -from funcy import juxt, compose, identity, lflatten, lmap, first, iterate, take, last +from funcy import juxt, compose, identity, lflatten, lmap, first, iterate, take, last, rest from cv_analysis.locations import TEST_PAGE_TEXTURES_DIR @@ -368,16 +369,16 @@ class RandomTextBlock(ContentRectangle): self.content = image def f(self, last_full, lines): + @lru_cache(maxsize=None) + def truncate(): + return random.random() < self.blank_line_percentage and last_full - line = lines[0] + line_processor = self.truncate_line if truncate() else identity + append = star(rconj) - if random.random() < self.blank_line_percentage and last_full: - line = self.truncate_line(line) - last_full = False - else: - last_full = True + fn = compose(append, juxt(rest, compose(line_processor, first))) - return last_full, [*lines[1:], line] + return (False, fn(lines)) if truncate() else (True, fn(lines)) def format_line(self, line, full=True): line = self.truncate_line(line) if not full else line @@ -390,6 +391,10 @@ class RandomTextBlock(ContentRectangle): return line +def rconj(xs, x): + return [*xs, x] + + def generate_random_text_block(rectangle: Rectangle) -> ContentRectangle: block = RandomTextBlock(*rectangle.coords) block.generate_random_text(rectangle)