[WIP] random text segments
This commit is contained in:
parent
4dfdd579a2
commit
41fdda4955
21
test/fixtures/page_generation/page.py
vendored
21
test/fixtures/page_generation/page.py
vendored
@ -1,5 +1,6 @@
|
|||||||
import random
|
import random
|
||||||
import textwrap
|
import textwrap
|
||||||
|
from functools import lru_cache
|
||||||
from typing import Tuple, Union, Iterable, List
|
from typing import Tuple, Union, Iterable, List
|
||||||
|
|
||||||
import albumentations as A
|
import albumentations as A
|
||||||
@ -69,7 +70,7 @@ Image_t = Union[Image.Image, np.ndarray]
|
|||||||
# ],
|
# ],
|
||||||
# p=0.5,
|
# 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
|
from cv_analysis.locations import TEST_PAGE_TEXTURES_DIR
|
||||||
|
|
||||||
@ -368,16 +369,16 @@ class RandomTextBlock(ContentRectangle):
|
|||||||
self.content = image
|
self.content = image
|
||||||
|
|
||||||
def f(self, last_full, lines):
|
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:
|
fn = compose(append, juxt(rest, compose(line_processor, first)))
|
||||||
line = self.truncate_line(line)
|
|
||||||
last_full = False
|
|
||||||
else:
|
|
||||||
last_full = True
|
|
||||||
|
|
||||||
return last_full, [*lines[1:], line]
|
return (False, fn(lines)) if truncate() else (True, fn(lines))
|
||||||
|
|
||||||
def format_line(self, line, full=True):
|
def format_line(self, line, full=True):
|
||||||
line = self.truncate_line(line) if not full else line
|
line = self.truncate_line(line) if not full else line
|
||||||
@ -390,6 +391,10 @@ class RandomTextBlock(ContentRectangle):
|
|||||||
return line
|
return line
|
||||||
|
|
||||||
|
|
||||||
|
def rconj(xs, x):
|
||||||
|
return [*xs, x]
|
||||||
|
|
||||||
|
|
||||||
def generate_random_text_block(rectangle: Rectangle) -> ContentRectangle:
|
def generate_random_text_block(rectangle: Rectangle) -> ContentRectangle:
|
||||||
block = RandomTextBlock(*rectangle.coords)
|
block = RandomTextBlock(*rectangle.coords)
|
||||||
block.generate_random_text(rectangle)
|
block.generate_random_text(rectangle)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user