Gooラボ on Heroku

Sample implementation of Goolabs API on Heroku


# coding:utf-8
# Sample of using Goolab as a Web Service (Flask)
from flask import Flask, json, jsonify, request, render_template, flash, redirect, url_for, Response
import requests
from goolabs import GoolabsAPI
import traceback
import sys
import logging
import os
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.ERROR)
app.secret_key = 'some_secret'
#app.config['JSON_AS_UTF-8'] = False
app_id = "<APP_ID_HERE>"
api = GoolabsAPI(app_id)
url_shortsum = "https://labs.goo.ne.jp/api/shortsum"
url_entity = "https://labs.goo.ne.jp/api/entity"
url_similarity = "https://labs.goo.ne.jp/api/similarity"
url_keyword = "https://labs.goo.ne.jp/api/keyword"
url_chrono = "https://labs.goo.ne.jp/api/chrono"
@app.route('/')
def start():
return 'Use /entity?="Japanese text" for Named Entity Extraction'
@app.route('/get_cabocha', methods=['GET', 'POST'])
def get_cabocha():
text = request.args.get('text')
print(text)
ret_cabocha = c.parseToString(text)
print(ret_cabocha)
return ret_cabocha
#形態素解析API (Morphology)
@app.route('/get_mecab', methods=['GET', 'POST'])
def get_mecab():
text = request.args.get('text')
print(text)
ret_mecab = mecab.parse(text)
print(ret_mecab)
return ret_mecab
#商品評判要約
@app.route('/get_summary', methods=['GET', 'POST'])
def get_summary():
length = request.args.get('length')
review_list = request.args.get('review_list')
request_id = "record001"
text = api.shortsum(length=length,review_list=[review_list])
json_string = json.dumps(text, ensure_ascii=False)
response = Response(json_string, content_type="application/json; charset=utf-8")
return response
#固有表現抽出API
@app.route('/get_entity', methods=['GET', 'POST'])
def get_entity():
text2convert = request.args.get('text')
text = api.entity(sentence=u"" + text2convert)
json_string = json.dumps(text, ensure_ascii=False)
response = Response(json_string, content_type="application/json; charset=utf-8")
return response #jsonify(text)
@app.route('/get_similarity', methods=['GET', 'POST'])
def get_similarity():
text1check = request.args.get('text1')
text2check = request.args.get('text2')
print(text1check)
print(text2check)
ret_similarity = api.similarity(query_pair=[text1check,text2check])
json_string = json.dumps(ret_similarity, ensure_ascii=False)
response = Response(json_string, content_type="application/json; charset=utf-8")
return response
#キーワード抽出
@app.route('/get_keyword', methods=['GET', 'POST'])
def get_keyword():
max_keyword = 10
focus = ""
title = request.args.get('title')
body = request.args.get('body')
focus = request.args.get('focus')
max_keyword = request.args.get('max')
print(title)
print(body)
ret_keyword = api.keyword(title=title,body=body,focus=focus,max=max_keyword)
json_string = json.dumps(ret_keyword, ensure_ascii=False)
response = Response(json_string, content_type="application/json; charset=utf-8")
return response
#時刻情報正規化API
@app.route('/get_datetime', methods=['GET', 'POST'])
def get_datetime():
max_keyword = 10
focus = ""
sentence = request.args.get('text')
ret_datetime = api.chrono(sentence=sentence)
json_string = json.dumps(ret_datetime, ensure_ascii=False)
response = Response(json_string, content_type="application/json; charset=utf-8")
return response
if __name__ == '__main__':
#app.run()
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)

コメント

人気の投稿