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

169 lines
4.0 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) {
2014-09-12 14:12:11 +00:00
var result = 0.0;
2014-07-28 06:41:14 +00:00
2014-07-28 03:10:55 +00:00
for (var feature in values1) {
2014-09-12 14:12:11 +00:00
result += values1[feature] * (values2[feature] || 0.0);
2014-07-28 03:10:55 +00:00
}
return result;
}
function scale(values, factor) {
2014-09-12 14:12:11 +00:00
var result = {};
for (var feature in values) {
result[feature] = values[feature] * factor;
}
return result;
}
2014-07-28 13:00:01 +00:00
function countData(searchParams, minScore) {
var dataCount = 0;
2014-09-14 10:40:46 +00:00
for (var i = 0, count = db_data.length; i < count; ++i) {
var record = db_data[i];
var score = 0.0;
for (var keyword in searchParams) {
var features = scale(db_keywords[keyword], searchParams[keyword]);
score += innerProduct(features, record.rating);
}
if (score >= minScore) {
++dataCount;
2014-07-28 13:00:01 +00:00
}
}
return dataCount;
}
function findData(searchParams, minScore) {
2014-07-28 03:10:55 +00:00
var results = [];
2014-09-14 10:40:46 +00:00
for (var i = 0, count = db_data.length; i < count; ++i) {
var record = db_data[i];
var score = 0.0;
for (var keyword in searchParams) {
var features = scale(db_keywords[keyword], searchParams[keyword]);
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;
});
return results;
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) {
2014-09-12 08:16:11 +00:00
if (!query.searchParams) {
query.searchParams = {};
for (var i = 0, count = query.keywords.length; i < count; ++i) {
var keyword = query.keywords[i];
if (_.has(db_keywords, keyword)) {
2014-09-12 08:16:11 +00:00
query.searchParams[keyword] = 1.0;
}
}
}
2014-07-28 06:41:14 +00:00
2014-09-12 08:16:11 +00:00
var searchResults = findData(
query.searchParams,
query.minScore * _.keys(query.searchParams).length
2014-09-12 08:16:11 +00:00
);
var graphColumns = {};
2014-09-12 08:16:11 +00:00
for (var keyword in query.searchParams) {
2014-07-28 06:51:48 +00:00
var searchHints = searchBuildHints(
2014-09-12 08:16:11 +00:00
query.searchParams,
query.minScore * _.keys(query.searchParams).length,
keyword,
2014-07-28 06:51:48 +00:00
query.searchRange,
2014-07-28 06:41:14 +00:00
query.hintSteps
);
graphColumns[keyword] = {
2014-07-28 06:51:48 +00:00
color: '#607080',
2014-09-12 08:16:11 +00:00
value: query.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,
2014-09-12 08:16:11 +00:00
params: query.searchParams,
items: searchResults.slice(0, query.maxResults),
count: searchResults.length
2014-07-28 06:41:14 +00:00
};
2014-07-28 03:10:55 +00:00
}