def test_construct_syllable(self): WordsData.words_data = self.SUCCESS_WORDS_TWELVE ChainsData.chains_data = self.SUCCESS_CHAINS_TWELVE haiker = Haiker() root = PhraseTree.define_root( possible_next_words=list(self.SUCCESS_CHAINS_TWELVE.keys())) self.assertIsInstance(haiker.construct_syllable(root, self.TWELVE), PhraseTree) pt = haiker.construct_syllable(root, self.TWELVE) self.assertEqual(PhraseTree.count_phrase_len(pt), self.TWELVE)
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
def test_count_phrase_len(self): WordsData(self.WORDS) word_tuple = ( Words( { "word": "古池", "vowel": "ウウイエ", "length": 4, "part": "名詞" } ), Words( { "word": "蛙", "vowel": "アエウ", "length": 3, "part": "名詞" } ), Words( { "word": "あ", "vowel": "ア", "length": 1, "part": "名詞" } ) ) root = PhraseTree.define_root(possible_next_words=list(self.SUCCESS_CHAINS.keys())) second_tree = PhraseTree(current_words=word_tuple, parent=root) root.next_tree = second_tree self.assertEqual( root.count_phrase_len(second_tree), 8 ) next_word_tuple = ( Words( { "word": "蛙", "vowel": "アエウ", "length": 3, "part": "名詞" } ), Words( { "word": "あ", "vowel": "ア", "length": 1, "part": "名詞" } ), Words( { "word": "いい", "vowel": "イイ", "length": 2, "part": "名詞" } ) ) third_tree = PhraseTree(next_word_tuple, parent=second_tree) self.assertEqual( PhraseTree.count_phrase_len(third_tree), 10 )