<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _extend_word(self, word, length, prefix=0, end=False, flatten=False):
 """ Extend the given word with a random suffix up to length. :param length: the length of the extended word; >= len(word); :param prefix: if greater than 0, the maximum length of the prefix to consider to choose the next character; :param end: if True, the generated word ends as a word of table; :param flatten: whether or not consider the table as flattened; :return: a random word of length generated from table, extending word. :raises GenerationError: if the generated word cannot be extended to length. """

        if len(word) == length:
            if end and "<" not in self[word[-1]]:
                raise GenerationError(word + " cannot be extended")
            else:
                return word
        else:  # len(word) < length
            exclude = {"<"}
            while True:
                choices = self.weighted_choices(word[-prefix
                                                     if prefix > 0
                                                     else 0:],
                                                exclude=exclude,
                                                flatten=flatten)
                if not choices:
                    raise GenerationError(word + " cannot be extended")
                # Extend with the weighted choice
                character = random_weighted_choice(choices)
                word += character
                try:
                    word = self._extend_word(word, length, prefix=prefix,
                                             end=end, flatten=flatten)
                    return word
                except GenerationError:
                    exclude.add(character)
                    word = word[:-1]