-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
191 lines (123 loc) · 5.07 KB
/
search.py
File metadata and controls
191 lines (123 loc) · 5.07 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import requests
from bs4 import BeautifulSoup
import urllib, json
import requests
import scraper
# define punctuation
punctuations = '''0123456789çşığ!()-[]{};:'"\,<>./?@#$%^&*_~'''
def searchGoogle(acClues,acIndexes,dnClues,dnIndexes):
resList = []
for i in range(len(acClues)):
google_words = set()
clue = acClues[i].text.replace(" ", "+")
request = requests.get('https://www.google.com/search?q={}&lr=lang_en'
.format(clue))
soup = BeautifulSoup(request.content, "html.parser")
result_elements = soup.text
for word in result_elements.split():
google_words.add(word)
# getting rid of from unnecessary chars
mylist = []
for word in google_words:
no_punct = ""
for char in word:
if char not in punctuations:
no_punct = no_punct + char
no_punct.lower()
mylist.append(no_punct)
google_words = mylist
# Process word list
#google_words = self.clear_google_words(google_words)
google_words = list(filter(lambda x: len(x) <= 5, google_words))
print("Google Search \nWord Count: " + str(len(google_words)))
resList.append(google_words)
return resList
def searchDataMuse(acClues,acIndexes,dnClues,dnIndexes):
resList = []
for i in range(len(acClues)):
#print("", acIndexes[i].text, " ", acClues[i].text)
query = acClues[i].text.replace(" ", "+")
query = query.lower()
request = requests.get('https://api.datamuse.com/words?ml={}'.format(query))
respond = request.json()
tList = []
#for j in range(len(respond)):
for j in range(min(5,len(respond))):
tList.append(respond[j]['word'])
resList.append(tList)
#resList = list(filter(lambda x: len(x) == length, datamuse_words))
#print("Datamuse Search \nWord Count: " + str(len(datamuse_words)))
for i in range(len(dnClues)):
#print("", acIndexes[i].text, " ", acClues[i].text)
query = dnClues[i].text.replace(" ", "+")
query = query.lower()
request = requests.get('https://api.datamuse.com/words?ml={}'.format(query))
respond = request.json()
tList = []
for item in respond:
tList.append(item['word'])
resList.append(tList)
for i in range(len(resList)):
resList[i] = list(filter(lambda x: len(x) <= 5, resList[i]))
return resList
def detailedSearchDataMuse(heuristic):
resList = []
#query = knu.replace(" ", "+")
#query = query.lower()
request = requests.get('https://api.datamuse.com/words?sp={}'.format(heuristic))
respond = request.json()
for j in range(len(respond)):
#for j in range(5):
resList.append(respond[j]['word'])
return resList
def searchMerriam(acClues,acIndexes,dnClues,dnIndexes):
API_KEY = "dd09241d-bf89-4f78-8834-34dc6e0495c4"
#for clue in acClues:
# print(clue.text.split())
resList = [[]]*(len(acClues)+len(dnClues))
ind = 0
for clue in acClues:
words = clue.text.split()
rList = []
if(len(words) <= 3):
for word in words:
URL = "https://www.dictionaryapi.com/api/v3/references/thesaurus/json/"
URL = URL + word + "?key="+API_KEY
try:
response = requests.get(URL)
#data = json.loads(response.read())
#print(word, ind)
#print (response.json()[0]['meta']['syns'][0])
rList.append(response.json()[0]['meta']['syns'][0])
#resList[ind].append(response.json()[0]['meta']['syns'][0])
#print(rList,"\n\n")
except:
pass
resList[ind] = rList
#print(rList,"\n",resList,'\n\n\n\n')
ind = ind + 1
for clue in dnClues:
words = clue.text.split()
rList = []
if(len(words) <= 3):
for word in words:
URL = "https://www.dictionaryapi.com/api/v3/references/thesaurus/json/"
URL = URL + word + "?key="+API_KEY
try:
response = requests.get(URL)
#data = json.loads(response.read())
#print(word, ind)
#print (response.json()[0]['meta']['syns'][0])
rList.append(response.json()[0]['meta']['syns'][0])
#resList[ind].append(response.json()[0]['meta']['syns'][0])
#print(rList,"\n\n")
except:
pass
resList[ind] = rList
#print(rList,"\n",resList,'\n\n\n\n')
ind = ind + 1
return resList
def searchRevDict(acClues,dnClues):
url = "https://reversedictionary.org/"
resl = scraper.findInputPutKey(url, "query",acClues,dnClues)
return resl