2015-03-15 08:10:55 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _ = require('underscore');
|
2015-03-17 04:23:32 +00:00
|
|
|
var async = require('async');
|
2015-03-15 08:10:55 +00:00
|
|
|
var geolib = require('geolib');
|
|
|
|
var mysql = require('mysql');
|
|
|
|
var pool = null;
|
|
|
|
|
|
|
|
|
|
|
|
function innerProduct(values1, values2) {
|
|
|
|
var result = 0.0;
|
|
|
|
|
|
|
|
for (var feature in values1) {
|
|
|
|
if (feature in values2) {
|
|
|
|
result += values1[feature] * values2[feature];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function walkMatches(data, features, minScore, callback) {
|
|
|
|
for (var i = 0, count = data.length; i < count; ++i) {
|
|
|
|
var record = data[i];
|
|
|
|
var score = innerProduct(features, record.features);
|
|
|
|
|
|
|
|
if (score >= minScore) {
|
|
|
|
callback(record, score);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-21 11:46:20 +00:00
|
|
|
function statRecords(data, features, minScore) {
|
|
|
|
var compatibility = 0;
|
|
|
|
var count = 0;
|
|
|
|
|
2015-03-15 08:10:55 +00:00
|
|
|
walkMatches(data, features, minScore, function(record, score) {
|
2015-03-22 02:51:05 +00:00
|
|
|
compatibility += record.compatibility * score;
|
2015-03-15 08:10:55 +00:00
|
|
|
++count;
|
|
|
|
});
|
|
|
|
|
2015-03-21 11:46:20 +00:00
|
|
|
return {
|
|
|
|
compatibility: compatibility,
|
|
|
|
count: count
|
|
|
|
};
|
2015-03-15 08:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function findRecords(data, features, minScore) {
|
|
|
|
var results = [];
|
|
|
|
|
|
|
|
walkMatches(data, features, minScore, function(record, score) {
|
|
|
|
results.push({
|
|
|
|
name: record.name,
|
|
|
|
score: score,
|
|
|
|
distanceToUser: record.distanceToUser / 1000.0,
|
|
|
|
distanceToStn: record.distanceToStn / 1000.0,
|
|
|
|
closestStn: record.closestStn,
|
2015-03-17 09:41:52 +00:00
|
|
|
accessCount: record.accessCount,
|
2015-03-15 08:10:55 +00:00
|
|
|
id: record.id
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function project(data, features, feature, minScore, range, steps) {
|
|
|
|
var sample = _.clone(features);
|
|
|
|
var results = [];
|
|
|
|
|
|
|
|
step(range, steps, function(position) {
|
|
|
|
sample[feature] = position;
|
|
|
|
results.push({
|
|
|
|
sample: position,
|
2015-03-21 11:46:20 +00:00
|
|
|
stats: statRecords(data, sample, minScore)
|
2015-03-15 08:10:55 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildHints(data, features, feature, minScore, range, steps) {
|
|
|
|
var projection = project(
|
|
|
|
data,
|
|
|
|
features,
|
|
|
|
feature,
|
|
|
|
minScore,
|
|
|
|
range,
|
|
|
|
steps
|
|
|
|
);
|
|
|
|
|
|
|
|
var hints = [];
|
|
|
|
_.each(projection, function(result) {
|
|
|
|
hints.push({
|
|
|
|
sample: result.sample,
|
2015-03-21 11:46:20 +00:00
|
|
|
stats: result.stats
|
2015-03-15 08:10:55 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return hints;
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadDb(params) {
|
|
|
|
pool = mysql.createPool(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRecords(context, callback) {
|
|
|
|
pool.query('SELECT * FROM reviews', function(err, rows) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
var records = _.map(rows, function(row) {
|
|
|
|
return {
|
|
|
|
name: row.name,
|
|
|
|
id: row.id,
|
|
|
|
closestStn: row.closestStn,
|
|
|
|
distanceToStn: row.distanceToStn,
|
2015-03-17 09:41:52 +00:00
|
|
|
accessCount: row.accessCount,
|
2015-03-15 08:10:55 +00:00
|
|
|
geo: {
|
|
|
|
latitude: row.latitude,
|
|
|
|
longitude: row.longitude
|
|
|
|
},
|
|
|
|
features: {
|
|
|
|
delicious: row.delicious,
|
|
|
|
accomodating: row.accomodating,
|
|
|
|
affordable: row.affordable,
|
|
|
|
atmospheric: row.atmospheric
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
computeRecordGeo(records, context);
|
2015-03-17 09:41:52 +00:00
|
|
|
computeRecordPopularity(records, context, callback);
|
2015-03-15 08:10:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function computeRecordGeo(records, context) {
|
|
|
|
var distUserMin = Number.MAX_VALUE;
|
|
|
|
var distUserMax = Number.MIN_VALUE;
|
|
|
|
|
|
|
|
_.each(records, function(record) {
|
|
|
|
record.distanceToUser = 0.0;
|
|
|
|
if (context.geo) {
|
|
|
|
record.distanceToUser = geolib.getDistance(record.geo, context.geo);
|
|
|
|
}
|
|
|
|
|
|
|
|
distUserMin = Math.min(distUserMin, record.distanceToUser);
|
|
|
|
distUserMax = Math.max(distUserMax, record.distanceToUser);
|
|
|
|
});
|
|
|
|
|
|
|
|
var distUserRange = distUserMax - distUserMin;
|
|
|
|
|
|
|
|
_.each(records, function(record) {
|
|
|
|
record.features.nearby = -((record.distanceToUser - distUserMin) / distUserRange - 0.5) * 2.0;
|
|
|
|
|
|
|
|
record.features.accessible = 1.0 - (record.distanceToStn / context.walkingDist);
|
|
|
|
record.features.accessible = Math.min(record.features.accessible, 1.0);
|
|
|
|
record.features.accessible = Math.max(record.features.accessible, -1.0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-17 09:41:52 +00:00
|
|
|
function computeRecordPopularity(records, context, callback) {
|
2015-03-17 06:23:57 +00:00
|
|
|
async.each(
|
2015-03-17 04:23:32 +00:00
|
|
|
records,
|
|
|
|
function(record, callback) {
|
2015-03-17 06:23:57 +00:00
|
|
|
pool.query(
|
|
|
|
'SELECT * FROM history WHERE reviewId = (?)',
|
|
|
|
[record.id],
|
|
|
|
function(err, rows) {
|
|
|
|
async.map(
|
|
|
|
rows,
|
|
|
|
function(row, callback) {
|
|
|
|
pool.query(
|
|
|
|
'SELECT * FROM historyGroups WHERE historyId = (?)',
|
|
|
|
[row.id],
|
|
|
|
function(err, historyGroupRows) {
|
|
|
|
var reviewFeatures = {};
|
|
|
|
_.each(historyGroupRows, function(historyGroupRow) {
|
|
|
|
reviewFeatures[historyGroupRow.categoryId] = historyGroupRow.categoryValue;
|
|
|
|
});
|
|
|
|
|
|
|
|
var groupScore = innerProduct(context.profile, reviewFeatures);
|
|
|
|
callback(err, groupScore);
|
2015-03-17 04:23:32 +00:00
|
|
|
}
|
2015-03-17 06:23:57 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
function(err, groupScores) {
|
2015-03-21 07:25:30 +00:00
|
|
|
var scoreAvg = 0;
|
|
|
|
if (groupScores.length > 0) {
|
|
|
|
var scoreSum = _.reduce(groupScores, function(a, b) { return a + b; });
|
|
|
|
scoreAvg = scoreSum / groupScores.length;
|
|
|
|
}
|
|
|
|
|
2015-03-21 11:46:20 +00:00
|
|
|
record.compatibility = scoreAvg;
|
2015-03-17 06:23:57 +00:00
|
|
|
callback(err);
|
2015-03-17 04:23:32 +00:00
|
|
|
}
|
2015-03-17 06:23:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2015-03-17 04:23:32 +00:00
|
|
|
},
|
2015-03-17 06:23:57 +00:00
|
|
|
function(err) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2015-03-17 04:23:32 +00:00
|
|
|
callback(records);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-03-17 07:28:16 +00:00
|
|
|
function fixupProfile(profile) {
|
|
|
|
var fixed = {};
|
2015-03-17 07:54:01 +00:00
|
|
|
_.each(JSON.parse(profile || '{}'), function(value, key) {
|
2015-03-17 07:28:16 +00:00
|
|
|
if (parseFloat(value) !== 0) {
|
|
|
|
fixed[key] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return fixed;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fixupFeatures(features) {
|
2015-03-15 08:10:55 +00:00
|
|
|
var keys = [
|
|
|
|
'delicious',
|
|
|
|
'accomodating',
|
|
|
|
'affordable',
|
|
|
|
'atmospheric',
|
|
|
|
'nearby',
|
2015-03-21 11:46:20 +00:00
|
|
|
'accessible'
|
2015-03-15 08:10:55 +00:00
|
|
|
];
|
|
|
|
|
2015-03-17 07:54:01 +00:00
|
|
|
if (!features) {
|
|
|
|
features = {};
|
|
|
|
}
|
|
|
|
|
2015-03-17 07:28:16 +00:00
|
|
|
var fixed = {};
|
2015-03-15 08:10:55 +00:00
|
|
|
_.each(keys, function(key) {
|
2015-03-17 07:54:01 +00:00
|
|
|
fixed[key] = features[key] || 0;
|
2015-03-15 08:10:55 +00:00
|
|
|
});
|
|
|
|
|
2015-03-17 07:28:16 +00:00
|
|
|
return fixed;
|
2015-03-15 08:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getCategories(callback) {
|
|
|
|
pool.query('SELECT * FROM categories', function(err, rows) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
var categories = _.map(rows, function(row) {
|
2015-03-17 07:54:01 +00:00
|
|
|
return {
|
|
|
|
id: row.id,
|
|
|
|
description: row.description
|
|
|
|
};
|
2015-03-15 08:10:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
callback(categories);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addCategory(query, callback) {
|
|
|
|
var description = query.description.trim();
|
|
|
|
|
|
|
|
if (description) {
|
|
|
|
pool.query('INSERT INTO categories(description) VALUES(?)', [description], function(err, info) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback({
|
|
|
|
id: info.insertId,
|
|
|
|
description: description,
|
|
|
|
success: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callback({success: false});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeCategory(query, callback) {
|
|
|
|
pool.query('DELETE FROM categories WHERE id = (?)', [query.id], function(err, info) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback({success: info.affectedRows > 0});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function accessReview(query, callback) {
|
2015-03-17 07:28:16 +00:00
|
|
|
query.profile = fixupProfile(query.profile);
|
|
|
|
|
2015-03-15 08:10:55 +00:00
|
|
|
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-17 09:41:52 +00:00
|
|
|
pool.query('UPDATE reviews SET accessCount = accessCount + 1 WHERE id = (?)', [query.id], function(err, info) {
|
|
|
|
if (_.keys(query.profile).length > 0) {
|
|
|
|
pool.query('INSERT INTO history(date, reviewId) VALUES(NOW(), ?)', [query.id], function(err, info) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var categoryId in query.profile) {
|
|
|
|
pool.query(
|
|
|
|
'INSERT INTO historyGroups(categoryId, categoryValue, historyId) VALUES(?, ?, ?)',
|
|
|
|
[categoryId, query.profile[categoryId], info.insertId]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-03-15 08:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
callback(results);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function runQuery(query, callback) {
|
2015-03-17 07:28:16 +00:00
|
|
|
query.profile = fixupProfile(query.profile);
|
|
|
|
query.features = fixupFeatures(query.features);
|
2015-03-15 08:10:55 +00:00
|
|
|
|
|
|
|
var context = {
|
|
|
|
geo: query.geo,
|
2015-03-17 04:23:32 +00:00
|
|
|
profile: query.profile,
|
2015-03-15 08:10:55 +00:00
|
|
|
walkingDist: query.walkingDist * 1000.0
|
|
|
|
};
|
|
|
|
|
|
|
|
getRecords(context, function(data) {
|
|
|
|
var searchResults = findRecords(
|
|
|
|
data,
|
|
|
|
query.features,
|
|
|
|
query.minScore
|
|
|
|
);
|
|
|
|
|
|
|
|
var graphColumns = {};
|
|
|
|
for (var feature in query.features) {
|
|
|
|
var searchHints = buildHints(
|
|
|
|
data,
|
|
|
|
query.features,
|
|
|
|
feature,
|
|
|
|
query.minScore,
|
|
|
|
query.range,
|
|
|
|
query.hintSteps
|
|
|
|
);
|
|
|
|
|
|
|
|
graphColumns[feature] = {
|
|
|
|
value: query.features[feature],
|
|
|
|
hints: searchHints,
|
|
|
|
steps: query.hintSteps
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
callback({
|
|
|
|
columns: graphColumns,
|
|
|
|
items: searchResults.slice(0, query.maxResults),
|
|
|
|
count: searchResults.length
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
loadDb: loadDb,
|
|
|
|
runQuery: runQuery,
|
|
|
|
getCategories: getCategories,
|
|
|
|
addCategory: addCategory,
|
|
|
|
removeCategory: removeCategory,
|
|
|
|
accessReview: accessReview
|
|
|
|
};
|