Exemplo n.º 1
0
class Bot():
    def __init__(self):
        self.dictionary = Dictionary()
        self.morph = Morph()

        self.resp_what = responder.WhatResponder(self.dictionary)
        self.resp_random = responder.RandomResponder(self.dictionary)
        self.resp_pattern = responder.PatternResponder(self.dictionary)
        self.resp_template = responder.TemplateResponder(self.dictionary)
        self.responder = self.resp_pattern

    def dialogue(self, input_text):
        parts = self.morph.analyze(input_text)

        i = random.randint(0, 100)
        if 0 <= i < 40:
            self.responder = self.resp_pattern
        elif 40 <= i < 70:
            self.response = self.resp_template
        elif 70 <= i < 90:
            self.responder = self.resp_random
        else:
            self.responder = self.resp_what

        self.response = self.responder.response(input_text, parts)

        #学習
        self.dictionary.study(input_text, parts)

        return self.response

    def save(self):
        self.dictionary.save()
Exemplo n.º 2
0
    def dialogue(self, input_text):
        self.emotion.update(input_text)
        tokens = Morph.analyze(input_text)

        number = randint(9)
        if number == 0:
            self.responder = self.responders['what']
        elif number >= 5:
            self.responder = self.responders['pattern']
        else:
            self.responder = self.responders['random']
        response = self.responder.response(input_text, self.emotion.mood)

        RandomResponder.dictionary.study(input_text, tokens)
        return response
Exemplo n.º 3
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)
Exemplo n.º 4
0
    def __add_sentense_bocchan(self):
        sample_file = '../KOISURU_PROGRAM/sample/markov/bocchan.txt'
        content = ''

        original_content = codecs.open(sample_file, 'r', 'shift_jis')
        for row in original_content:
            content += row.rstrip()
        original_content.close()
        texts = re.split(r'[。??!!  ]+', content)

        for text in texts:
            if text == '':
                continue
            tokens = Morph.analyze(text)
            self.markov.add_sentence(tokens)
            print('.', end='')
        print('')
Exemplo n.º 5
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)
Exemplo n.º 6
0
    def dialogue(self, input_text):
        self.emotion.update(input_text)
        tokens = Morph.analyze(input_text)

        number = randint(20)
        if number == 0:
            self.responder = self.responders['what']
        elif number <= 5:
            self.responder = self.responders['pattern']
        elif number <= 9:
            self.responder = self.responders['template']
        elif number <= 13:
            self.responder = self.responders['markov']
        elif number <= 17:
            self.responder = self.responders['search']
        else:
            self.responder = self.responders['random']
        response = self.responder.response(input_text, tokens,
                                           self.emotion.mood)

        RandomResponder.dictionary.study(input_text, tokens)
        return response