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

159 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] || 0.0) * (values2[feature] || 0.0);
2014-07-28 03:10:55 +00:00
}
return result;
}
function scale(values, factor) {
return _.map(values, function(value) {
return value * factor;
});
}
2014-07-28 13:00:01 +00:00
function countData(searchParams, minScore) {
var dataCount = 0;
for (var keyword in searchParams) {
var features = scale(db_keywords[keyword], searchParams[keyword]);
for (var i = 0, count = db_data.length; i < count; ++i) {
if (innerProduct(features, db_data[i].rating) >= minScore) {
++dataCount;
}
2014-07-28 13:00:01 +00:00
}
}
return dataCount;
}
function findData(searchParams, minScore, maxResults) {
2014-07-28 03:10:55 +00:00
var results = [];
for (var keyword in searchParams) {
var features = scale(db_keywords[keyword], searchParams[keyword]);
for (var i = 0, count = db_data.length; i < count; ++i) {
var record = db_data[i];
var score = innerProduct(features, record.rating);
if (score >= minScore) {
results.push({
name: record.name,
url: 'http://www.tripadvisor.com' + record.relativeUrl,
score: score
});
}
2014-07-28 03:10:55 +00:00
}
}
results.sort(function(a, b) {
return b.score - a.score;
});
2014-07-28 13:00:01 +00:00
return results.slice(0, maxResults);
2014-07-28 03:10:55 +00:00
}
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);
}
}
function searchProjection(searchParams, minScore, keyword, range, steps) {
2014-07-28 06:41:14 +00:00
var testParams = _.clone(searchParams);
2014-07-28 03:10:55 +00:00
var results = [];
searchStepper(range, steps, function(position) {
testParams[keyword] = position;
2014-07-28 03:10:55 +00:00
results.push({
2014-07-28 06:51:48 +00:00
sample: position,
2014-07-28 13:00:01 +00:00
count: countData(testParams, minScore)
2014-07-28 03:10:55 +00:00
});
});
return results;
}
function searchBuildHints(searchParams, minScore, keyword, 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,
keyword,
2014-07-28 03:10:55 +00:00
range,
steps
);
var hints = [];
_.each(projection, function(result) {
hints.push({
2014-07-28 06:51:48 +00:00
sample: result.sample,
2014-07-28 13:00:01 +00:00
count: result.count
2014-07-28 03:10:55 +00:00
});
});
return hints;
}
module.exports.getKeywords = function() {
return _.keys(db_keywords).sort();
2014-07-28 03:10:55 +00:00
}
2014-07-28 06:41:14 +00:00
module.exports.execQuery = function(query) {
var searchParams = query.searchParams;
var searchResults = null;
if (!searchParams) {
for (var i = 0, count = query.keywords.length; i < count; ++i) {
var keyword = query.keywords[i];
if (_.has(db_keywords, keyword)) {
searchParams[keyword] = 1.0;
}
}
searchResults = findData(
searchParams,
query.minScore,
query.maxResults
);
}
2014-07-28 06:41:14 +00:00
var graphColumns = {};
for (var keyword in searchParams) {
2014-07-28 06:51:48 +00:00
var searchHints = searchBuildHints(
2014-07-28 06:41:14 +00:00
searchParams,
query.minScore,
keyword,
2014-07-28 06:51:48 +00:00
query.searchRange,
2014-07-28 06:41:14 +00:00
query.hintSteps
);
graphColumns[param.name] = {
2014-07-28 06:51:48 +00:00
color: '#607080',
value: searchParams[keyword],
2014-07-28 06:51:48 +00:00
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
}