python-freeDictionaryAPI is a wrapper for Free Dictionary API.
Library is simple, light and uses very cool fully free dictionary API.
Library components can be used with high level API (clients) or if you wish so you can use only some implemented parts that you`re interested in (like URL generating, parsers, ...).
Implemented synchronous and asynchronous clients that powered with httpx and aiohttp accordingly.
If you do not prefer to use implemented clients and want to use some other web lib. So, it is synchronous and asynchronous base clients for inheriting. All you need it is to implement one method that makes HTTP request.
You can read the docs here.
$ pip install python-freeDictionaryAPI
To install package with extra requirements for one of the client:
- for synchronous client that uses
httpx
:
$ pip install python-freeDictionaryAPI[sync-client]
- for asynchronous client that uses
aiohttp
:
$ pip install python-freeDictionaryAPI[async-client]
>>> from freedictionaryapi.clients.sync_client import DictionaryApiClient
>>> with DictionaryApiClient() as client:
... parser = client.fetch_parser('hello')
>>> word = parser.word
>>> word.word
'hello'
>>> word.phonetics
[Phonetic(text='/həˈloʊ/', audio='https://lex-audio.useremarkable.com/mp3/hello_us_1_rr.mp3'), Phonetic(text='/hɛˈloʊ/', audio='https://lex-audio.useremarkable.com/mp3/hello_us_2_rr.mp3')]
>>> for meaning in word.meanings:
... print(meaning.part_of_speech)
... for definition in meaning.definitions:
... print(definition)
... print()
noun
Definition(definition='An utterance of “hello”; a greeting.', example='she was getting polite nods and hellos from people', synonyms=['greeting', 'welcome', 'salutation', 'saluting', 'hailing', 'address', 'hello', 'hallo'])
intransitive verb
Definition(definition='Say or shout “hello”; greet someone.', example='I pressed the phone button and helloed', synonyms=None)
exclamation
Definition(definition='Used as a greeting or to begin a phone conversation.', example='hello there, Katie!', synonyms=None)
>>> parser.get_transcription()
'/həˈloʊ/'
>>> parser.get_link_on_audio_with_pronunciation()
'https://lex-audio.useremarkable.com/mp3/hello_us_1_rr.mp3'
>>> parser.get_all_definitions()
['An utterance of “hello”; a greeting.', 'Say or shout “hello”; greet someone.', 'Used as a greeting or to begin a phone conversation.']
>>> parser.get_all_synonyms()
['hello', 'hailing', 'welcome', 'address', 'salutation', 'hallo', 'saluting', 'greeting']
>>> parser.get_all_examples()
['she was getting polite nods and hellos from people', 'I pressed the phone button and helloed', 'hello there, Katie!']
API that used in this library does not provide present of all fields in response.
So, be aware, when response is parsed
and some of the fields are empty in result -
in code they`ll be returning None
.
Good luck!