2014-10-17 09:07:06 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2014-10-03 02:43:56 +00:00
|
|
|
/*
|
2015-01-05 06:10:10 +00:00
|
|
|
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2014-10-03 02:43:56 +00:00
|
|
|
|
2014-09-17 07:39:46 +00:00
|
|
|
'use strict';
|
|
|
|
|
2014-11-17 06:58:32 +00:00
|
|
|
var _ = require('underscore');
|
|
|
|
var geolib = require('geolib');
|
|
|
|
var mysql = require('mysql');
|
|
|
|
var pool = null;
|
|
|
|
|
2014-09-17 07:39:46 +00:00
|
|
|
|
|
|
|
function innerProduct(values1, values2) {
|
|
|
|
var result = 0.0;
|
|
|
|
|
|
|
|
for (var feature in values1) {
|
2015-01-15 11:06:22 +00:00
|
|
|
if (feature in values2) {
|
|
|
|
result += values1[feature] * values2[feature];
|
|
|
|
}
|
2014-09-17 07:39:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-11-08 02:23:42 +00:00
|
|
|
function walkMatches(data, features, minScore, callback) {
|
2015-01-15 11:06:22 +00:00
|
|
|
for (var i = 0, count = data.length; i < count; ++i) {
|
|
|
|
var record = data[i];
|
2014-11-17 08:43:27 +00:00
|
|
|
var score = innerProduct(features, record.features);
|
2014-09-17 07:39:46 +00:00
|
|
|
|
|
|
|
if (score >= minScore) {
|
2014-09-19 00:38:58 +00:00
|
|
|
callback(record, score);
|
2014-09-17 07:39:46 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-19 00:38:58 +00:00
|
|
|
}
|
|
|
|
|
2014-11-08 02:23:42 +00:00
|
|
|
function countRecords(data, features, minScore) {
|
2014-09-19 00:38:58 +00:00
|
|
|
var count = 0;
|
2014-11-08 02:23:42 +00:00
|
|
|
walkMatches(data, features, minScore, function(record, score) {
|
2014-09-19 00:38:58 +00:00
|
|
|
++count;
|
|
|
|
});
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2014-11-17 08:14:41 +00:00
|
|
|
function findRecords(data, features, minScore) {
|
2014-09-19 00:38:58 +00:00
|
|
|
var results = [];
|
2015-01-15 11:06:22 +00:00
|
|
|
|
2014-11-08 02:23:42 +00:00
|
|
|
walkMatches(data, features, minScore, function(record, score) {
|
2014-09-19 00:38:58 +00:00
|
|
|
results.push({
|
2015-01-05 06:54:40 +00:00
|
|
|
name: record.name,
|
|
|
|
score: score,
|
|
|
|
distanceToUser: record.distanceToUser / 1000.0,
|
|
|
|
distanceToStn: record.distanceToStn / 1000.0,
|
|
|
|
closestStn: record.closestStn,
|
|
|
|
id: record.id
|
2014-09-19 00:38:58 +00:00
|
|
|
});
|
|
|
|
});
|
2014-09-17 07:39:46 +00:00
|
|
|
|
|
|
|
results.sort(function(a, b) {
|
|
|
|
return b.score - a.score;
|
|
|
|
});
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
function step(range, steps, callback) {
|
|
|
|
var stepSize = (range.max - range.min) / steps;
|
|
|
|
|
|
|
|
for (var i = 0; i < steps; ++i) {
|
|
|
|
var stepMax = range.max - stepSize * i;
|
|
|
|
var stepMin = stepMax - stepSize;
|
|
|
|
var stepMid = (stepMin + stepMax) / 2;
|
|
|
|
|
|
|
|
callback(stepMid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-08 02:23:42 +00:00
|
|
|
function project(data, features, feature, minScore, range, steps) {
|
|
|
|
var sample = _.clone(features);
|
|
|
|
var results = [];
|
2014-09-17 07:39:46 +00:00
|
|
|
|
|
|
|
step(range, steps, function(position) {
|
2014-11-08 02:23:42 +00:00
|
|
|
sample[feature] = position;
|
2014-09-17 07:39:46 +00:00
|
|
|
results.push({
|
|
|
|
sample: position,
|
2014-11-08 02:23:42 +00:00
|
|
|
count: countRecords(data, sample, minScore)
|
2014-09-17 07:39:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2014-11-08 02:23:42 +00:00
|
|
|
function buildHints(data, features, feature, minScore, range, steps) {
|
2014-09-17 07:39:46 +00:00
|
|
|
var projection = project(
|
|
|
|
data,
|
2014-11-08 02:23:42 +00:00
|
|
|
features,
|
|
|
|
feature,
|
2014-09-17 07:39:46 +00:00
|
|
|
minScore,
|
|
|
|
range,
|
|
|
|
steps
|
|
|
|
);
|
|
|
|
|
|
|
|
var hints = [];
|
|
|
|
_.each(projection, function(result) {
|
|
|
|
hints.push({
|
|
|
|
sample: result.sample,
|
|
|
|
count: result.count
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return hints;
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadDb(params) {
|
2014-10-04 09:35:22 +00:00
|
|
|
pool = mysql.createPool(params);
|
2014-09-17 07:39:46 +00:00
|
|
|
}
|
|
|
|
|
2015-01-05 09:08:24 +00:00
|
|
|
function getRecords(context, callback) {
|
2014-10-04 09:35:22 +00:00
|
|
|
pool.query('SELECT * FROM reviews', function(err, rows) {
|
2014-09-17 07:39:46 +00:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
var records = _.map(rows, function(row) {
|
|
|
|
return {
|
2015-01-05 06:54:40 +00:00
|
|
|
name: row.name,
|
|
|
|
id: row.id,
|
|
|
|
closestStn: row.closestStn,
|
|
|
|
distanceToStn: row.distanceToStn,
|
2014-11-17 07:29:02 +00:00
|
|
|
geo: {
|
|
|
|
latitude: row.latitude,
|
|
|
|
longitude: row.longitude
|
|
|
|
},
|
2014-11-17 08:43:27 +00:00
|
|
|
features: {
|
2015-01-05 06:10:10 +00:00
|
|
|
delicious: row.delicious,
|
|
|
|
accomodating: row.accomodating,
|
|
|
|
affordable: row.affordable,
|
|
|
|
atmospheric: row.atmospheric
|
2014-11-17 07:29:02 +00:00
|
|
|
},
|
2014-09-17 07:39:46 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2015-01-05 09:08:24 +00:00
|
|
|
computeRecordGeo(records, context);
|
2014-09-17 07:39:46 +00:00
|
|
|
callback(records);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-01-05 09:08:24 +00:00
|
|
|
function computeRecordGeo(records, context) {
|
2015-01-05 06:36:27 +00:00
|
|
|
var distUserMin = Number.MAX_VALUE;
|
|
|
|
var distUserMax = Number.MIN_VALUE;
|
2014-11-17 08:43:27 +00:00
|
|
|
|
2014-11-17 08:14:41 +00:00
|
|
|
_.each(records, function(record) {
|
2015-01-05 06:36:27 +00:00
|
|
|
record.distanceToUser = 0.0;
|
2015-01-05 09:08:24 +00:00
|
|
|
if (context.geo) {
|
|
|
|
record.distanceToUser = geolib.getDistance(record.geo, context.geo);
|
2014-11-17 08:14:41 +00:00
|
|
|
}
|
2014-11-17 08:43:27 +00:00
|
|
|
|
2015-01-05 06:36:27 +00:00
|
|
|
distUserMin = Math.min(distUserMin, record.distanceToUser);
|
|
|
|
distUserMax = Math.max(distUserMax, record.distanceToUser);
|
2014-11-17 08:43:27 +00:00
|
|
|
});
|
|
|
|
|
2015-01-05 06:36:27 +00:00
|
|
|
var distUserRange = distUserMax - distUserMin;
|
2014-11-17 08:43:27 +00:00
|
|
|
|
|
|
|
_.each(records, function(record) {
|
2015-01-05 06:36:27 +00:00
|
|
|
record.features.nearby = -((record.distanceToUser - distUserMin) / distUserRange - 0.5) * 2.0;
|
|
|
|
|
2015-01-05 09:08:24 +00:00
|
|
|
record.features.accessible = 1.0 - (record.distanceToStn / context.walkingDist);
|
2015-01-05 06:36:27 +00:00
|
|
|
record.features.accessible = Math.min(record.features.accessible, 1.0);
|
|
|
|
record.features.accessible = Math.max(record.features.accessible, -1.0);
|
2014-11-17 08:14:41 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-01-15 11:06:22 +00:00
|
|
|
function sanitizeQuery(query) {
|
|
|
|
var keys = [
|
|
|
|
'delicious',
|
|
|
|
'accomodating',
|
|
|
|
'affordable',
|
|
|
|
'atmospheric',
|
|
|
|
'nearby',
|
|
|
|
'accessible'
|
|
|
|
];
|
|
|
|
|
|
|
|
var features = {};
|
|
|
|
_.each(keys, function(key) {
|
2015-03-11 03:11:19 +00:00
|
|
|
features[key] = _.has(query.features, key) ? query.features[key] : 0;
|
2015-01-15 11:06:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
query.features = features;
|
2015-03-11 03:11:19 +00:00
|
|
|
|
|
|
|
for (var category in query.profile) {
|
|
|
|
if (parseFloat(query.profile[category]) === 0) {
|
|
|
|
delete query.profile[category];
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 11:06:22 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 01:55:32 +00:00
|
|
|
function getCategories(callback) {
|
|
|
|
pool.query('SELECT * FROM categories', function(err, rows) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
var categories = _.map(rows, function(row) {
|
|
|
|
return {id: row.id, description: row.description};
|
|
|
|
});
|
|
|
|
|
2015-03-11 05:41:17 +00:00
|
|
|
callback(categories);
|
2015-03-03 01:55:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addCategory(query, callback) {
|
2015-03-03 09:52:33 +00:00
|
|
|
var description = query.description.trim();
|
2015-03-03 01:55:32 +00:00
|
|
|
|
2015-03-03 09:52:33 +00:00
|
|
|
if (description) {
|
2015-03-11 05:41:17 +00:00
|
|
|
pool.query('INSERT INTO categories(description) VALUES(?)', [description], function(err, info) {
|
2015-03-03 09:52:33 +00:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback({
|
2015-03-11 05:41:17 +00:00
|
|
|
id: info.insertId,
|
2015-03-03 09:52:33 +00:00
|
|
|
description: description,
|
|
|
|
success: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callback({success: false});
|
|
|
|
}
|
2015-03-03 01:55:32 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 10:20:19 +00:00
|
|
|
function accessReview(query, callback) {
|
|
|
|
pool.query('SELECT url FROM reviews WHERE id = (?) LIMIT 1', [query.id], function(err, rows) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
var results = {
|
|
|
|
success: rows.length > 0
|
|
|
|
};
|
|
|
|
|
|
|
|
if (results.success) {
|
|
|
|
results.url = 'http://www.tripadvisor.com' + rows[0].url;
|
2015-03-11 06:50:00 +00:00
|
|
|
|
|
|
|
pool.query('INSERT INTO history(date, reviewId) VALUES(NOW(), ?)', [query.id], function(err, info) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2015-03-11 06:54:34 +00:00
|
|
|
var pitcher = function(err, info) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-11 06:50:00 +00:00
|
|
|
for (var categoryId in query.profile) {
|
|
|
|
pool.query(
|
|
|
|
'INSERT INTO historyGroups(categoryId, categoryValue, historyId) VALUES(?, ?, ?)',
|
2015-03-11 06:54:34 +00:00
|
|
|
[categoryId, query.profile[categoryId], info.insertId],
|
|
|
|
pitcher
|
2015-03-11 06:50:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2015-03-09 10:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
callback(results);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-03 12:40:27 +00:00
|
|
|
function runQuery(query, callback) {
|
2015-01-15 11:06:22 +00:00
|
|
|
sanitizeQuery(query);
|
|
|
|
|
2015-01-05 09:08:24 +00:00
|
|
|
var context = {
|
|
|
|
geo: query.geo,
|
|
|
|
walkingDist: query.walkingDist * 1000.0
|
|
|
|
};
|
|
|
|
|
2015-01-13 07:36:22 +00:00
|
|
|
getRecords(context, function(data) {
|
2014-09-17 07:39:46 +00:00
|
|
|
var searchResults = findRecords(
|
|
|
|
data,
|
2014-11-08 02:23:42 +00:00
|
|
|
query.features,
|
2014-09-26 04:17:43 +00:00
|
|
|
query.minScore
|
2014-09-17 07:39:46 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
var graphColumns = {};
|
2014-11-08 02:23:42 +00:00
|
|
|
for (var feature in query.features) {
|
2014-09-17 07:39:46 +00:00
|
|
|
var searchHints = buildHints(
|
|
|
|
data,
|
2014-11-08 02:23:42 +00:00
|
|
|
query.features,
|
|
|
|
feature,
|
2014-09-26 04:17:43 +00:00
|
|
|
query.minScore,
|
2014-11-08 02:23:42 +00:00
|
|
|
query.range,
|
2014-09-17 07:39:46 +00:00
|
|
|
query.hintSteps
|
|
|
|
);
|
|
|
|
|
2014-11-08 02:23:42 +00:00
|
|
|
graphColumns[feature] = {
|
|
|
|
value: query.features[feature],
|
2014-09-17 07:39:46 +00:00
|
|
|
hints: searchHints,
|
|
|
|
steps: query.hintSteps
|
2014-10-17 09:07:06 +00:00
|
|
|
};
|
2014-09-17 07:39:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
callback({
|
|
|
|
columns: graphColumns,
|
|
|
|
items: searchResults.slice(0, query.maxResults),
|
|
|
|
count: searchResults.length
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2014-11-17 07:29:02 +00:00
|
|
|
loadDb: loadDb,
|
2015-03-03 12:40:27 +00:00
|
|
|
runQuery: runQuery,
|
2015-03-03 01:55:32 +00:00
|
|
|
getCategories: getCategories,
|
2015-03-09 10:20:19 +00:00
|
|
|
addCategory: addCategory,
|
|
|
|
accessReview: accessReview
|
2014-09-17 07:39:46 +00:00
|
|
|
};
|