1
restaurant-search/server/db/search.js

161 lines
3.8 KiB
JavaScript
Raw Normal View History

2014-07-28 03:10:55 +00:00
'use strict';
2014-07-28 06:41:14 +00:00
var _ = require('underscore');
var db_keywords = require('./keywords.json');
var db_data = require('./data.json');
2014-07-28 03:10:55 +00:00
2014-07-28 07:48:24 +00:00
2014-07-28 03:10:55 +00:00
function innerProduct(values1, values2) {
var result = 0;
2014-07-28 06:41:14 +00:00
2014-07-28 03:10:55 +00:00
for (var feature in values1) {
result += values1[feature] * (values2[feature] || 0.0);
}
return result;
}
2014-07-28 06:41:14 +00:00
function searchData(searchParams, minScore) {
2014-07-28 03:10:55 +00:00
var results = [];
2014-07-28 06:41:14 +00:00
for (var i = 0, count = db_data.length; i < count; ++i) {
var record = db_data[i];
var score = innerProduct(searchParams, record['rating']);
2014-07-28 03:10:55 +00:00
if (score >= minScore) {
results.push({
name: record['name'],
url: 'http://www.tripadvisor.com' + record['relativeUrl'],
score: score
});
}
}
results.sort(function(a, b) {
return b.score - a.score;
});
return results;
}
function searchStepper(range, steps, callback) {
2014-07-28 06:41:14 +00:00
var stepSize = (range.max - range.min) / steps;
2014-07-28 03:10:55 +00:00
for (var i = 0; i < steps; ++i) {
2014-07-28 06:41:14 +00:00
var stepMax = range.max - stepSize * i;
2014-07-28 03:10:55 +00:00
var stepMin = stepMax - stepSize;
var stepMid = (stepMin + stepMax) / 2;
callback(stepMid);
}
}
2014-07-28 06:41:14 +00:00
function searchProjection(searchParams, minScore, feature, range, steps) {
var testParams = _.clone(searchParams);
2014-07-28 03:10:55 +00:00
var results = [];
searchStepper(range, steps, function(position) {
testParams[feature] = position;
results.push({
2014-07-28 06:51:48 +00:00
sample: position,
values: searchData(testParams, minScore)
2014-07-28 03:10:55 +00:00
});
});
return results;
}
2014-07-28 06:41:14 +00:00
function searchProjection2d(searchParams, minScore, feature1, feature2, range, steps) {
var testParams = _.clone(searchParams);
2014-07-28 03:10:55 +00:00
var results = [];
searchStepper(range, steps, function(sampleX) {
testParams[feature1] = sampleX;
searchStepper(range, steps, function(sampleY) {
testParams[feature2] = sampleY;
results.push({
2014-07-28 06:51:48 +00:00
sampleX: sampleX,
sampleY: sampleY,
values: searchData(testParams, minScore)
2014-07-28 03:10:55 +00:00
});
});
});
return results;
}
2014-07-28 06:41:14 +00:00
function searchBuildHints(searchParams, minScore, feature, range, steps) {
2014-07-28 03:10:55 +00:00
var projection = searchProjection(
2014-07-28 06:41:14 +00:00
searchParams,
2014-07-28 03:10:55 +00:00
minScore,
feature,
range,
steps
);
var hints = [];
_.each(projection, function(result) {
hints.push({
2014-07-28 06:51:48 +00:00
sample: result.sample,
count: result.values.length
2014-07-28 03:10:55 +00:00
});
});
return hints;
}
2014-07-28 06:41:14 +00:00
function searchBuildHints2d(searchParams, minScore, feature1, feature2, range, steps) {
2014-07-28 03:10:55 +00:00
var projection = searchProjection2d(
2014-07-28 06:41:14 +00:00
searchParams,
2014-07-28 03:10:55 +00:00
minScore,
feature1,
feature2,
range,
steps
);
var hints = [];
_.each(projection, function(result) {
hints.push({
2014-07-28 06:51:48 +00:00
sampleX: result.sampleX,
sampleY: result.sampleY,
count: result.values.length
2014-07-28 03:10:55 +00:00
});
});
return hints;
}
module.exports.getKeywords = function() {
2014-07-28 06:41:14 +00:00
return _.keys(db_keywords);
2014-07-28 03:10:55 +00:00
}
2014-07-28 06:41:14 +00:00
module.exports.execQuery = function(query) {
2014-07-28 07:21:08 +00:00
var searchParams = query.searchParams || db_keywords[query.keyword];
2014-07-28 06:51:48 +00:00
var searchResults = searchData(searchParams, query.minScore);
var graphColumns = { };
2014-07-28 06:41:14 +00:00
for (var feature in searchParams) {
2014-07-28 06:51:48 +00:00
var searchHints = searchBuildHints(
2014-07-28 06:41:14 +00:00
searchParams,
query.minScore,
feature,
2014-07-28 06:51:48 +00:00
query.searchRange,
2014-07-28 06:41:14 +00:00
query.hintSteps
);
graphColumns[feature] = {
2014-07-28 06:51:48 +00:00
color: '#607080',
value: searchParams[feature],
hints: searchHints,
steps: query.hintSteps
2014-07-28 06:41:14 +00:00
}
}
return {
2014-07-28 06:51:48 +00:00
columns: graphColumns,
params: searchParams,
items: searchResults
2014-07-28 06:41:14 +00:00
};
2014-07-28 03:10:55 +00:00
}