-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateGraph.js
235 lines (199 loc) · 8.44 KB
/
createGraph.js
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
let admin = require("firebase-admin");
const ug = require('ug');
let serviceAccount = require('./acckey');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://hotel-finder-e69a6.firebaseio.com/'
});
// Get a database reference to our posts
let db = admin.database();
let ref = db.ref("/hotels_new",);
const graph = new ug.Graph();
//create variables to store data
let hotels = [];
let cities = [];
let categories = [];
let ratings = [];
let countries = [];
let descriptions = [];
let states = [];
let sentimentals = [];
let localities = [];
let location = [];
let state = [];
let country = [];
let description = [];
let category = [];
let rating = [];
let sentiment = [];
let locality = [];
let areas = [];
// quick and dirty function to get a node by rating
function getHotelNode(nodes, prop){
return graph.nodes(nodes).query().filter({hotel_name: prop}).first();
}
function getCityNode(nodes, prop){
return graph.nodes(nodes).query().filter({city: prop}).first();
}
function getCategoryNode(nodes, prop){
return graph.nodes(nodes).query().filter({category: prop}).first();
}
function getRatingNode(nodes, prop){
return graph.nodes(nodes).query().filter({rating: prop}).first();
}
function getCountryNode(nodes, prop){
return graph.nodes(nodes).query().filter({country: prop}).first();
}
function getStateNode(nodes, prop){
return graph.nodes(nodes).query().filter({state: prop}).first();
}
function getDescriptionNode(nodes, prop){
return graph.nodes(nodes).query().filter({description: prop}).first();
}
function getSentimentNode(nodes, prop){
return graph.nodes(nodes).query().filter({sentiment: prop}).first();
}
function getLocalityNode(node, prop){
return graph.nodes(node).query().filter({locality: prop}).first();
}
//fetch data
ref.orderByChild('Property_Name').once('value')
.then((data)=>{
let dat = data.val();
console.log(data.numChildren());
for (key in dat) {
if(dat.hasOwnProperty(key)){
//console.log(dat[key].hotel_name);
if (hotels.includes(dat[key].Property_Name) === false) hotels.push(dat[key].Property_Name);
if (cities.includes(dat[key].City) === false) cities.push(dat[key].City);
if (categories.includes(dat[key].Property_Type) === false) categories.push(dat[key].Property_Type);
if (ratings.includes(dat[key].Site_Review_Rating) === false) ratings.push(dat[key].Site_Review_Rating);
if (countries.includes(dat[key].Country) === false) countries.push(dat[key].Country);
if (descriptions.includes(dat[key].rating) === false) descriptions.push(dat[key].Hotel_Description);
if (states.includes(dat[key].rating) === false) states.push(dat[key].State);
if (sentimentals.includes(dat[key].Sentimental_Value) === false) sentimentals.push(dat[key].Sentimental_Value);
if (localities.includes(dat[key].Locality) === false) localities.push((dat[key].Locality.trim()+', '+dat[key].City.trim()));
/* location = {...{hotel_name: dat[key].hotel_name, city:dat[key].city} };
category = {...{hotel_name: dat[key].hotel_name, city:dat[key].category} };
rating = {...{hotel_name: dat[key].hotel_name, city:dat[key].rating} };*/
let loc = {hotel_name: dat[key].Property_Name, location: dat[key].City};
let cat = {hotel_name: dat[key].Property_Name, category: dat[key].Property_Type};
let rat = {hotel_name: dat[key].Property_Name, rating: dat[key].Site_Review_Rating};
let sta = {hotel_name: dat[key].Property_Name, state: dat[key].State};
let coun = {hotel_name: dat[key].Property_Name, country: dat[key].Country};
let des = {hotel_name: dat[key].Property_Name, description: dat[key].Hotel_Description};
let sen = {hotel_name: dat[key].Property_Name, sentiment: dat[key].Sentimental_Value};
let area = {city: dat[key].City, locality: (dat[key].Locality.trim()+', '+dat[key].City.trim())};
let areah = {hotel_name: dat[key].Property_Name, locality: (dat[key].Locality.trim()+', '+dat[key].City.trim())};
location.push(loc);
category.push(cat);
rating.push(rat);
state.push(sta);
country.push(coun);
description.push(des);
sentiment.push(sen);
locality.push(area);
areas.push(areah);
}
}
return hotels, cities, categories, ratings,country,countries,description,descriptions,states,state, sentiment, sentimentals, location, category, rating, locality, localities, areas;
})
.then(()=>{
// Add to graph
hotels.forEach(function(hotel) {
graph.createNode('hotel', {hotel_name: hotel});
});
cities.forEach(function(city) {
graph.createNode('city', {city: city});
});
categories.forEach(function(category) {
graph.createNode('category', {category: category});
});
ratings.forEach(function(rating) {
graph.createNode('rating', {rating: rating});
});
countries.forEach(function(country) {
graph.createNode('country', {country: country});
});
descriptions.forEach(function(description) {
graph.createNode('description', {description: description});
});
states.forEach(function(state) {
graph.createNode('state', {state: state});
});
sentimentals.forEach(function(sentiment) {
graph.createNode('sentiment', {sentiment: sentiment});
});
localities.forEach(function (locality) {
graph.createNode('locality', {locality: locality});
})
})
.then(()=>{
location.forEach(function(dat) {
//console.log(getNode('category','Hotel'));
//console.log(getNode('hotel', dat.hotel_name));
graph.createEdge('location').link(
getHotelNode('hotel', dat.hotel_name),
getCityNode('city', dat.location)
).setDistance(1);
});
locality.forEach(function (dat) {
graph.createEdge('locality').link(
getCityNode('city',dat.city),
getLocalityNode('locality',dat.locality)
).setDistance(1);
});
areas.forEach(function (dat) {
graph.createEdge('area').link(
getHotelNode('hotel', dat.hotel_name),
getLocalityNode('locality',dat.locality)
).setDistance(1);
});
category.forEach(function(dat) {
graph.createEdge('category').link(
getHotelNode('hotel',dat.hotel_name),
getCategoryNode('category',dat.category)
).setDistance(2);
});
rating.forEach(function(dat) {
graph.createEdge('rating').link(
getHotelNode('hotel',dat.hotel_name),
getRatingNode('rating',dat.rating)
).setDistance(6);
});
state.forEach(function(dat) {
graph.createEdge('state').link(
getHotelNode('hotel',dat.hotel_name),
getStateNode('state',dat.state)
).setDistance(4);
});
country.forEach(function(dat) {
graph.createEdge('country').link(
getHotelNode('hotel',dat.hotel_name),
getCountryNode('country',dat.country)
).setDistance(8);
});
description.forEach(function(dat) {
graph.createEdge('description').link(
getHotelNode('hotel',dat.hotel_name),
getDescriptionNode('description',dat.description)
).setDistance(7);
});
sentiment.forEach(function(dat) {
graph.createEdge('sentiment').link(
getHotelNode('hotel',dat.hotel_name),
getSentimentNode('sentiment',dat.sentiment)
).setDistance(7);
});
return location, category, rating, state, country, description, sentiment;
})
.then(()=>{
// save graph
console.log(graph.nodeCount());
console.log(graph.edgeCount());
graph.save('./graph.ugd', function() {
//doAfterSave();
console.log('Graph saved into file');
process.exit();
});
});