def random_word(corpus: list):
    """Return random word and syllable count from training corpus."""
    word = random.choice(corpus)
    num_syls = count_syllables(word)
    if num_syls > 4:
        return random_word(corpus)
    else:
        logging.debug(f"random word and syllables = {word} {num_syls}\n")
        return (word, num_syls)
def word_after_double(prefix: str, suffix_map_2: dict, current_syls: int,
                      target_syls: int) -> list:
    """Returns all acceptable words in a corpus that follow a word pair."""
    accepted_words = []
    suffixes = suffix_map_2.get(prefix)
    if suffixes != None:
        for candidate in suffixes:
            num_syls = count_syllables(candidate)
            if current_syls + num_syls <= target_syls:
                accepted_words.append(candidate)
    logging.debug(f'accepted words after "{prefix}" = {set(accepted_words)}\n')
    return accepted_words
def haiku_line(
    suffix_map_1: dict,
    suffix_map_2: dict,
    corpus: list,
    end_prev_line,
    target_syls: int,
):
    """Build a haiku line from a training corpus and return it."""
    line = "2/3"
    line_syls = 0
    current_line = []

    if len(end_prev_line) == 0:  # build first line
        line = "1"
        word, num_syls = random_word(corpus)
        current_line.append(word)
        line_syls += num_syls
        word_choices = word_after_single(word, suffix_map_1, line_syls,
                                         target_syls)
        while len(word_choices) == 0:
            prefix = random.choice(corpus)
            logging.debug(f"new random prefix = {prefix}\n")
            word_choices = word_after_single(prefix, suffix_map_1, line_syls,
                                             target_syls)
        word = random.choice(word_choices)
        num_syls = count_syllables(word)
        logging.debug(f"word and syllables {word}: {num_syls}\n")
        line_syls += num_syls
        current_line.append(word)
        if line_syls == target_syls:
            end_prev_line.extend(current_line[-2:])
            return current_line, end_prev_line

    else:  # build lines 2 and 3
        current_line.extend(end_prev_line)

    while True:
        logging.debug(f"line = {line}\n")
        prefix = current_line[-2] + " " + current_line[-1]
        word_choices = word_after_double(prefix, suffix_map_2, line_syls,
                                         target_syls)

        while len(word_choices) == 0:
            index = random.randint(0, len(corpus) - 2)
            prefix = corpus[index] + " " + corpus[index + 1]
            logging.debug(f"new random prefix = {prefix}")
            word_choices = word_after_double(prefix, suffix_map_2, line_syls,
                                             target_syls)

        word = random.choice(word_choices)
        num_syls = count_syllables(word)
        logging.debug(f"word and syllables = {word}: {num_syls}")

        if line_syls + num_syls > target_syls:
            continue
        elif line_syls + num_syls < target_syls:
            current_line.append(word)
            line_syls += num_syls
        elif line_syls + num_syls == target_syls:
            current_line.append(word)
            break

    end_prev_line = []
    end_prev_line.extend(current_line[-2:])

    if line == "1":
        final_line = current_line[:]
    else:
        final_line = current_line[2:]

    return final_line, end_prev_line