-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
139 lines (125 loc) · 5.77 KB
/
main.rb
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
require 'rubygems'
require 'open-uri'
require 'sentimental'
require 'json'
require 'neo4j-core'
class GooglePlaces
KEY = 'AIzaSyAlhwar36zhTpcOZkkA4_t9wEWU95reGPY'
PLACEURL = 'https://maps.googleapis.com/maps/api/place'
GEOCODEURL = 'https://maps.googleapis.com/maps/api/geocode'
attr_accessor :session, :analyzer, :mode
def initialize(mode)
@session = Neo4j::Session.open(:server_db) if mode == :neo4j
Sentimental.load_defaults
@analyzer = Sentimental.new
end
def load_data
files = [
{ name: "Indiranagar", file: 'indiranagar', coords: { lat: 12.9718915, lng: 77.6411545 }, bounds: { northeast: {lat: 12.985708,lng: 77.6481491}, southwest: {lat: 12.9605021,lng: 77.628361 } } },
{ name: "HSR Layout", file: 'hsr', coords: { lat: 12.9081357, lng: 77.64760799999999 } ,bounds: { northeast: { lat: 12.924701, lng: 77.668761 }, southwest: { lat: 12.8958159, lng: 77.6225599 } } },
{ name: "Koramangala", file: 'koramangala', coords: { lat: 12.9279232, lng: 77.62710779999999 }, bounds: { northeast: { lat: 12.949918, lng: 77.6541309 }, southwest: { lat: 12.916524, lng: 77.610254 } } },
]
# files.each { |file| commit_neo4j(file) } - Uncomment to push this to Neo4J
files.each_with_index { |file, ix| files[ix] = commit_json(file) }
File.open("data/locations.json", 'w') {|f| f.write(files.to_json) }
end
def commit_neo4j(file)
area = Neo4j::Node.create({name: file[:name], food_sentiment: 0, education_sentiment: 0, healthcare_sentiment: 0}, :area)
data = fetch_ammenities(file[:coords][:lat], file[:coords][:lng])
scores = { }
data.keys.each do |key|
scores[key] = []
data[key].each do |entity|
scores[key] << commit_entity(entity, key, area)
end
end
scores.keys.each { |key| area["#{key.to_s}_sentiment".to_sym] = calculate_average(scores[key]) }
end
def commit_json(file)
scores = { }
data = fetch_ammenities(file[:coords][:lat], file[:coords][:lng])
data.keys.each do |key|
scores[key] = []
data[key].each do |entity|
score = entity['rating'] || 0
(entity["comments"] || []).each{ |comment| scores[key] << calculate_score(score, comment[:sentiment]) }
end
end
scores.keys.each { |key| file["#{key}_average_sentiment"] = calculate_average(scores[key]) }
File.open("data/#{file[:file]}.json", 'w') {|f| f.write(data.to_json) }
file
end
def commit_entity(entity, type, parent)
score = entity['rating'] || 0
location_type = "has_#{type.to_s}_option".to_sym
comment_type = "has_#{type.to_s}_option_comment".to_sym
node = Neo4j::Node.create({
name: entity["name"],
latitude: entity["geometry"]["location"]["lat"],
longitude: entity["geometry"]["location"]["lng"],
address: entity["address"],
rating: entity["rating"],
icon: entity['icon'],
place_id: entity['place_id'],
website: entity['website'],
phone_no: entity['phone_no']
}, type)
parent.create_rel(location_type, node)
(entity["comments"] || []).each do |comment|
comment_node = Neo4j::Node.create({comment: comment[:text], sentiment: comment[:sentiment]}, :comment)
parent.create_rel(comment_type, comment_node)
score = calculate_score(score, sentiment)
end
score
end
def calculate_score(score, sentiment)
score = sentiment == :positive ? score + 1 : score - 1
score = 5 if score > 5
score = 0 if score < 0
score
end
def calculate_average(scores)
avg_score = 0
scores.each { |score| avg_score += score }
avg_score = avg_score == 0 ? 0 : avg_score / scores.size
end
def parse_json(url_part, type)
url = type.eql?('place') ? GooglePlaces::PLACEURL : GooglePlaces::GEOCODEURL
uri = URI.parse(URI.encode("#{url}/#{url_part}"))
uri.query += uri.query.empty? ? "key=#{key}" : "&key=#{GooglePlaces::KEY}"
JSON.parse(open(uri.to_s).read)
end
def fetch_ammenities(lat, long)
parsers = {
food: %w(food bakery grocery_or_supermarket meal_delivery meal_takeaway restaurant),
healthcare: %w(dentist doctor health hospital pharmacy physiotherapist),
education: %w(school university),
}
data = parse_json("nearbysearch/json?location=#{lat},#{long}&radius=1000&types=food|park|" +
"bakery|cafe|clothing_store|dentist|doctor|department_store|grocery_or_supermarket|health|" +
"hospital|meal_delivery|meal_takeaway|pharmacy|physiotherapist|restaurant|school|shopping_" +
"mall|store|university", 'place')
data = data['results'].map { |row| row.select { |key, value| %w(geometry icon id name types place_id).include?(key.to_s) } }
data.each_with_index do |rec, ix|
details = parse_json("details/json?placeid=#{rec['place_id']}", 'place')
data[ix] = data[ix].merge({
'address' => details['result']['formatted_address'],
'rating' => details['result']['rating'],
'website' => details['result']['website'],
'comments' => [],
'phone_no' => details['international_phone_number'],
})
(details['result']['reviews'] || []).map { |rw| rw['text'] }.each do |comment|
data[ix]['comments'] << { text: comment, sentiment: @analyzer.get_sentiment(comment) }
end
end
output = {
food: data.select { |row| (row['types'] - parsers[:food]).size < row['types'].size },
healthcare: data.select { |row| (row['types'] - parsers[:healthcare]).size < row['types'].size },
education: data.select { |row| (row['types'] - parsers[:education]).size < row['types'].size },
}
output
end
end
gp = GooglePlaces.new(:json)
gp.load_data