from funcy import identity, compose, first, juxt, rest, rcompose from cv_analysis.utils import star, rconj from synthesis.random import rnd from synthesis.text.line_formatter.line_formatter import LineFormatter class ParagraphLineFormatter(LineFormatter): def __init__(self, blank_line_percentage=None): self.blank_line_percentage = blank_line_percentage or rnd.uniform(0, 0.5) def __call__(self, lines, last_full): return self.format_lines(lines, last_full) def format_lines(self, lines, last_full): def truncate_current_line(): return rnd.random() < self.blank_line_percentage and last_full # This is meant to be read from the bottom up. current_line_shall_not_be_a_full_line = truncate_current_line() line_formatter = self.truncate_line if current_line_shall_not_be_a_full_line else identity format_current_line = compose(line_formatter, first) move_current_line_to_back = star(rconj) split_first_line_from_lines_and_format_the_former = juxt(rest, format_current_line) split_off_current_line_then_format_it_then_move_it_to_the_back = rcompose( split_first_line_from_lines_and_format_the_former, move_current_line_to_back, ) current_line_is_a_full_line = not current_line_shall_not_be_a_full_line # Start reading here and move up. return split_off_current_line_then_format_it_then_move_it_to_the_back(lines), current_line_is_a_full_line def format_line(self, line, full=True): line = self.truncate_line(line) if not full else line return line, full def truncate_line(self, line: str): n_trailing_words = rnd.randint(0, 4) line = " ".join(line.split()[-n_trailing_words - 1 : -1]).replace(".", "") line = line + ".\n" if line else line return line