0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【PHP】ルビ振りAPIを使ってみた

Last updated at Posted at 2025-04-05

初めに

Excelにて漢字にふりがなをつけようとしたが、CSVをインポートしたデータやWEBページからコピペしたテキストには自動でふりがなが付かないことがわかった。そのため、代替手段としてYahoo!デベロッパーネットワークルビ振りAPI を使用することにした。

リファレンスより引用
漢字かな交じり文に、ひらがなとローマ字のふりがな(ルビ)を付けます。

環境

  • Windows11
  • PHP 8.0.12

パラメータ

  • id(必須項目) => 任意の値
  • jsonrpc(必須項目) => 2.0
  • params/q(必須項目) => 解析対象テキスト
  • params/grade =>小学校の学年を指定できる

本コードではparamsにてパラメータをセットしてください。
※事前に利用登録を行い、Client IDを取得すること。
※GETメソッドでは取得できないため、POSTメソッドを使用する必要がある

コード

hiragana_api.php
<?php

$appid = "your Client ID"; // <-- ここにあなたのClient ID(アプリケーションID)を設定してください。
$url = "https://jlp.yahooapis.jp/FuriganaService/V2/furigana";

function post($query, $appid, $url)
{
	$headers = [
		"Content-Type: application/json",
		"User-Agent: Yahoo AppID: $appid"
	];

	$param_dic = [
		"id" => "1234-1",
		"jsonrpc" => "2.0",
		"method" => "jlp.furiganaservice.furigana",
		"params" => [
			"q" => $query,
			"grade" => 1
		]
	];

	$params = json_encode($param_dic);

	$options = [
		'http' => [
			'header'  => implode("\r\n", $headers),
			'method'  => 'POST',
			'content' => $params
		]
	];

	$context  = stream_context_create($options);
	$result = file_get_contents($url, false, $context);

	if ($result === FALSE) {
		return "Error occurred";
	}

	return $result;
}

$response = post("漢字かな交じり文にふりがなを振ること。", $appid, $url);
echo $response;


レスポンス例

response.json
{
	"id": "1234-1",
	"jsonrpc": "2.0",
	"result": {
		"word": [{
			"furigana": "かんじ",
			"roman": "kanzi",
			"surface": "漢字"
		}, {
			"furigana": "かなまじり",
			"roman": "kanamaziri",
			"subword": [{
				"furigana": "かな",
				"roman": "kana",
				"surface": "かな"
			}, {
				"furigana": "ま",
				"roman": "ma",
				"surface": "交"
			}, {
				"furigana": "じり",
				"roman": "ziri",
				"surface": "じり"
			}],
			"surface": "かな交じり"
		}, {
			"furigana": "ぶん",
			"roman": "bun",
			"surface": "文"
		}, {
			"surface": "に"
		}, {
			"surface": "ふりがな"
		}, {
			"surface": "を"
		}, {
			"furigana": "ふる",
			"roman": "huru",
			"subword": [{
				"furigana": "ふ",
				"roman": "hu",
				"surface": "振"
			}, {
				"furigana": "る",
				"roman": "ru",
				"surface": "る"
			}],
			"surface": "振る"
		}, {
			"surface": "こと"
		}, {
			"surface": "。"
		}]
	}
}

終わりに

このAPIを使用することで、自動でふりがなをつけることができた。手動でするよりもはるかに工数が減らせるので、非常に有用なAPIだと思う。

使用にあたってはYahoo!デベロッパーネットワーク ガイドラインの範囲内、非商用目的でお使いください。商用利用の場合はお問い合わせください。
またAPI等の詳細は公式リファレンスを読んでお使いください。

各種条件

  • 利用回数->Client IDに対し、1分で300回
  • クレジット表示が必要

クレジット

Web Services by Yahoo! JAPAN

参考サイト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?