Create a gist now

Instantly share code, notes, and snippets.

# -*- coding: utf-8 -*-
"""
docomoの言語解析API(固有表現抽出)を叩くサンプルコード
"""
import requests
import json
BASEURL = "https://api.apigw.smt.docomo.ne.jp/gooLanguageAnalysis/v1/entity?APIKEY="
APIKEY = "YOUR APIKEY" #ここにAPIKEYを入力
class Docomo(object):
def __init__(self, APIKEY=APIKEY):
self.uri = "{}{}".format(BASEURL, APIKEY)
self.payload = {
"sentence": None,
"class_filter": "PSN" #必要なフィルタを記述
}
self.headers = {'Content-Type': 'application/json'}
def parse(self, query):
self.payload["sentence"] = query
r = requests.post(self.uri, data=json.dumps(self.payload), headers=self.headers)
return json.loads(r.text)
def main():
agent = Docomo()
resp = agent.parse("田中さんの日常。")
for name, _type in resp["ne_list"]:
print "Entity: {}\t Type: {}".format(name, _type)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment