def test_pattern_save_and_load(): """Dictionary#pattern: 保存した辞書を読み込める""" word = '名詞' sentense = '名詞です' parts = analyze(sentense) d1 = Dictionary() d1.study_pattern(sentense, parts) d1.save() d2 = Dictionary() patterns = [ ptn for ptn in d2.pattern if re.search(ptn['pattern'], sentense) ] eq_(len(patterns), 1) ok_(patterns[0], {'pattern': word, 'phrases': [sentense]})
class TestDictionary: """Dictionaryオブジェクトのメソッドテスト""" def setup(self): self.dictionary = Dictionary() def teardown(self): remove_dic() def test_study_template_replace_nouns(self): """Dictionary#study_template: 形態素のリストを受け取り、名詞のみ%noun%に変換する""" parts = analyze('私はプログラムの女の子です') self.dictionary.study_template(parts) eq_(self.dictionary.template[3], ['%noun%は%noun%の%noun%です']) def test_study_template_count(self): """Dictionary#study_template: 名詞の数を辞書のインデックスにする""" parts = analyze('私はプログラムの女の子です') ok_(3 not in self.dictionary.template) self.dictionary.study_template(parts) ok_(3 in self.dictionary.template) def test_random(self): """Dictionary#random: デフォルトで['こんにちは']というリスト""" eq_(self.dictionary.random, ['こんにちは']) def test_study_random(self): """Dictionary#study_random: 発言を学習する""" sentense = 'Hello, World!' eq_(len(self.dictionary.random), 1) self.dictionary.study_random(sentense) eq_(len(self.dictionary.random), 2) def test_study_random_if_doubled(self): """Dictionary#study_random: 重複発言は学習しない""" sentense = 'Hello, World!' self.dictionary.study_random(sentense) eq_(len(self.dictionary.random), 2) self.dictionary.study_random(sentense) eq_(len(self.dictionary.random), 2) def test_study_template(self): """Dictionary#study_template: テンプレートを学習する""" eq_(len(self.dictionary.template), 0) parts = analyze('テンプレートを学習する') self.dictionary.study_template(parts) eq_(len(self.dictionary.template), 1) eq_(self.dictionary.template[2], ['%noun%を%noun%する']) def test_study_template_with_same_template(self): """Dictionary#study_template: 同じテンプレートは学習しない""" parts1 = analyze('テンプレートを学習する') parts2 = analyze('プログラムを作成する') self.dictionary.study_template(parts1) self.dictionary.study_template(parts2) eq_(len(self.dictionary.template), 1) eq_(self.dictionary.template[2], ['%noun%を%noun%する']) def test_study_template_without_nouns(self): """Dictionary#study_template: 名詞がなければ学習しない""" parts = analyze('実はさっきから寒い') self.dictionary.study_template(parts) eq_(len(self.dictionary.template), 0) def test_study_pattern(self): """Dictionary#study_pattern: 名詞の数だけパターンを学習する""" sentense = '名詞の数だけパターンを学習する' parts = analyze(sentense) self.dictionary.study_pattern(sentense, parts) eq_(len(self.dictionary.pattern), 4) nouns = ['名詞', '数', 'パターン', '学習'] for pattern in self.dictionary.pattern: ok_(pattern['pattern'] in nouns) eq_(pattern['phrases'], [sentense]) def test_study_pattern_without_nouns(self): """Dictionary#study_pattern: 名詞がなければ学習しない""" sentense = '実はさっきから寒い' parts = analyze(sentense) self.dictionary.study_pattern(sentense, parts) eq_(len(self.dictionary.pattern), 0) def test_study_pattern_with_same_word(self): """Dictionary#study_pattern: 同じ単語があれば追加する""" sentenses = ['波が立つ', '波が引く'] parts_per_sentense = [analyze(s) for s in sentenses] for sentense, parts in zip(sentenses, parts_per_sentense): self.dictionary.study_pattern(sentense, parts) eq_(len(self.dictionary.pattern), 1) eq_(self.dictionary.pattern[0]['pattern'], '波') for i, sentense in enumerate(sentenses): eq_(self.dictionary.pattern[0]['phrases'][i], sentense) def test_save(self): """Dictionary#save: 正常に保存できる""" self.dictionary.save() def test_study(self): """Dictionary#study: すべての辞書に学習させる""" sentense = '私はプログラムの女の子です' parts = analyze(sentense) self.dictionary.study(sentense, parts) eq_(len(self.dictionary.random), 2) # デフォルト + 1 eq_(len(self.dictionary.pattern), 3) # 名詞の数 eq_(len(self.dictionary.template), 1) # template[3] eq_(len(self.dictionary.markov._starts), 1) # _starts['私']