Exemple #1
0
    def __call__(self, env):
        if not (self.status.user.protected or self.status.source in IGNORE_SOURCE):
            #twittbotの発言も学習してほしい? だ が 断 る
            chatter.extend_table(env.markovtable, self.text)
        
        if self.status.in_reply_to_status_id: #会話を学習する
            target = env.api.status(self.status.in_reply_to_status_id)
            env.association.learn(chatter.getelements(target.cleaned()),
                                  chatter.getelements(self.text))

        return True
Exemple #2
0
def get_response(env, status):
    """Generate response message from specified status.
    todo: これらの汚い条件文をpysocialbot.botlib.patternに置き換える
    todo: 返信やお気に入り、好感度増減などの処理をモナドにする
    """
    


    if not status.user.id in env.impression:
        env.impression[status.user.id] = 0
    
    def increment_impression(value):
        """increments the impression."""
        env.impression[status.user.id] += value
    
    def withimpression(value, increment=0):
        """returns the value and changes impression."""
        increment_impression(increment)
        return value
    
    def choice_i(data, els):
        """choices response by the impression."""
        for i in data:
            if env.impression[status.user.id] >= i[0]:
                if len(i) == 3:
                    increment_impression(i[2])
                return random.choice(i[1])
        
        if len(els) == 2:
            increment_impression(els[1])
        return random.choice(els[0])
    
    cleanedtext = status.cleaned()
    
    def check(pattern):
        return PATTERN[pattern].search(cleanedtext)

    isreply = bool(REPLY_REGEX.search(status.text))
    ismentions = isreply or bool(MENTION_REGEX.search(status.text))
    
    if isreply:
        if check("もしゃ") or check("もふ"):
            return choice_i([(15, ["@%(id)s きゃうん!",
                                   "@%(id)s きゃうん…",
                                   "@%(id)s きゃん!",
                                   "@%(id)s ……コンティニュー//",
                                   "@%(id)s ……つづけて//",
                                   "@%(id)s やめて…",], -1),
                             (4, ["@%(id)s きゃうん!",
                                  "@%(id)s きゃうん…",
                                  "@%(id)s きゃうん…",
                                  "@%(id)s きゃん!",
                                  "@%(id)s やめて",
                                  "@%(id)s だめ",
                                  "@%(id)s やめて、%(name)s氏"])
                             ],
                             (["@%(id)s やめて、%(name)s氏",
                               "@%(id)s %(name)s氏、やめて",
                               "@%(id)s やめて…",
                               "@%(id)s だめ",
                               "@%(id)s やめて"], 1))
        elif check("ぎゅ"):
            return choice_i([(20, ["@%(id)s きゃうん!",
                                  "@%(id)s きゃうっ!",
                                  "@%(id)s きゃん!",
                                  "@%(id)s やめて、%(name)s氏",
                                  "@%(id)s %(name)s氏、やめて"])],
                            (["@%(id)s やめて、%(name)s氏",
                              "@%(id)s %(name)s氏、やめて",
                              "@%(id)s やめて…","@%(id)s だめ",
                              "@%(id)s やめて"], -2))   
        elif check("なで"):
            choice_i([(3, ["@%(id)s ……", "@%(id)s つづけて"])],
                            (["@%(id)s 邪魔しないで", "@%(id)s 不要"], 0))
        elif check("ぺろ") or check("ちゅ"):
            return choice_i([(6, ["@%(id)s きゃうん!",
                                  "@%(id)s きゃうん…",
                                  "@%(id)s きゃん!",
                                  "@%(id)s やめて、%(name)s氏",
                                  "@%(id)s %(name)s氏、やめて"])
                                  ],
                            (["@%(id)s やめて、%(name)s氏",
                              "@%(id)s %(name)s氏、やめて",
                              "@%(id)s やめて…",
                              "@%(id)s やめて"], -2)) 
        elif check("早い"):
            return "@%(id)s UserStreamを使っているから"
        elif check("rm") or check("REC"):
            return random.choice(["@%(id)s Deny", "@%(id)s Permission Denied"])
        elif check("アイ"):
            return random.choice(["@%(id)s Iterationのi",
                                  "@%(id)s Indexのi",
                                  "@%(id)s Integerのi"])
        
        elif check("PC"):
            return "@%(id)s Dvorak配列で、キートップに何も記されていないノートPC"
        
        elif check("NagatoBot_End"):
            return None

    if status.in_reply_to_screen_name == None or \
    status.in_reply_to_screen_name == SCREEN_NAME:

        #フィボナッチ・サインの判定
        fib = lambda i: FIBONACCI_SIGN[i].search(status.text)
        if fib("dec"):
            return withimpression("@%(id)s 5", 2)
        elif fib("bin") or fib("bin3"):
            return withimpression("@%(id)s 101", 2)
        elif fib("bin4"):
            return withimpression("@%(id)s 0101", 2)
        elif fib("bin5"):
            return withimpression("@%(id)s 00101", 2)
        elif fib("oct"):
            return withimpression("@%(id)s 05", 2)
        elif fib("hex"):
            return withimpression("@%(id)s 0x5", 2)
        elif fib("hex2"):
            return withimpression("@%(id)s 0x05", 2)
        elif fib("jpn") or fib("jpn2"):
            return withimpression("@%(id)s ご", 2)

        if check("ちゃん"):
            env.api.favorite(status.id)
            return "@%(id)s 《ちゃん》は不要"

    if ismentions or status.in_reply_to_screen_name == None:
        elements = chatter.getelements(cleanedtext)
        if elements:
            assoc, score = env.association.extract(elements, random.randint(1, 4))
            assoc = [y for x, y in assoc]
            if ismentions or score >= len(elements) * RESPONSE_THRESHOLD:
                keywords = chatter.getkeywords(cleanedtext)
                text = chatter.greedygenerate(env.markovtable, assoc + keywords, True)
                if text:
                    return withimpression("@%(id)s " + text, 1)

    if check("リサ"): #自分に対する言及はふぁぼる
        env.api.favorite(status.id)
        return withimpression(None, 1)