예제 #1
0
    def construct_syllable(self, phrase_tree, expected_vowel_len, noun=False):
        """
        最初の音節を作成する.

        Args:
          phrase_tree: PhraseTree クラスオブジェクト
          expected_vowel_len: 音節の読みの長さ
        Returns:
          音節
        """
        # phrase_tree には,current と next が格納済み
        while True:
            if self._is_empty_first_node(phrase_tree):
                raise ConstructionError('First node has become empty.')
            while True:
                # 単語タプルをランダムに1つ取得
                try:
                    PhraseTree.disclose(phrase_tree, "construct ---------")
                    phrase_tree, phrase_tree.next_tree = self._get_word(
                        phrase_tree)
                    PhraseTree.disclose(phrase_tree, "construct 2 ---------")
                except ValueError:
                    logger.info('*** Cnstruction failed ***')
                    raise
                if phrase_tree.next_tree is None:
                    break

                text_vowel_len = PhraseTree.count_phrase_len(
                    phrase_tree.next_tree)

                if phrase_tree.possible_next_words == list():
                    try:
                        phrase_tree = self._back_prev_word_list(phrase_tree)
                    except ValueError:
                        logger.info('*** Cnstruction failed ***')
                        raise
                elif self._is_n_char(expected_vowel_len, text_vowel_len):
                    assert phrase_tree is not None, "phrase_tree is None"
                    self._post_proc(phrase_tree.next_tree)
                    return phrase_tree.next_tree
                elif self._is_less_than_n_char(expected_vowel_len,
                                               text_vowel_len):
                    # 次の単語タプルを取得する前の処理
                    phrase_tree = phrase_tree.next_tree
                    assert phrase_tree is not None, "phrase_tree is None"
                    break
                else:
                    # 5文字より長くなったら別の単語を取得しなおす
                    # 現在の next_tree を破棄する
                    pass
예제 #2
0
    def compose(self):
        """
        俳句を詠む.
        """
        first_loop_limit = 500
        first_current_loop = 0
        while True:
            logger.debug('+++ current loop: {} +++'.format(first_current_loop))

            first_current_loop += 1
            if first_current_loop == first_loop_limit:
                return "***** だめでした *****"

            try:
                root = PhraseTree.define_root(
                    possible_next_words=self._get_word_list())
                first_phrase_tree = self.construct_syllable(root, self.FIVE)
                first_text_list = Phrase.text_list
                PhraseTree.disclose(first_phrase_tree, "first -------------")
            except ValueError:
                logger.debug('***** first ValueError *****')
                continue

            try:
                root = PhraseTree.define_root(
                    possible_next_words=first_phrase_tree.possible_next_words)
                second_phrase_tree = self.construct_syllable(root, self.SEVEN)
                second_last_vowel = Phrase.last_vowel
                second_text_list = Phrase.text_list
                PhraseTree.disclose(second_phrase_tree, "second -------------")
            except ValueError:
                logger.debug('***** second ValueError *****')
                continue

            try:
                root = PhraseTree.define_root(
                    possible_next_words=second_phrase_tree.possible_next_words)
                third_phrase_tree = self.construct_syllable(root, self.FIVE)
                third_last_vowel = Phrase.last_vowel
                third_text_list = Phrase.text_list
                PhraseTree.disclose(third_phrase_tree, "third -------------")
            except ValueError:
                logger.debug('***** third ValueError *****')
                continue

            if Rhymer.is_rhymed(second_last_vowel, third_last_vowel):
                haiku = "".join(first_text_list + second_text_list +
                                third_text_list)
                logger.debug("[Haiku] >>> {}".format(haiku))

                return haiku