-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathcore.py
More file actions
143 lines (122 loc) · 4.41 KB
/
core.py
File metadata and controls
143 lines (122 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from __future__ import print_function
import sys, re, goslate
try:
from .utils import _get_soup_object
except:
from utils import _get_soup_object
python2 = False
if list(sys.version_info)[0] == 2:
python2 = True
class PyDictionary(object):
def __init__(self, *args):
try:
if isinstance(args[0], list):
self.args = args[0]
else:
self.args = args
except:
self.args = args
def printMeanings(self):
dic = self.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(self):
antonyms = dict(zip(self.args,self.getAntonyms(False)))
for word in antonyms:
print(word+':')
print(', '.join(antonyms[word]))
def printSynonyms(self):
synonyms = dict(zip(self.args,self.getSynonyms(False)))
for word in synonyms:
print(word+':')
print(', '.join(synonyms[word]))
def getMeanings(self):
out = {}
for term in self.args:
out[term] = self.meaning(term)
return out
def translateTo(self, language):
return [self.translate(term, language) for term in self.args]
def translate(self, 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:
raise InvalidWord
def getSynonyms(self, formatted=True):
return [self.synonym(term, formatted) for term in self.args]
def __repr__(self):
return "<PyDictionary Object with {0} words>".format(len(self.args))
def __getitem__(self, index):
return self.args[index]
def __eq__(self):
return self.args
def getAntonyms(self, formatted=True):
return [self.antonym(term, formatted) for term in self.args]
@staticmethod
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:
raise TooManyWords
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:
raise NoResults
@staticmethod
def meaning(term, disable_errors=False):
if len(term.split()) > 1:
raise TooManyWords
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:
raise e
if __name__ == '__main__':
d = PyDictionary('honest','happy')
d.printSynonyms()