高木のブログ

codicのAPIを試してみる

2019/06/06

いつもお世話になってるcodicにAPIがあったので試してみる。もちろん認証が必要。
トークンの取得とか使い方の説明は以下を参照
API | codic

手順

こんな感じでcurlを叩く。

$ curl -H "Authorization: Bearer {ACCESS_TOKEN}" "https://api.codic.jp/v1/engine/translate.json?text=記事を書く&casing=lower+underscore"

実際に叩いてみた結果。

$ curl -H "Authorization: Bearer {ACCESS_TOKEN}" "https://api.codic.jp/v1/engine/translate.json?text=記事を書く&casing=lower+underscore"
[{"successful":true,"text":"\u8a18\u4e8b\u3092\u66f8\u304f","translated_text":"write_article","words":[{"successful":true,"text":"\u66f8\u304f","translated_text":"write","candidates":[{"text":"write"},{"text":"writing"}]},{"successful":true,"text":"\u3092","translated_text":null,"candidates":[{"text":null},{"text":"that"},{"text":"to"},{"text":"for"},{"text":"from"},{"text":"is"},{"text":"of"}]},{"successful":true,"text":"\u8a18\u4e8b","translated_text":"article","candidates":[{"text":"article"},{"text":"posts"},{"text":"post"},{"text":"news"}]}]}]

見にくいし、文字があれなので、jq使う。

[Dev:~]$ curl -H "Authorization: Bearer {ACCESS_TOKEN}" "https://api.codic.jp/v1/engine/translate.json?text=記事を書く&casing=lower+underscore" | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   554  100   554    0     0   1355      0 --:--:-- --:--:-- --:--:--  1354
[
  {
    "successful": true,
    "text": "記事を書く",
    "translated_text": "write_article",
    "words": [
      {
        "successful": true,
        "text": "書く",
        "translated_text": "write",
        "candidates": [
          {
            "text": "write"
          },
          {
            "text": "writing"
          }
        ]
      },
      {
        "successful": true,
        "text": "を",
        "translated_text": null,
        "candidates": [
          {
            "text": null
          },
          {
            "text": "that"
          },
          {
            "text": "to"
          },
          {
            "text": "for"
          },
          {
            "text": "from"
          },
          {
            "text": "is"
          },
          {
            "text": "of"
          }
        ]
      },
      {
        "successful": true,
        "text": "記事",
        "translated_text": "article",
        "candidates": [
          {
            "text": "article"
          },
          {
            "text": "posts"
          },
          {
            "text": "post"
          },
          {
            "text": "news"
          }
        ]
      }
    ]
  }
]

変換されたものだけじゃなくて単語ごとの候補も出してくれるっぽい。
何かに使えたら使ってみよう。


Pixela