Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #4

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions application/app.py

This file was deleted.

16 changes: 16 additions & 0 deletions application/console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
from dialogue_system.bot import Bot


if __name__ == '__main__':
bot = Bot()

print('S: 料理のジャンルや場所をおっしゃってください。')
while True:
sent = input('U: ')
if sent == 'ありがとう':
print('S: どういたしまして')
break

reply = bot.reply(sent)
print('S: {0}'.format(reply))
26 changes: 26 additions & 0 deletions application/plugins/slack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-

from slackbot.bot import respond_to

from dialogue_system.bot import Bot

bots = {}


def create_or_read(user_id):
return bots[user_id] if user_id in bots else Bot()


def save_bot(bot, user_id):
bots[user_id] = bot


@respond_to('(.*)')
def food(message, something):
body = message.body
text, ts, user_id = body['text'], body['ts'], body['user']
bot = create_or_read(user_id)
reply_message = bot.reply(text)
save_bot(bot, user_id)
message.reply(reply_message)

34 changes: 34 additions & 0 deletions application/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
import tornado.ioloop
import tornado.web
import tornado.websocket

from dialogue_system.bot import Bot


class MessageServer(tornado.websocket.WebSocketHandler):
bots = {}

def check_origin(self, origin):
return True

def open(self):
print('on open')
self.bots[self] = Bot()
self.write_message('料理のジャンルや場所をおっしゃってください。')

def on_message(self, message):
print('on message')
print(message)
bot = self.bots[self]
self.write_message(bot.reply(message))

def on_close(self):
print('on close')
del self.bots[self]

application = tornado.web.Application([(r'/ws', MessageServer)])

if __name__ == '__main__':
application.listen(8080)
tornado.ioloop.IOLoop.current().start()
10 changes: 10 additions & 0 deletions application/slack_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
from slackbot.bot import Bot


def main():
bot = Bot()
bot.run()

if __name__ == "__main__":
main()
11 changes: 11 additions & 0 deletions application/slackbot_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
import os


API_TOKEN = os.environ.get('SLACK_API_KEY', '')

default_reply = "スイマセン。其ノ言葉ワカリマセン"

PLUGINS = [
'plugins',
]
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions dialogue_system/backend/apis/docomo_dialogue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
import os

from doco.client import Client


class DocomoDialogAPI(object):

def __init__(self, api_key=None):
api_key = os.environ.get('DOCOMO_DIALOGUE_API_KEY', api_key)
self.__client = Client(apikey=api_key)

def reply(self, text):
response = self.__client.send(utt=text, apiname='Dialogue')
utt = response['utt']

return utt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import os

import requests


Expand All @@ -8,7 +9,6 @@ class AreaNotFoundException(BaseException):


class HotPepperGourmetAPI(object):

BASE_URL = 'http://webservice.recruit.co.jp/hotpepper/{0}/v1/'

def __init__(self, api_key=None):
Expand Down Expand Up @@ -79,4 +79,4 @@ def search_food(self, **kwargs):

if __name__ == '__main__':
api = HotPepperGourmetAPI()
api.search_food()
api.search_food()
22 changes: 22 additions & 0 deletions dialogue_system/bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
from dialogue_system.dialogue_management.manager import DialogueManager
from dialogue_system.language_generation.generator import LanguageGenerator
from dialogue_system.language_understanding.language_understanding import RuleBasedLanguageUnderstanding


class Bot(object):

def __init__(self):
self.generator = LanguageGenerator()
self.language_understanding = RuleBasedLanguageUnderstanding()
self.manager = DialogueManager()

def reply(self, sent):
dialogue_act = self.language_understanding.execute(sent)

self.manager.update_dialogue_state(dialogue_act)
sys_act_type = self.manager.select_action(dialogue_act)

sent = self.generator.generate_sentence(sys_act_type)

return sent
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# -*- coding: utf-8 -*-
from modules.DialogueManagement.state import DialogueState
from modules.BackEnd.APIs.hotpepper import HotPepperGourmetAPI
from copy import deepcopy

from dialogue_system.dialogue_management.state import DialogueState
from dialogue_system.backend.apis.hotpepper import HotPepperGourmetAPI
from dialogue_system.backend.apis.docomo_dialogue import DocomoDialogAPI


class DialogueManager(object):
Expand All @@ -12,9 +15,13 @@ def update_dialogue_state(self, dialogue_act):
self.dialogue_state.update(dialogue_act)

def select_action(self, dialogue_act):
from copy import deepcopy
sys_act = deepcopy(dialogue_act)
if not self.dialogue_state.has('LOCATION'):
if dialogue_act['user_act_type'] == 'other':
api = DocomoDialogAPI()
reply = api.reply(dialogue_act['utt'])
sys_act['sys_act_type'] = 'CHAT'
sys_act['utt'] = reply
elif not self.dialogue_state.has('LOCATION'):
sys_act['sys_act_type'] = 'REQUEST_LOCATION'
elif not self.dialogue_state.has('GENRE'):
sys_act['sys_act_type'] = 'REQUEST_GENRE'
Expand All @@ -28,5 +35,6 @@ def select_action(self, dialogue_act):
restaurant = api.search_restaurant(area=area, food=food,budget=budget)
sys_act['sys_act_type'] = 'INFORM_RESTAURANT'
sys_act['restaurant'] = restaurant
self.dialogue_state.clear()

return sys_act
return sys_act
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def update(self, dialogue_act):
self.__state['MAXIMUM_AMOUNT'] = dialogue_act.get('MAXIMUM_AMOUNT', self.__state['MAXIMUM_AMOUNT'])

def has(self, name):
return self.__state[name] != None
return self.__state.get(name, None) != None

def get_area(self):
return self.__state['LOCATION']
Expand All @@ -23,6 +23,9 @@ def get_food(self):
def get_budget(self):
return self.__state['MAXIMUM_AMOUNT']

def clear(self):
self.__init__()

def __str__(self):
import pprint
return pprint.pformat(self.__state)
1 change: 1 addition & 0 deletions dialogue_system/knowledge/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__author__ = 'h-nakayama'
Loading