예제 #1
0
 def response(self, input_text='', tokens=[], mood=0):
     keyword = ''
     for token in tokens:
         if Morph.is_keyword(token):
             keyword = token.surface
     generated = self.dictionary.markov.generate(keyword)
     return generated if ((generated is not None) and
                          (len(generated) > 0)) else random.choice(
                              self.dictionary.random)
예제 #2
0
 def study_pattern(self, input_text, tokens):
     for token in tokens:
         if not Morph.is_keyword(token):
             continue
         word = token.surface
         duped = self.find_pattern(word, input_text)
         if duped != None:
             duped.phrases.append({'need': 0, 'phrase': input_text})
         else:
             self.pattern.append(PatternItem(word, '0##' + input_text))
예제 #3
0
 def response(self, input_text='', tokens=[], mood=0):
     keywords = []
     for token in tokens:
         if Morph.is_keyword(token):
             keywords.append(token.surface)
     count = len(keywords)
     if count > 0 and count in self.dictionary.template.keys():
         template = random.choice(self.dictionary.template[count])
         for keyword in keywords:
             template = template.replace('%noun%', keyword, 1)
         return template
     return random.choice(self.dictionary.random)
예제 #4
0
    def response(self, input_text='', tokens=[], mood=0):
        try:
            keyword = ''
            for token in tokens:
                if Morph.is_keyword(token):
                    keyword += token.surface + ' '

            if len(keyword) > 0:
                sentence = Search.get_sentence(keyword)
                self.dictionary.study_markov(sentence)
                return sentence
        except:
            print('*** error ***')
        return random.choice(self.dictionary.random)
예제 #5
0
 def study_template(self, tokens):
     template = ''
     count = 0
     for token in tokens:
         word = token.surface
         if Morph.is_keyword(token):
             word = '%noun%'
             count += 1
         template += word
     if count == 0:
         return
     if not count in self.template.keys():
         self.template[count] = []
     if not template in self.template[count]:
         self.template[count].append(template)
예제 #6
0
    def test_get_sentence(self):
        input_texts = ['こんにちは', 'ジブリが好きです', 'ディズニーが好きです', 'ピクサーが好きです']

        for input_text in input_texts:
            with self.subTest():
                tokens = Morph.analyze(input_text)
                keyword = ''
                for token in tokens:
                    if Morph.is_keyword(token):
                        keyword += token.surface + ' '
                sentence = Search.get_sentence(keyword)
                print('you > ' + input_text)
                print('keyword > ' + keyword)
                print('sentence > ' + sentence)
                print('************')
                self.assertTrue(len(sentence) > 0)
예제 #7
0
    def test_generate(self):
        self.__add_sentense_bocchan()
        input_texts = [
            '初めまして、坊ちゃん', 'あら、ご病気ですか', 'あらあら、大変ですね', 'いたずらして病気になっちゃったんですか?',
            'そんな威張らなくてもいいでしょう', 'はあ、そんなもんですか', '遅刻しちゃだめですね', 'よく覚えてないんですか?',
            'ターナー?', 'どなたですか?'
        ]

        for input_text in input_texts:
            with self.subTest():
                tokens = Morph.analyze(input_text)
                keyword = 'N/A'
                for token in tokens:
                    if Morph.is_keyword(token):
                        keyword = token.surface
                generated = self.markov.generate(keyword)
                print('you > ' + input_text)
                print('generated > ' + generated)
                print('************')
                self.assertTrue(len(generated) > 0)