[WIP] random text segments

This commit is contained in:
Matthias Bisping 2023-01-16 17:55:20 +01:00
parent 4dfdd579a2
commit 41fdda4955

View File

@ -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)