├── .coveralls.yml ├── .gitignore ├── .travis.yml ├── LICENSE ├── PyDictionary ├── __init__.py ├── __init__.pyc ├── core.py ├── core.pyc ├── script.py ├── test_pydictionary.py ├── utils.py └── utils.pyc ├── README.md └── setup.py /.coveralls.yml: -------------------------------------------------------------------------------- 1 | service_name: travis-pro 2 | repo_token: 9jxTBTo0thl5BvXtkoUpShiXxgtwIrVhr -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | build/ 3 | *.egg-info/ 4 | __pycache__ 5 | token -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "2.7" 4 | - "3.4" 5 | - "3.8" 6 | install: 7 | - pip install PyDictionary 8 | - pip install coveralls 9 | script: nosetests -w PyDictionary 10 | after_success: 11 | - coveralls --verbose 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2020 Pradipta Bora 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. -------------------------------------------------------------------------------- /PyDictionary/__init__.py: -------------------------------------------------------------------------------- 1 | __author__ = "Pradipta Bora" 2 | __version__ = "1.5.1" 3 | 4 | try: 5 | from .core import * 6 | except: 7 | from core import * 8 | 9 | -------------------------------------------------------------------------------- /PyDictionary/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geekpradd/PyDictionary/75836b70a09ddb8d383530ac26dce102cbe5ced7/PyDictionary/__init__.pyc -------------------------------------------------------------------------------- /PyDictionary/core.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import sys, re, goslate 3 | try: 4 | from .utils import _get_soup_object 5 | except: 6 | from utils import _get_soup_object 7 | 8 | python2 = False 9 | if list(sys.version_info)[0] == 2: 10 | python2 = True 11 | 12 | class PyDictionary(object): 13 | 14 | def __init__(self, *args): 15 | try: 16 | if isinstance(args[0], list): 17 | self.args = args[0] 18 | else: 19 | self.args = args 20 | except: 21 | self.args = args 22 | 23 | 24 | def printMeanings(self): 25 | dic = self.getMeanings() 26 | for key in dic.keys(): 27 | print(key.capitalize() + ':') 28 | for k in dic[key].keys(): 29 | print(k + ':') 30 | for m in dic[key][k]: 31 | print(m) 32 | 33 | def printAntonyms(self): 34 | antonyms = dict(zip(self.args,self.getAntonyms(False))) 35 | for word in antonyms: 36 | print(word+':') 37 | print(', '.join(antonyms[word])) 38 | 39 | def printSynonyms(self): 40 | synonyms = dict(zip(self.args,self.getSynonyms(False))) 41 | for word in synonyms: 42 | print(word+':') 43 | print(', '.join(synonyms[word])) 44 | 45 | def getMeanings(self): 46 | out = {} 47 | for term in self.args: 48 | out[term] = self.meaning(term) 49 | return out 50 | 51 | def translateTo(self, language): 52 | return [self.translate(term, language) for term in self.args] 53 | 54 | def translate(self, term, language): 55 | if len(term.split()) > 1: 56 | print("Error: A Term must be only a single word") 57 | else: 58 | try: 59 | gs = goslate.Goslate() 60 | word = gs.translate(term, language) 61 | return word 62 | except: 63 | print("Invalid Word") 64 | 65 | def getSynonyms(self, formatted=True): 66 | return [self.synonym(term, formatted) for term in self.args] 67 | 68 | def __repr__(self): 69 | return "".format(len(self.args)) 70 | 71 | def __getitem__(self, index): 72 | return self.args[index] 73 | 74 | def __eq__(self): 75 | return self.args 76 | 77 | def getAntonyms(self, formatted=True): 78 | return [self.antonym(term, formatted) for term in self.args] 79 | 80 | @staticmethod 81 | def synonym(term, formatted=False): 82 | if len(term.split()) > 1: 83 | print("Error: A Term must be only a single word") 84 | else: 85 | try: 86 | data = _get_soup_object("https://www.synonym.com/synonyms/{0}".format(term)) 87 | section = data.find('div', {'class': 'type-synonym'}) 88 | spans = section.findAll('a') 89 | synonyms = [span.text.strip() for span in spans] 90 | if formatted: 91 | return {term: synonyms} 92 | return synonyms 93 | except: 94 | print("{0} has no Synonyms in the API".format(term)) 95 | 96 | 97 | @staticmethod 98 | def antonym(term, formatted=False): 99 | if len(term.split()) > 1: 100 | print("Error: A Term must be only a single word") 101 | else: 102 | try: 103 | data = _get_soup_object("https://www.synonym.com/synonyms/{0}".format(term)) 104 | section = data.find('div', {'class': 'type-antonym'}) 105 | spans = section.findAll('a') 106 | antonyms = [span.text.strip() for span in spans] 107 | if formatted: 108 | return {term: antonyms} 109 | return antonyms 110 | except: 111 | print("{0} has no Antonyms in the API".format(term)) 112 | 113 | @staticmethod 114 | def meaning(term, disable_errors=False): 115 | if len(term.split()) > 1: 116 | print("Error: A Term must be only a single word") 117 | else: 118 | try: 119 | html = _get_soup_object("http://wordnetweb.princeton.edu/perl/webwn?s={0}".format( 120 | term)) 121 | types = html.findAll("h3") 122 | length = len(types) 123 | lists = html.findAll("ul") 124 | out = {} 125 | for a in types: 126 | reg = str(lists[types.index(a)]) 127 | meanings = [] 128 | for x in re.findall(r'\((.*?)\)', reg): 129 | if 'often followed by' in x: 130 | pass 131 | elif len(x) > 5 or ' ' in str(x): 132 | meanings.append(x) 133 | name = a.text 134 | out[name] = meanings 135 | return out 136 | except Exception as e: 137 | if disable_errors == False: 138 | print("Error: The Following Error occured: %s" % e) 139 | 140 | if __name__ == '__main__': 141 | d = PyDictionary('honest','happy') 142 | d.printSynonyms() 143 | 144 | -------------------------------------------------------------------------------- /PyDictionary/core.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geekpradd/PyDictionary/75836b70a09ddb8d383530ac26dce102cbe5ced7/PyDictionary/core.pyc -------------------------------------------------------------------------------- /PyDictionary/script.py: -------------------------------------------------------------------------------- 1 | import click 2 | try: 3 | from .core import * 4 | except: 5 | from core import * 6 | 7 | @click.command() 8 | @click.option('--mode','-m',default="meaning",help="Mode of Script [meaning, antonym, synonym]") 9 | @click.option('--words', '-w',prompt="Enter words in a string separated by commas") 10 | def script(words,mode): 11 | print("PyDictionary:") 12 | word_values = [w.strip() for w in words.split(',')] 13 | d = PyDictionary(word_values) 14 | maps = {"meaning":d.printMeanings,"antonym":d.printAntonyms,"synonym":d.printSynonyms} 15 | click.echo(maps[mode]()) 16 | -------------------------------------------------------------------------------- /PyDictionary/test_pydictionary.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | try: 3 | from .__init__ import PyDictionary #Python 3 4 | except: 5 | from __init__ import PyDictionary 6 | 7 | dictionary=PyDictionary() 8 | 9 | class PyDictionaryTest(unittest.TestCase): 10 | def testMeaning(self): 11 | self.assertIsInstance(dictionary.meaning('python'),dict) 12 | def testSynonym(self): 13 | self.assertIsInstance(dictionary.synonym('happy'),list) 14 | def testAntonym(self): 15 | self.assertIsInstance(dictionary.antonym('happy'),list) 16 | 17 | if __name__=='__main__': 18 | unittest.main() -------------------------------------------------------------------------------- /PyDictionary/utils.py: -------------------------------------------------------------------------------- 1 | import requests 2 | from bs4 import BeautifulSoup 3 | 4 | def _get_soup_object(url, parser="html.parser"): 5 | return BeautifulSoup(requests.get(url).text, parser) 6 | -------------------------------------------------------------------------------- /PyDictionary/utils.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geekpradd/PyDictionary/75836b70a09ddb8d383530ac26dce102cbe5ced7/PyDictionary/utils.pyc -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## PyDictionary: A "Real" Dictionary Module for Python 2 | 3 | [![Build Status](http://img.shields.io/travis/geekpradd/PyDictionary/master.svg?style=flat-square)](https://travis-ci.org/geekpradd/PyDictionary) 4 | [![Latest Version](http://img.shields.io/pypi/v/PyDictionary.svg?style=flat-square)](https://pypi.python.org/pypi/PyDictionary/) 5 | [![License](https://img.shields.io/pypi/l/PyDictionary.svg?style=flat-square)](https://pypi.python.org/pypi/PyDictionary/) 6 | [![Downloads](https://img.shields.io/pypi/dm/PyDictionary.svg?style=flat-square)](https://pypi.python.org/pypi/PyDictionary/) 7 | 8 | NOTE: Mainintaing this module requires constantly changing the scrapping endpoints which unfortunately I no longer have the bandwidth to do so, so this module is DEPRECATED. Kindly use other substitutes available on PyPI. Thanks! 9 | 10 | 11 | PyDictionary is a Dictionary Module for Python 2/3 to get meanings, translations, synonyms and Antonyms of words. It uses WordNet for getting meanings, Google for translations, and synonym.com for getting synonyms and antonyms. 12 | 13 | This module uses Python Requests, BeautifulSoup4 and goslate as dependencies 14 | 15 | ### Installation 16 | 17 | Installation is very simple through pip (or easy_install) 18 | 19 | For pip 20 | 21 | ``` 22 | pip install PyDictionary 23 | ``` 24 | 25 | For Easy_Install 26 | 27 | ``` 28 | easy_install PyDictionary 29 | ``` 30 | 31 | ### Usage 32 | 33 | PyDictionary can be utilised in 2 ways, either by creating a dictionary instance which can take words as arguments or by creating a dictionary instance with a fixed amount of words. 34 | 35 | For example, 36 | 37 | ```python 38 | from PyDictionary import PyDictionary 39 | dictionary=PyDictionary() 40 | ``` 41 | 42 | This is will create a local instance of the PyDictionary class and now it can be used to get meanings, translations etc. 43 | 44 | ```python 45 | print (dictionary.meaning("indentation")) 46 | ``` 47 | 48 | This will return a dictionary containing the meanings of the word. 49 | For example the above code will return: 50 | 51 | ``` 52 | {'Noun': ['a concave cut into a surface or edge (as in a coastline', 'the 53 | formation of small pits in a surface as a consequence of corrosion', 'th 54 | e space left between the margin and the start of an indented line', 'the 55 | act of cutting into an edge with toothlike notches or angular incisions'] 56 | } 57 | ``` 58 | The dictionary keys are the different types of the word. If a word is both a verb and a noun then there will be 2 keys:'Noun' and 'Verb'. 59 | Each key refers to a list containing the meanings 60 | 61 | 62 | For Synonyms, 63 | 64 | ```python 65 | print (dictionary.synonym("Life")) 66 | ``` 67 | 68 | This will return a list containing the Synonyms of the word. 69 | 70 | For Antonyms, 71 | 72 | ```python 73 | print (dictionary.antonym("Life")) 74 | ``` 75 | This will return a list containing the Antonyms of the word. 76 | 77 | For Translations, 78 | 79 | ```python 80 | print (dictionary.translate("Range",'es')) 81 | ``` 82 | 83 | This will return the Translation of the word "Range" in Spanish. For Language codes consult Google Translate. The return value is string in Python 3 and unicode in Python 2 84 | 85 | Alternatively, you can set a fixed number of words to the PyDictionary Instance. This is useful if you just want to get the meanings of some words quickly without any development need. 86 | 87 | Example: 88 | 89 | ```python 90 | from PyDictionary import PyDictionary 91 | 92 | dictionary=PyDictionary("hotel","ambush","nonchalant","perceptive") 93 | 'There can be any number of words in the Instance' 94 | 95 | print(dictionary.printMeanings()) '''This print the meanings of all the words''' 96 | print(dictionary.getMeanings()) '''This will return meanings as dictionaries''' 97 | print (dictionary.getSynonyms()) 98 | 99 | print (dictionary.translateTo("hi")) '''This will translate all words to Hindi''' 100 | 101 | ``` 102 | 103 | Similarly Synonyms and Antonyms can also be printed onto the screen. 104 | 105 | ### About 106 | 107 | Current Version: 2.0.1 108 | Created By Pradipta Bora 2020. 109 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import setuptools 2 | 3 | with open("README.md", "r") as fh: 4 | long_description = fh.read() 5 | 6 | setuptools.setup( 7 | name="PyDictionary", # Replace with your own username 8 | version="2.0.1", 9 | author="geekpradd", 10 | author_email="pradd@outlook.com", 11 | description="A real dictionary module for Python", 12 | long_description=long_description, 13 | long_description_content_type="text/markdown", 14 | url="https://github.com/geekpradd/PyDictionary", 15 | packages=setuptools.find_packages(), 16 | classifiers=[ 17 | "License :: OSI Approved :: MIT License", 18 | "Operating System :: OS Independent", 19 | ], 20 | install_requires=[ 21 | 'bs4', 22 | 'click', 23 | 'goslate', 24 | 'requests' 25 | ] 26 | ) --------------------------------------------------------------------------------