forked from adialachar/CS172ProjTwo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elastic.py
236 lines (133 loc) · 4.91 KB
/
elastic.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import re
import functions
from flask import Flask, render_template, request
import googlemaps
app = Flask(__name__)
#test to make sure Elastic search is up and running
import requests
res = requests.get('http://localhost:9200')
print(res.content)
#connect to cluster
from elasticsearch import Elasticsearch
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
#index test data
es.index(index = 'test-index', doc_type = 'test', id = 1, body = {'test' : 'test'})
#delete test data
es.delete(index = 'test-index', doc_type = 'test', id = 1)
#itereate over json file and index them
import json
r = requests.get('http://localhost:9200')
file = open("file.json", 'r').read()
newData = file.splitlines(True)
i = 0
json_string = ""
docs = {}
locations = []
for line in newData:
line = ''.join(line.split())
for word in line:
if word != "}":
json_string += word
#json_string += "_"
#print(type(json_string))
else:
docs[i] = json_string + "}"
json_string = ""
es.index(index = 'tweets', doc_type = 'tweet', id = i, body = docs[i])
i+=1
#res = es.get(index='tweets', doc_type='tweet', id=0)
#print (res['_source'])
#print(type(res['_source']))
def find_average_tweet_length():
avg_length = 0
for i in range (0,69):
res = es.get(index='tweets', doc_type = 'tweet',id = i)
bag = res['_source']['tweet']
avg_length += len(bag)
return ((float(avg_length)/100))
def find_number_of_docs(term):
n = 0
for i in range(0,69):
res = es.get(index = 'tweets', doc_type = 'tweet', id = i)
if (len(re.findall(term, res['_source']['tweet'])) > 0):
n += 1
return n
@app.route("/", methods=['GET', 'POST'])
def main():
tweets_and_scores = []
time_sorter = []
if request.method == 'POST':
query = request.form.get('query',-1)
#query = "girlfriend offered"
terms = query.split()
AVG = find_average_tweet_length()
n = 0
for i in range(0,69):
res = es.get(index = 'tweets', doc_type = 'tweet', id = i)
#print(res['_source'])
#print(res['_source']['tweet'])
K = (1.2 * (0.25))
K += (0.75) * ((len(res['_source']['tweet']))/AVG)
#print(K)
score = 0
for j in range(0, len(terms)):
f = re.findall(terms[j], res['_source']['tweet'])
n = find_number_of_docs(terms[j])
if (len(f) is not 0):
score += functions.BM25(n,K,len(f))
t = (res['_source'], score)
#print(t)
tweets_and_scores.append(t)
tweets_and_scores = sorted(tweets_and_scores, key=lambda tweets_and_scores: tweets_and_scores[1])
tweets_and_scores = tweets_and_scores[-10:]
#tweets_and_scores = sorted(tweets_and_scores, key=lambda tweets_and_scores: tweets_and_scores[0]['Time'])
#print(tweets_and_scores[1][0]['Time'])
no_time_list = []
for i in range(0,10):
#t = (tweets_and_scores[i][0]['Time'],i)
#print(tweets_and_scores[i][0])
if 'Time' in tweets_and_scores[i][0].keys():
t = (tweets_and_scores[i][0]['Time'],i)
time_sorter.append(t)
else:
no_time_list.append(i)
time_sorter = sorted(time_sorter, key=lambda time_sorter: time_sorter[0])
#print(time_sorter)
#time_sorter.reverse()
#print(time_sorter)
sorted_tweets = []
for i in range(0, len(time_sorter)):
sorted_tweets.append(tweets_and_scores[i])
#sorted_tweets.extend(no_time_list)
for i in range(0, len(no_time_list)):
sorted_tweets.append(tweets_and_scores[i])
#print(sorted_tweets)
just_tweets = []
for i in range(0,len(sorted_tweets)):
#just_tweets.append(sorted_tweets[i][0]['tweet'])
#print(sorted_tweets[i])
#print(type(sorted_tweets[i]))
if type(sorted_tweets[i] == tuple):
just_tweets.append(sorted_tweets[i][0]['tweet'])
locations.append(sorted_tweets[i][0]['Location'])
#elif type(sorted_tweets[i] == dict):
# just_tweets.append(sorted_tweets[i]['tweet'])
print(locations)
return render_template('index.html', just_tweets = just_tweets,coordinate_pairs = coordinate_pairs)
return render_template('index.html')
@app.route("/map",methods=['GET','POST'])
def map():
coordinate_pairs = []
gmaps = googlemaps.Client(key=key)
#geocode = gmaps.geocode('Miami,FL')
current_location = (33.97305556, -117.3280555633) #Hardcode for UCR if browser location unavailab
for i in range(0, len(locations)):
geocode = gmaps.geocode(locations[i])
coords = geocode[0]['geometry']['location']
#1.4450867052 is approximately 100 miles in latitude and longitude. This is a rough estimate. We found that this approach was much faster than using the google maps library to calculate the distance between two coordinates
if (len(coords) is not 0):
if (abs(current_location[0] - coords['lat']) > 1.4450867052 or abs(current_location[1] - coords['lng']) > 1.4450867052):
coordinate_pairs.append((coords['lat'], coords['lng']))
return render_template('maps.html', coordinate_pairs = coordinate_pairs)
if __name__ == "__main__":
app.run()