-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
executable file
·61 lines (41 loc) · 1.98 KB
/
search.py
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
from datetime import datetime
import utils.helpers as helpers
class Search:
def __init__(self):
self.anagram = []
self.dictionary_name = "files/words_pt.txt"
self.my_dictionary = self._load_dictionary()
def _load_dictionary(self):
tmp_dictionary = {}
file = open(self.dictionary_name, "r")
dict_all = file.read()
file.close()
dict_all = dict_all.strip().split('\n')
for i in range(len(dict_all)):
if helpers.check_valid_word(dict_all[i]):
sorted_word = ''.join(sorted(helpers.clean_word(dict_all[i])))
if sorted_word not in tmp_dictionary.keys():
tmp_dictionary[sorted_word] = list()
tmp_dictionary[sorted_word].append(dict_all[i])
return tmp_dictionary
def search_anagrams(self, anagram):
existing_anagrams = list()
sorted_word = ''.join(sorted(helpers.clean_word(anagram)))
start_time = datetime.now().time().strftime('%H:%M:%S.%f')
if sorted_word in self.my_dictionary.keys():
existing_anagrams = self.my_dictionary[sorted_word]
end_time = datetime.now().time().strftime('%H:%M:%S.%f')
total_time = (datetime.strptime(end_time, '%H:%M:%S.%f') -
datetime.strptime(start_time, '%H:%M:%S.%f'))
return total_time, existing_anagrams
def search_like_anagrams(self, anagram):
existing_anagrams = list()
sorted_word = ''.join(sorted(helpers.clean_word(anagram)))
start_time = datetime.now().time().strftime('%H:%M:%S.%f')
for key_word in self.my_dictionary.keys():
if sorted_word in key_word:
existing_anagrams += self.my_dictionary[key_word]
end_time = datetime.now().time().strftime('%H:%M:%S.%f')
total_time = (datetime.strptime(end_time, '%H:%M:%S.%f') -
datetime.strptime(start_time, '%H:%M:%S.%f'))
return total_time, existing_anagrams