
docomo の Developer Supportで雑談対話APIなどという面白そうなAPIが公開されいたので、Twitterの雑談BOTの作者として黙っていられず、早速試しに使ってみました。
新規登録を行って、マイページから使いたいAPIの利用申請を行うことで、申請から90日は開発用として自由に使うことができます。それ以降は再度申請を行うか、商用利用の申請を行うことで再び使えるようです。
早速チャットを作って会話をしてみました。
どこも : 声かけてくれるのを待ってたんだ。
あなた : こんばんはー
どこも : こんありです
あなた : 晩御飯はカレーだったよ
どこも : カレー味はいいにおいするしね
あなた : 本格的なやつです
どこも : クセがあるの好きですよ
あなた : 今日のニュース見た?
どこも : うーんにおいよくわかんない
あなた : もうその話じゃないって
どこも : 好き好きがあるのです
あなた : バイバイ
どこも : ばいばーい
噛み合っているような、噛み合っていないような。話題をきちんと覚えているあたり、SimSimiよりは賢いかなって感じがします。何より、AIによる雑談の難しさを感じますね。あなた : こんばんはー
どこも : こんありです
あなた : 晩御飯はカレーだったよ
どこも : カレー味はいいにおいするしね
あなた : 本格的なやつです
どこも : クセがあるの好きですよ
あなた : 今日のニュース見た?
どこも : うーんにおいよくわかんない
あなた : もうその話じゃないって
どこも : 好き好きがあるのです
あなた : バイバイ
どこも : ばいばーい
ちなみにしりとりもできます。
どこも : 食べ物が美味しい季節だね。
あなた : しりとりしよう
どこも : はい、やりましょう。それでは、ロボットのトからスタートです。
あなた : 鳥取
どこも : 理想
あなた : 馬の耳に念仏
どこも : つけ
あなた : 結膜炎
どこも : ンで終わりましたね。私の勝ちです。
あなた : 負けたー
どこも : かった
あなた : バイバイ
どこも : おやすみ
あなた : しりとりしよう
どこも : はい、やりましょう。それでは、ロボットのトからスタートです。
あなた : 鳥取
どこも : 理想
あなた : 馬の耳に念仏
どこも : つけ
あなた : 結膜炎
どこも : ンで終わりましたね。私の勝ちです。
あなた : 負けたー
どこも : かった
あなた : バイバイ
どこも : おやすみ
以下がソースコードです。ニックネームの他に性別や年齢なども登録できるみたいです。
細かい仕様は雑談対話APIのリファレンスをご参照下さい。
$ vi chat.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
u"""Docomoの雑談対話APIを使ってチャットできるスクリプト
"""
import sys
import urllib2
import json
APP_URL = 'https://api.apigw.smt.docomo.ne.jp/dialogue/v1/dialogue'
class DocomoChat(object):
u"""Docomoの雑談対話APIでチャット"""
def __init__(self, api_key):
super(DocomoChat, self).__init__()
self.api_url = APP_URL + '?APIKEY=%s'%(api_key)
self.context, self.mode = None, None
def __send_message(self, input_message='', custom_dict=None):
req_data = {'utt': input_message}
if self.context:
req_data['context'] = self.context
if self.mode:
req_data['mode'] = self.mode
if custom_dict:
req_data.update(custom_dict)
request = urllib2.Request(self.api_url, json.dumps(req_data))
request.add_header('Content-Type', 'application/json')
try:
response = urllib2.urlopen(request)
except Exception as e:
print e
sys.exit()
return response
def __process_response(self, response):
resp_json = json.load(response)
self.context = resp_json['context'].encode('utf-8')
self.mode = resp_json['mode'].encode('utf-8')
return resp_json['utt'].encode('utf-8')
def send_and_get(self, input_message):
response = self.__send_message(input_message)
received_message = self.__process_response(response)
return received_message
def set_name(self, name, yomi):
response = self.__send_message(custom_dict={'nickname': name, 'nickname_y': yomi})
received_message = self.__process_response(response)
return received_message
def main():
api_key = 'APIキーをここに記入'
chat = DocomoChat(api_key)
resp = chat.set_name('あなたのニックネーム', 'ニックネームのヨミガナ')
print '相手 : %s'%(resp)
message = ''
while message != 'バイバイ':
message = raw_input('あなた : ')
resp = chat.send_and_get(message)
print '相手 : %s'%(resp)
if __name__ == '__main__':
main()
ウェブアプリやAndroidアプリのオマケ機能として、キャラクターに喋らせる時に使えそうですね。
自分のBOTも負けないように頑張らないと!
0 件のコメント:
コメントを投稿