”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 如何使用免费词典API

如何使用免费词典API

发布于2024-11-08
浏览:467

How to use Free dictionary API

封面照片由 Christopher Gower 在 Unsplash 上拍摄

您是否正在开发语言学习应用程序、写作助手或任何涉及单词并需要 API 来检索单词含义的项目?免费词典 API 提供了一种免费且易于访问的方式,将语言数据合并到您的作品中。本文档将向您展示如何开始。

在此 API 文档中:

  • 关于免费词典API
  • 端点
  • 示例
  • 其他资源

关于免费词典 API:

由 meetDeveloper 开发的免费词典 API,在 GitHub 上拥有 2,600 颗星。 API 仅支持 GET 请求,并提供单词定义和音标。

主要特点:

  • 免费使用
  • 无使用限制
  • 无需授权或 API 密钥

端点

https://api.dictionaryapi.dev/api/v2/entries/en/
  • 该端点检索指定英文单词的词典信息。

  • API有两个版本:v1和v2。主要区别在于响应结构。当前版本是 v2。

查询参数

  • word(字符串,必填):要查找的单词。

响应格式

API返回一个JSON数组,其中包含单个对象,其中包含有关单词的详细信息,包括:

如果请求成功:

  • word(string): 查询的单词
  • voicetic(string): 单词的音标
  • voicetics(array): 语音对象数组,每个对象包含:
  • text(string): 音标
  • 音频(字符串):音频发音的 URL(可以为空)
  • 含义(数组):表示含义的对象数组
  • partOfSpeech(字符串):词性(例如名词、动词)
  • 定义(数组):定义对象的数组,每个对象包含:
  • 定义(字符串):单词的定义
  • synonyms(array): 同义词数组
  • antonyms (array): 反义词数组
  • 许可证(对象):有关提供数据的许可证的信息
  • sourceUrls (array): 数据源的 URL 数组

如果请求不成功:

  • title(字符串):一条消息,指示未找到单词
  • 的定义
  • message(字符串):未找到该单词的定义的消息
  • 解决方案(字符串):建议再次使用搜索或网络。

示例:

请求示例 1(成功):

const word = "documentation";
fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`)
  .then(response => response.json())
  .then(data => console.log(data));

响应示例 1:

[
  {
    "word": "documentation",
    "phonetic": "/ˌdɒkjʊmənˈteɪʃən/",
    "phonetics": [
      {
        "text": "/ˌdɒkjʊmənˈteɪʃən/",
        "audio": ""
      },
      {
        "text": "/ˌdɑkjəmənˈteɪʃən/",
        "audio": ""
      }
    ],
    "meanings": [
      {
        "partOfSpeech": "noun",
        "definitions": [
          {
            "definition": "Something transposed from a thought to a document; the written account of an idea.",
            "synonyms": [],
            "antonyms": []
          },
          {
            "definition": "Documentary evidence and sources.",
            "synonyms": [],
            "antonyms": []
          },
          {
            "definition": "Documents that explain the operation of a particular machine or software program.",
            "synonyms": [],
            "antonyms": []
          },
          {
            "definition": "Comments that explain the usage of individual functions, libraries and blocks of code.",
            "synonyms": [],
            "antonyms": []
          }
        ],
        "synonyms": [],
        "antonyms": []
      }
    ],
    "license": {
      "name": "CC BY-SA 3.0",
      "url": "https://creativecommons.org/licenses/by-sa/3.0"
    },
    "sourceUrls": [
      "https://en.wiktionary.org/wiki/documentation"
    ]
  }
]

请求示例 2:

const word = "Software engineer";
fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`)
  .then(response => response.json())
  .then(data => console.log(data));

响应示例 2(未成功):

{
  "title": "No Definitions Found",
  "message": "Sorry pal, we couldn't find definitions for the word you were looking for.",
  "resolution": "You can try the search again at later time or head to the web instead."
}

成功和错误代码

该API当前不返回特定的成功或错误代码。

其他资源

要尝试该 API 或支持 Free Dictionary API,请访问 Free Dictionary API 网站。

另请参阅 Github 上的免费词典 API 项目。

版本声明 本文转载于:https://dev.to/trushmi/how-to-use-free-dictionary-api-gec?1如有侵犯,请联系[email protected]删除
最新教程 更多>

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3