-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic_controller.py
More file actions
68 lines (56 loc) · 1.98 KB
/
logic_controller.py
File metadata and controls
68 lines (56 loc) · 1.98 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
import requests
import json
class Model:
def __init__(self):
self.word_freq = {}
self.load()
self.txt = ''
def add_freq(self, word):
if word in self.word_freq.keys():
self.word_freq[word] += 1
else:
self.word_freq.update({word: 1})
def get_freq_range(self, minimum, maximum):
word_list = [word for word in self.word_freq.keys() if minimum <= self.word_freq[word] <= maximum]
with open('export.txt', 'w') as f:
for word in word_list:
f.write(word)
f.write(' | ')
line_def = self.get_definition(word).replace('\n', '')
f.write(line_def)
f.write('\n')
def get_definition(self, word):
s = ''
response = requests.get(f'https://api.dictionaryapi.dev/api/v2/entries/en/{word}')
dict = response.json()
try:
definitions = dict[0]['meanings']
except:
return "Sorry pals no definitions here."
else:
for part_of_speech in definitions:
s += part_of_speech['partOfSpeech']
s += '\n---\n'
defs = '\n'.join([defs['definition'] for defs in part_of_speech['definitions']])
s += defs
s += '\n------------------------------------------\n'
return s
def save(self):
with open("word_freq.json", "w") as outfile:
json.dump(self.word_freq, outfile)
def load(self):
with open('word_freq.json') as user_file:
json_object = json.load(user_file)
self.word_freq = json_object
def get_text(self, file_dir):
with open(file_dir, 'r', encoding="utf-8") as file:
text = file.read()
self.txt = text
return text
model = Model()
model.get_freq_range(1, 5)
if __name__ == '__main__':
m = Model()
print(m.word_freq)
print(type((m.word_freq)))
#print(m.get_definition('hell'))