Oxford Dictionaries APIの使い方


# Oxford Dictionaries APIとは
一言でいえば、かの有名なOxford Universityが提供する35か国語対応の辞書APIです。
月1000アクセスまでは無償で（商用も含めた）利用が可能です。
詳細は[こちら](https://developer.oxforddictionaries.com/about)。


## [FAQ](https://developer.oxforddictionaries.com/faq)より抜粋&意訳
> Who built the Oxford Dictionaries API?
> 
The Oxford Dictionaries API was built by a team in Oxford Dictionaries which is a part of Oxford University Press. You can find out more about our API here, and more about Oxford Dictionaries here.

Q：誰がOxford Dictionaries APIを作ったの？
A：Oxford University出版局のOxford Dictionaries部門のチームによって構築されました。
詳しくは[こちら](https://languages.oup.com/)。


> What can I do with the Oxford Dictionaries API?
>
The Oxford Dictionaries API offers an easy way to access powerful lexical data (words, definitions, translations, audio pronunciations, synonyms, antonyms, parts of speech, and more) to use in your apps and websites. Please read our Terms and Conditions before signing up. If you would like to discuss your use of the API then please complete the Enterprise form to contact us.

Q：何ができる？
A：自然言語処理（見出し語変換や定義、翻訳、類義語など）。


> Which dictionary does the API data come from?
>
The Oxford Dictionaries API provides access to our current English dictionaries which are available on en.oxforddictionaries.com. This is our most comprehensive current English dictionary dataset and includes enhanced, updated versions of the Oxford Dictionary of English and the New Oxford American Dictionary. It includes more than 350,000 entries and is regularly updated with new words, senses, and definitions.
We also have content in Spanish, German, Portuguese, Romanian, isiZulu, Northern Sotho, Malay, Indonesian...and many more, and we're adding more all the time. Take a look at our supported languages page for the most up-to-date list.

Q：データはどこから？
A：[こちら](https://www.lexico.com/)のサイトで利用しているものと同じ。

> Which languages do you support?
>
In the API, we currently offer content in English, Spanish, German, Portuguese, Romanian, isiZulu, Northern Sotho, Latvian, Malay, Indonesian...and many more, and we're adding more all the time. Take a look at our supported languages page for the most up-to-date list.
If you’re looking for a dictionary dataset for a language which isn’t currently available via the API, we might still be able to help. We have over 35 language datasets, from Afrikaans to Vietnamese, available to license via Oxford Dictionaries Licensing. We are also building content in 100 of the world's languages, many of which are digitally under-represented, as part of our Oxford Global Languages initiative.

Q：対応言語は？
A：英語、スペイン語、ドイツ語、ポルトガル語、ルーマニア語、ズールー語、北ソト語、ラトビア語、マレー語、インドネシア語など。


# アカウントの作り方
1. [こちら](https://developer.oxforddictionaries.com/)からサイトにアクセスし、Get your API keyをクリック
<img width="1118" alt="ox1.jpg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/304608/04e5f71d-7c21-09ee-b206-2197dbd92efc.jpeg">

2. （無料枠で運用したい場合は）左枠のPrototypeのGET STARTEDをクリック
<img width="1115" alt="ox2.jpg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/304608/cd373b77-b10f-cafc-5495-b14c1a2fe38f.jpeg">

3. 必要事項を記入。なお"Please tell us more about your application"は短くても大丈夫（自分の場合はTo develop English learning bot.としました）。
<img width="1115" alt="ox3.jpg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/304608/dfe75146-8a21-1edc-0bbd-51c019015e69.jpeg">

4. 入力したアドレスにメールが来るので、アカウントアクティベート用のURLをクリック。

5. アクティベート完了後にトップページに戻り、Sign In
<img width="1118" alt="ox1.jpg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/304608/04e5f71d-7c21-09ee-b206-2197dbd92efc.jpeg">

# 使い方
1. サインイン後はトップページに(API) CREDENTIALSが追加されるので、そこをクリック
<img width="1118" alt="ox4.jpg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/304608/cd856ea2-9d33-1c28-e140-9f8c2b28a0f9.jpeg">

2. 自分で設定したアプリ名をクリック
<img width="1128" alt="ox5.jpg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/304608/726d283c-bbc4-f044-3d28-dc986b954a70.jpeg">

3. Application IDとApplication KEYを確認
<img width="1091" alt="ox6.jpg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/304608/8f498d8e-9ea0-1cb5-450d-928200a1e962.jpeg">

4. 以下コピペ

```py
app_id = "Your own ID"
app_key = "Your own Key"
language = "en-gb"
word = "Word you want to search"
url = "https://od-api.oxforddictionaries.com:443/api/v2/entries/" + language + "/" + word.lower()
response = requests.get(url, headers={"app_id":app_id, "app_key":app_key})
```

`response.status_code`が200であれば成功、それ以外であれば失敗

| status_code      |       Message |
|:-----------------|:------------------|
| 400              | Invalid value for filters such as lexicalCategory, registers, domains, etc. Invalid value for fields projections accepted. It is not possible to project a non-existing field.|
| 404              | No entry was found matching the selection parameters; OR an invalid filter was specified. |
| 414              | URL is too long. |
| 500              | Internal error. An error occurred during processing. |

# 試し方
1. DOCUMENTATIONタブのV2 SWAGGER DOCSをクリック
<img width="1116" alt="ox7.jpg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/304608/d420020b-772e-5e33-aac5-8aaf7a292162.jpeg">

2. 下の方にあるExpand Operationsをクリック
<img width="1128" alt="ox8.jpg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/304608/2df6bd02-c916-3ecd-af4e-60269e2ccde1.jpeg">

3. 確認したいプログラミング言語を選択]
<img width="1128" alt="ox9.jpg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/304608/27e09484-6ee1-1256-3145-323952e2e666.jpeg">

例：Pythonを選択
<img width="1118" alt="ox9-extra.jpg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/304608/e1054570-3464-b1a2-1043-35ee28f46d70.jpeg">

```py:python
# for more information on how to install requests
# http://docs.python-requests.org/en/master/user/install/#install
import requests
import json

# TODO: replace with your own app_id and app_key
app_id = '<my_app_id>'
app_key = '<my_app_key>'

language = 'en-gb'
word_id = 'Ace'
fields = 'pronunciations'
strictMatch = 'false'

url = 'https://od-api.oxforddictionaries.com:443/api/v2/entries/' + language + '/' + word_id.lower() + '?fields=' + fields + '&strictMatch=' + strictMatch;

r = requests.get(url, headers = {'app_id': app_id, 'app_key': app_key})

print("code {}\n".format(r.status_code))
print("text \n" + r.text)
print("json \n" + json.dumps(r.json()))
```

4. word_idに調べたい単語の「原形」を入力
<img width="1128" alt="ox9.jpg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/304608/27e09484-6ee1-1256-3145-323952e2e666.jpeg">

5. Try it outをクリック
<img width="1117" alt="ox10.jpg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/304608/0d669582-02fc-c9a6-55d1-e11419f85fe8.jpeg">

6. Try it outの下に表示されるAPIの返り値を確認
<img width="1115" alt="ox11.jpg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/304608/6716f9bb-c105-cf63-a2cb-8546e7d3e769.jpeg">


# 最後に
月1000回まではちょっと少ない気もしますが、いろいろ使えそうです！