1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Node.jsでYahooの校正支援APIを叩いてみる

Last updated at Posted at 2022-11-06

TextLintなどを調べていて、Yahooの校正支援のAPIがあったので試してみました。

https://developer.yahoo.co.jp/webapi/jlp/kousei/v2/kousei.html

Node.jsからアクセスする

Pythonのサンプルをもとに書いてみました。

https://developer.yahoo.co.jp/webapi/jlp/sample/sample11.html

今回Fetch APIを使ってPOSTしてます。

const BASE_URL = `https://jlp.yahooapis.jp/KouseiService/V2`;
const YAHOO_APPID = `YAHOOディベロッパーの管理画面で取得したAPPID`;

const yahooKosei = async () => {
    const ENDPOINT = `${BASE_URL}/kousei`;

    try {
        const data = {
            id: '1234-1',
            jsonrpc: '2.0',
            method: 'jlp.kouseiservice.kousei',
            params: {
                q: "セキュリティー,食べれる"
            }
        };
        
        const options = {
            headers:{
                'Content-Type': 'application/json',
                'User-Agent': `Yahoo AppID: ${YAHOO_APPID}`
            },
            method: 'POST',
            body: JSON.stringify(data)
        };

        const jsonResponse = await fetch(ENDPOINT, options);
        const jsonData = await jsonResponse.json();

        return jsonData;
    } catch (error) {
        throw new Error(error);
    }
}

実行するとこんな感じです。

{
  id: "1234-1",
  jsonrpc: "2.0",
  result: {
    suggestions: [
      {
        length: "7",
        note: "語末が-tyだが昨今のネット上の慣習に準ず",
        offset: "0",
        rule: "用字",
        suggestion: "セキュリティ",
        word: "セキュリティー"
      },
      {
        length: "4",
        note: "",
        offset: "8",
        rule: "ら抜き",
        suggestion: "食べられる",
        word: "食べれる"
      }
    ]
  }
}

FetchのPOST時は文字列にする必要があるっぽい

最初body: dataと書いていて、文字列化しないとパースエラーっぽい感じになりました。

body: JSON.stringify(data)でうまく動いた。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?