This is My Code That I modified the core of PyDictionary module :: Please Tell the errors if any,ALso My Plan is that after this program I will convert it to C++. I will use it to make a word meaning teller Arduino phone,, Also I have replaced the "self" with "word" because I ain't using classes any more:
import re
import goslate
from PyDictionary import PyDictionary
from PyDictionary.utils import _get_soup_object
def printMeanings(word):
dic = word.getMeanings()
for key in dic.keys():
print(key.capitalize() + ':')
for k in dic[key].keys():
print(k + ':')
for m in dic[key][k]:
print(m)
def printAntonyms(word):
antonyms = dict(zip(word.args,word.getAntonyms(False)))
for word in antonyms:
print(word+':')
print(', '.join(antonyms[word]))
def printSynonyms(word):
synonyms = dict(zip(word.args,word.getSynonyms(False)))
for word in synonyms:
print(word+':')
print(', '.join(synonyms[word]))
def getMeanings(word):
out = {}
for term in word.args:
out[term] = word.meaning(term)
return out
def translateTo(word, language):
return [word.translate(term, language) for term in word.args]
def translate(word, term, language):
if len(term.split()) > 1:
print("Error: A Term must be only a single word")
else:
try:
gs = goslate.Goslate()
word = gs.translate(term, language)
return word
except:
print("Invalid Word")
def getSynonyms(word, formatted=True):
return [word.synonym(term, formatted) for term in word.args]
def getAntonyms(word, formatted=True):
return [word.antonym(term, formatted) for term in word.args]
def synonym(term, formatted=False):
if len(term.split()) > 1:
print("Error: A Term must be only a single word")
else:
try:
data = _get_soup_object("https://www.synonym.com/synonyms/{0}".format(term))
section = data.find('div', {'class': 'type-synonym'})
spans = section.findAll('a')
synonyms = [span.text.strip() for span in spans]
if formatted:
return {term: synonyms}
return synonyms
except:
print("{0} has no Synonyms in the API".format(term))
@staticmethod
def antonym(term, formatted=False):
if len(term.split()) > 1:
print("Error: A Term must be only a single word")
else:
try:
data = _get_soup_object("https://www.synonym.com/synonyms/{0}".format(term))
section = data.find('div', {'class': 'type-antonym'})
spans = section.findAll('a')
antonyms = [span.text.strip() for span in spans]
if formatted:
return {term: antonyms}
return antonyms
except:
print("{0} has no Antonyms in the API".format(term))
@staticmethod
def meaning(term, disable_errors=False):
if len(term.split()) > 1:
print("Error: A Term must be only a single word")
else:
try:
html = _get_soup_object("http://wordnetweb.princeton.edu/perl/webwn?s={0}".format(
term))
types = html.findAll("h3")
length = len(types)
lists = html.findAll("ul")
out = {}
for a in types:
reg = str(lists[types.index(a)])
meanings = []
for x in re.findall(r'\((.*?)\)', reg):
if 'often followed by' in x:
pass
elif len(x) > 5 or ' ' in str(x):
meanings.append(x)
name = a.text
out[name] = meanings
return out
except Exception as e:
if disable_errors == False:
print("Error: The Following Error occured: %s" % e)
d = PyDictionary('honest','happy')
d.printSynonyms()