Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions pyDictonary/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Just import the pyDictonary by given code and start using the dictonary right away

```python
from pyDictonary import Dictonary

Dictonary('happy')
```
```
>>> ------------------------------------
>>> meaning: inclined to use a specified thing excessively or at random.
>>> ------------------------------------
>>> example: they tended to be grenade-happy
```
![](demo.png)

## TO-DO
- return print statement when user doesn't specify word and run the class empty
- Play the audio without downloading it
- Have the method which gives more definations and examples
Binary file added pyDictonary/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
184 changes: 184 additions & 0 deletions pyDictonary/pyDictonary.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 30,
"id": "73e98971",
"metadata": {},
"outputs": [],
"source": [
"import ssl\n",
"import urllib\n",
"import json\n",
"import os\n",
"try:\n",
" from IPython.core.display import display, HTML\n",
"except:\n",
" os.system('pip install IPython')\n",
"\n",
"\n",
"ssl._create_default_https_context = ssl._create_unverified_context"
]
},
{
"cell_type": "code",
"execution_count": 94,
"id": "9bd47d3d",
"metadata": {},
"outputs": [],
"source": [
"class Dictonary:\n",
" '''Pass the word inside the Dictonary and get the meaning along with example and audio pronaunciation\n",
" Dictonary('happy')\n",
" >>> ------------------------------------\n",
" >>> meaning: inclined to use a specified thing excessively or at random.\n",
" >>> ------------------------------------\n",
" >>> example: they tended to be grenade-happy\n",
" \n",
" '''\n",
" def __init__(self, word):\n",
" self.word = word\n",
" BASE_URL = 'http://api.dictionaryapi.dev/api/v2/entries/en_US/'\n",
" self.url = BASE_URL + word\n",
" \n",
" try:\n",
" response = json.load(urllib.request.urlopen(self.url))\n",
" response = response[-1]\n",
" except Exception as e:\n",
" print(e)\n",
" \n",
" try:\n",
" self.phonetic = response['phonetic']\n",
" except:\n",
" self.phonetic = 'None'\n",
" try:\n",
" self.origin = response['origin']\n",
" except:\n",
" self.origin = 'None'\n",
" try:\n",
" self.meaning = response['meanings'][-1]['definitions'][-1]['definition']\n",
" except:\n",
" self.meaning = 'None'\n",
" try:\n",
" self.example = response['meanings'][-1]['definitions'][-1]['example']\n",
" except:\n",
" self.example = 'None'\n",
" try:\n",
" self.audio_url = 'https:'+response['phonetics'][-1]['audio']\n",
" except:\n",
" self.audio_url = 'None'\n",
" \n",
" print('------------------------------------')\n",
" print(f'meaning: {self.meaning}')\n",
" print('------------------------------------')\n",
" print(f'example: {self.example}')\n",
" try:\n",
" self.show_audio_with_controls()\n",
" except:\n",
" pass\n",
" print('-------- Use class instance for more methods like word origin, phonetic, etc. --------')\n",
" print('-------- Please close the instance after-use to delete saved mp3 file ---------')\n",
" \n",
" def show_audio_with_controls(self):\n",
" urllib.request.urlretrieve(self.audio_url,f'{self.word}.mp3')\n",
" display(HTML(\"<audio controls><source src={}.mp3 type='audio/mpeg'></audio>\".format(self.word)))\n",
" \n",
" def close(self):\n",
" \n",
" mp3_file = f'{self.word}.mp3'\n",
" \n",
" try:\n",
" os.remove(mp3_file)\n",
" except OSError as e: \n",
" pass\n",
" \n",
" @staticmethod\n",
" def credit():\n",
" print('--------------------------------------------------')\n",
" print('| Created on: 16th October 2021 by Aditya Rajgor |')\n",
" print('| Inspired from: @rahulnegi20 github repository |')\n",
" print('| Author: Aditya Rajgor |')\n",
" print('| Open for contribution |')\n",
" print('--------------------------------------------------')\n",
" \n",
" pass\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 93,
"id": "301febbd",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------------------------------\n",
"meaning: inclined to use a specified thing excessively or at random.\n",
"------------------------------------\n",
"example: they tended to be grenade-happy\n"
]
},
{
"data": {
"text/html": [
"<audio controls><source src=happy.mp3 type='audio/mpeg'></audio>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-------- Use class instance for more methods like word origin, phonetic, etc. --------\n",
"-------- Please close the instance after-use to delete saved mp3 file ---------\n"
]
},
{
"data": {
"text/plain": [
"<__main__.Dictonary at 0x27ee15af6d0>"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Dictonary('happy')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
89 changes: 89 additions & 0 deletions pyDictonary/pyDictonary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import ssl
import urllib
import json
import os
try:
from IPython.core.display import display, HTML
except:
os.system('pip install IPython')


ssl._create_default_https_context = ssl._create_unverified_context


class Dictonary:
'''Pass the word inside the Dictonary and get the meaning along with example and audio pronaunciation
Dictonary('happy')
'>>> ------------------------------------'
''>>> meaning: inclined to use a specified thing excessively or at random.'
'>>> ------------------------------------'
'>>> example: they tended to be grenade-happy'
'''

def __init__(self, word):
self.word = word
BASE_URL = 'http://api.dictionaryapi.dev/api/v2/entries/en_US/'
self.url = BASE_URL + word

try:
response = json.load(urllib.request.urlopen(self.url))
response = response[-1]
except Exception as e:
print(e)

try:
self.phonetic = response['phonetic']
except:
self.phonetic = 'None'
try:
self.origin = response['origin']
except:
self.origin = 'None'
try:
self.meaning = response['meanings'][-1]['definitions'][-1]['definition']
except:
self.meaning = 'None'
try:
self.example = response['meanings'][-1]['definitions'][-1]['example']
except:
self.example = 'None'
try:
self.audio_url = 'https:' + response['phonetics'][-1]['audio']
except:
self.audio_url = 'None'

print('------------------------------------')
print(f'meaning: {self.meaning}')
print('------------------------------------')
print(f'example: {self.example}')
try:
self.show_audio_with_controls()
except:
pass
print('-------- Use class instance for more methods like word origin, phonetic, etc. --------')
print('-------- Please close the instance after-use to delete saved mp3 file ---------')

def show_audio_with_controls(self):
urllib.request.urlretrieve(self.audio_url, f'{self.word}.mp3')
display(HTML("<audio controls><source src={}.mp3 type='audio/mpeg'></audio>".format(self.word)))

def close(self):

mp3_file = f'{self.word}.mp3'

try:
os.remove(mp3_file)
except OSError as e:
pass

@staticmethod
def credit():
print('--------------------------------------------------')
print('| Created on: 16th October 2021 by Aditya Rajgor |')
print('| Inspired from: @rahulnegi20 github repository |')
print('| Author: Aditya Rajgor |')
print('| Open for contribution |')
print('--------------------------------------------------')

pass

2 changes: 2 additions & 0 deletions pyDictonary/requirements
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
IPython
jupyter