おすすめの記事
おすすめアプリ
はじめに
英単語を調べるとき、例文や発音、類義語なども一括で取得できると便利ですよね。
今回は、アメリカ発の有名な英語 API「Wordnik API」を使い、TypeScript から英単語の意味や例文、類義語などを取得する方法を解説します。
wordnik API とは?
Wordnikは、英単語の定義、例文、類義語、発音などを API で取得できる英語学習者向けの人気サービスです。
無料プランでも十分使えますが、API キーの取得が必要です。
Wordnik API でできること
- 英単語の意味(定義)取得
- 用例(例文)取得
- 類義語・同義語・反意語取得
- 発音記号の取得
- 関連語、成句、語源の取得も可能
API キーの取得方法
- Wordnik Developers にアクセス
- サインアップして API キーを取得(無料)
無料でも利用できますが、1 週間ほど API キーの発行に時間がかかるようです。
5 ドル寄付すれば 24 時間以内に発行されてメールで API キーがおくられてきます。
(私の場合は 2 時間程度で送られてきました)
TypeScript で API を使う基本例
今回は英単語 “apple” の意味を取得してみます。
const apiKey = "YOUR_API_KEY";
const word = "apple";
fetch(
`https://api.wordnik.com/v4/word.json/${word}/definitions?limit=3&api_key=${apiKey}`
)
.then((res) => res.json())
.then((data) => {
console.log(data);
});
レスポンス
[
{
"text": "The firm, edible, usually rounded fruit of this tree.",
"partOfSpeech": "noun",
"sourceDictionary": "ahd-5"
},
{
"text": "Any of several other plants, especially those with fruits suggestive of the apple.",
"partOfSpeech": "noun",
"sourceDictionary": "ahd-5"
}
]
他の使い方の例
// 例文取得
fetch(
`https://api.wordnik.com/v4/word.json/${word}/examples?limit=3&api_key=${apiKey}`
)
.then((res) => res.json())
.then((data) => {
console.log(data.examples.map((e) => e.text));
});
// 関連語の取得
fetch(
`https://api.wordnik.com/v4/word.json/${word}/relatedWords?relationshipTypes=synonym&api_key=${apiKey}`
)
.then((res) => res.json())
.then((data) => {
const synonyms =
data.find((rw) => rw.relationshipType === "synonym")?.words ?? [];
console.log(synonyms);
});
自分のアプリに統合してみた!
絶賛開発中の lingofy に組み込んでみました。
定義、例文、発音、関連語の取得ができました!
おわりに
その他の記事もぜひご覧ください!
お問い合わせ: hiromacha1116@icloud.com