かな漢字変換
このサンプルはかな漢字変換APIを使用して、入力された文章に対してかな漢字変換を行い、その結果を出力するものです。
使用API | |
---|---|
構成環境 | Python 3.6以上 |
利用規約
LINEヤフーの提供するサンプルコードをご利用いただくにあたっては、
ガイドラインの内容をお読みいただき、同意していただくことが必要です。
サンプルコードを利用することによって、ガイドラインの内容に同意いただいたものとみなします。
ガイドラインに同意いただけない場合は、サンプルコードを使用するライセンスは許諾されません。
サンプルコード
sample.py
import json
from urllib import request
APPID = "<あなたのClient ID(アプリケーションID)>" # <-- ここにあなたのClient ID(アプリケーションID)を設定してください。
URL = "https://jlp.yahooapis.jp/JIMService/V2/conversion"
def post(query):
headers = {
"Content-Type": "application/json",
"User-Agent": "Yahoo AppID: {}".format(APPID),
}
param_dic = {
"id": "1234-1",
"jsonrpc": "2.0",
"method": "jlp.jimservice.conversion",
"params": {
"q": query,
"format": "hiragana",
"mode": "kanakanji",
"option": ["hiragana", "katakana", "alphanumeric", "half_katakana", "half_alphanumeric"],
"dictionary": ["base", "name", "place", "zip", "symbol"],
"results": 999
}
}
params = json.dumps(param_dic).encode()
req = request.Request(URL, params, headers)
with request.urlopen(req) as res:
body = res.read()
return body.decode()
response = post("きょうはよいてんきです。")
print(response)
実行例
$ python3 sample.py
{"id":"1234-1","jsonrpc":"2.0","result":{"segment":[{"alphanumeric":"kyouha","candidate":["今日は","きょうは","教派","興は","京は","強は","経は","教は","境は","凶は","卿は","峡は","協は","香は","恭は","享は","杏は","馨は","叶は","許は","亨は","競は","共は","匡は","叫は","喬は","彊は","橋は","郷は","鏡は","響は","驚は","尭は","暁は","慶は","洪は","姜は","癸生は"],"half_alphanumeric":"kyouha","half_katakana":"キョウハ","hiragana":"きょうは","katakana":"キョウハ","reading":"きょうは"},{"alphanumeric":"yoi","candidate":["よい","良い","宵","酔い","四位","4位","余威","好い","善い","佳い","吉い","快い","与位","与井","與井","嘉","四意","世以","代威","代居","霄","佳","可","儀","義","誼","吉","慶","好","淑","酔","善","良","俶","懿","譱","臧","醉"],"half_alphanumeric":"yoi","half_katakana":"ヨイ","hiragana":"よい","katakana":"ヨイ","reading":"よい"},{"alphanumeric":"tennkidesu.","candidate":["天気です。","転機です。","転記です。","転帰です。","天機です。","天貴です。","てんきです。"],"half_alphanumeric":"tennkidesu.","half_katakana":"テンキデス。","hiragana":"てんきです。","katakana":"テンキデス。","reading":"てんきです。"}]}}