Skip to main content

No project description provided

Project description

Dictionary Wrapper

  • Get definitions, synonyms and antonyms, etymology, audio link from Merriam Webster Dictionary API
  • Get example sentences from Wordnik API
  • Get IPA using Cambridge Dictionary scraper

Models

Synonym or Antonym

Result model for synonyms or antonyms

class SynonymOrAntonym(BaseModel):
    partOfSpeech: str
    detail: str
    words: list[str]

Definition

Result model for definition

class Definition(BaseModel):
    partOfSpeech: str
    detail: str
    exampleSentence: str

WordField

Result model for all the word fields

class WordField(BaseModel):
    word: str
    phonetic: str
    definitions: list[Definition]
    synonyms: list[SynonymOrAntonym]
    antonyms: list[SynonymOrAntonym]
    etymologies: list[str]
    exampleSentences: list[str]
    audioLink: str | None = None

Syncronous Version

import os

from dotenv import load_dotenv
from english_ipa.cambridge import CambridgeDictScraper

from dictionary_wrapper import get_word_field
from dictionary_wrapper.clients.mw_client import MerriamWebsterClient
from dictionary_wrapper.models.syn_ant_enum import SynAntEnum

load_dotenv()

## get all the word fields
dictionary_api_key = os.getenv("MW_DICT_KEY")
thesaurus_api_key = os.getenv("MW_THE_KEY")
wordnik_api_key = os.getenv("WORDIK_API_KEY")

word = "gallant"

word_field = get_word_field(
    word, dictionary_api_key, thesaurus_api_key, wordnik_api_key
)

## get definitions from Merriam-Webster Dictionary
mw_client = MerriamWebsterClient(word, dictionary_api_key, thesaurus_api_key)
definition = mw_client.extract_definitions()

## get definitions, synonyms and antonyms from Merriam-Webster Thesaurus
synonyms = mw_client.extract_synonyms_or_antonyms(SynAntEnum.Synonym)
antonyms = mw_client.extract_synonyms_or_antonyms(SynAntEnum.Antonym)

## get audio link from Merriam-Webster Dictionary
audio_link = mw_client.extract_audio_link()

## get etymologies from Merriam-Webster Thesaurus
etymologies = mw_client.extract_etymologies()

## get example sentences from Wordnik
wordnik_client = MerriamWebsterClient(word, dictionary_api_key, thesaurus_api_key)
example_sentences = wordnik_client.extract_example_sentences()

## get audio link from Wordnik
audio_link = wordnik_client.extract_audio_link()

## get ipa from Cambridge Dictionary
scraper = CambridgeDictScraper()
ipa = scraper.get_ipa_in_str(word)

Asyncronous Version

import asyncio
import os

import aiohttp
from dotenv import load_dotenv
from english_ipa.cambridge_async import AsyncCambridgeDictScraper

from dictionary_wrapper import get_word_field_async
from dictionary_wrapper.clients._wm_utils import (
    extract_definitions,
    extract_synonyms_or_antonyms,
)
from dictionary_wrapper.clients.mw_client_async import AsyncMerriamWebsterClient
from dictionary_wrapper.clients.wordnit_client_async import AsyncWordnikClient
from dictionary_wrapper.config import MWDictType
from dictionary_wrapper.models.common_models import WordField
from dictionary_wrapper.models.syn_ant_enum import SynAntEnum

load_dotenv()
dictionary_api_key = os.getenv("MW_DICT_KEY")
thesaurus_api_key = os.getenv("MW_THE_KEY")
wordnik_api_key = os.getenv("WORDIK_API_KEY")

word = "gallant"


## get all the word fields
async def main():
    word_field = await get_word_field_async(
        word, dictionary_api_key, thesaurus_api_key, wordnik_api_key
    )
    print(word_field)


asyncio.run(main())


## get api results from merriam-webster
async def fetch_mw_result(
    word: str, dictionary_api_key: str, thesaurus_api_key: str, wordnik_api_key: str
) -> WordField:
    mw_client = AsyncMerriamWebsterClient(word)
    async with aiohttp.ClientSession() as session:
        mw_dictionary = asyncio.create_task(
            mw_client.get_api_result(session, MWDictType.DICTIONARY, dictionary_api_key)
        )
        mw_thesaurus = asyncio.create_task(
            mw_client.get_api_result(session, MWDictType.THESAURUS, thesaurus_api_key)
        )

        return await asyncio.gather(mw_dictionary, mw_thesaurus)


## get definitions, synonyms and antonyms, etymology, audio link from Merriam-Webster Thesaurus
async def get_definitions(
    word: str, dictionary_api_key: str, thesaurus_api_key: str, wordnik_api_key: str
):
    dictionary_result, thesaurus_result = await fetch_mw_result(
        word, dictionary_api_key, thesaurus_api_key, wordnik_api_key
    )

    definitions = extract_definitions(word, dictionary_result)
    synonyms = extract_synonyms_or_antonyms(SynAntEnum.Synonym, thesaurus_result)
    antonyms = extract_synonyms_or_antonyms(SynAntEnum.Antonym, thesaurus_result)
    etymologies = extract_definitions(word, dictionary_result)
    audio_link = extract_definitions(word, dictionary_result)

    return definitions, synonyms, antonyms, etymologies, audio_link


# get example sentences and audio link from Wordnik
async def fetch_wordnik_result(word: str, wordnik_api_key: str):
    wordnik_client = AsyncWordnikClient(word, wordnik_api_key)
    async with aiohttp.ClientSession() as session:
        example_sentences = asyncio.create_task(
            wordnik_client.extract_example_sentences(session)
        )

        audio_link = asyncio.create_task(wordnik_client.extract_audio_link(session))

        return await asyncio.gather(example_sentences, audio_link)


# get ipa from Cambridge Dictionary
async def get_ipa(word: str):
    scraper = AsyncCambridgeDictScraper()
    ipa = await scraper.get_ipa_in_str(word)

    return ipa

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dictionary_wrapper-0.1.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

dictionary_wrapper-0.1.1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file dictionary_wrapper-0.1.1.tar.gz.

File metadata

  • Download URL: dictionary_wrapper-0.1.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for dictionary_wrapper-0.1.1.tar.gz
Algorithm Hash digest
SHA256 46d28923fb461f47d0c35210d19eb25dc9bae850fe7eed43a257ef19a190f200
MD5 8f9a918c73c9f92a7752165da7e2d582
BLAKE2b-256 45ebf1e65af9ee7f7e55288951f3e8f2a10e6dcb0019924c867f04f80a726e3c

See more details on using hashes here.

File details

Details for the file dictionary_wrapper-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for dictionary_wrapper-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 182193596b098530ea0a9d15a71b657de0f196479ba32111946750b3672ce51a
MD5 1ad9c2b84cce2ae3f3917dfd7f0fe388
BLAKE2b-256 e1543538220c553c2706ff6e990348e697544834df49854f8de9798db6423fe3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page