# -*- coding: utf-8 -*
"""トルネードを使用した ask.api を作成します."""
from json import dumps

from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.options import parse_command_line
from tornado.web import Application, RequestHandler

from tornado.options import define, options
from tokenizer import get_entity

from logger import getLogger

LOGGER = getLogger('API_MODULE')
define("port", default=8000, help="run on the given port", type=int)


class AskHandler(RequestHandler):
    """question に get された文章と親密度の高い語を返します."""

    def get(self):
        """Question に答えます."""
        from ask import ask
        author = self.get_argument('author')
        question = self.get_argument('question')
        answers = {
            'answers': ask(author, get_entity(question))
        }
        self.finish(
            dumps(
                answers,
                ensure_ascii=False,
                indent=4,
                sort_keys=True,
                separators=(',', ': ')
            )
        )

    def post(self):
        """Action on google の web フック用レスポンス"""
        from ask import ask
        import json
        data = json.loads(self.request.body)
        LOGGER.info('input: {data}'.format(data=data))

        author = data.get('author', '夏目漱石')
        question = data.get('question')
        answers = ask(author, get_entity(question))

        if answers:
            adjective = answers.get('adjective', None)
            nouns = answers.get('nouns')

            if adjective:
                speech = '。'.join([
                    'それは {adjective} 質問ですね'.format(adjective=adjective[0]),
                    'きっと, {0} や {1} あるいは {2} のことです'.format(*nouns)
                ])
            else:
                speech = 'それはきっと, {0} や {1} あるいは {2} のことです'.format(*nouns)
        else:
            speech = '。'.join([
                '{q} についてですか'.format(q=question),
                '難しいことを聞きますね',
                '私にはわからないです'
            ])

        displayText = speech
        respose = {
            'speech': speech,
            'displayText': displayText,
            'data': answers,
            'contextOut': [answers],
            'source': 'how-about-natume'
        }
        self.finish(
            dumps(
                respose,
                ensure_ascii=False,
                indent=4,
                sort_keys=True,
                separators=(',', ': ')
            )
        )



if __name__ == "__main__":
    parse_command_line()
    app = Application(handlers=[(r"/", AskHandler)])
    http_server = HTTPServer(app)
    http_server.listen(options.port)
    IOLoop.instance().start()

[((15, 9, 15, 32), 'logger.getLogger', 'getLogger', ({(15, 19, 15, 31): '"""API_MODULE"""'}, {}), "('API_MODULE')", False, 'from logger import getLogger\n'), ((16, 0, 16, 68), 'tornado.options.define', 'define', (), '', False, 'from tornado.options import define, options\n'), ((90, 4, 90, 24), 'tornado.options.parse_command_line', 'parse_command_line', ({}, {}), '()', False, 'from tornado.options import parse_command_line\n'), ((91, 10, 91, 52), 'tornado.web.Application', 'Application', (), '', False, 'from tornado.web import Application, RequestHandler\n'), ((92, 18, 92, 33), 'tornado.httpserver.HTTPServer', 'HTTPServer', ({(92, 29, 92, 32): 'app'}, {}), '(app)', False, 'from tornado.httpserver import HTTPServer\n'), ((44, 15, 44, 44), 'json.loads', 'json.loads', ({(44, 26, 44, 43): 'self.request.body'}, {}), '(self.request.body)', False, 'import json\n'), ((31, 12, 37, 13), 'json.dumps', 'dumps', (), '', False, 'from json import dumps\n'), ((49, 30, 49, 50), 'tokenizer.get_entity', 'get_entity', ({(49, 41, 49, 49): 'question'}, {}), '(question)', False, 'from tokenizer import get_entity\n'), ((78, 12, 84, 13), 'json.dumps', 'dumps', (), '', False, 'from json import dumps\n'), ((94, 4, 94, 21), 'tornado.ioloop.IOLoop.instance', 'IOLoop.instance', ({}, {}), '()', False, 'from tornado.ioloop import IOLoop\n'), ((28, 35, 28, 55), 'tokenizer.get_entity', 'get_entity', ({(28, 46, 28, 54): 'question'}, {}), '(question)', False, 'from tokenizer import get_entity\n')]