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) {
|
2014-09-12 06:41:27 +00:00
|
|
|
result += (values1[feature] || 0.0) * (values2[feature] || 0.0);
|
2014-07-28 03:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-09-12 06:41:27 +00:00
|
|
|
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;
|
|
|
|
|
2014-09-12 06:41:27 +00:00
|
|
|
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 = [];
|
|
|
|
|
2014-09-12 06:41:27 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-12 06:41:27 +00:00
|
|
|
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) {
|
2014-09-12 06:41:27 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-09-12 06:41:27 +00:00
|
|
|
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,
|
2014-09-12 06:41:27 +00:00
|
|
|
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() {
|
2014-09-12 06:41:27 +00:00
|
|
|
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 06:41:27 +00:00
|
|
|
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
|
|
|
|
2014-09-12 06:41:27 +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,
|
2014-09-12 06:41:27 +00:00
|
|
|
keyword,
|
2014-07-28 06:51:48 +00:00
|
|
|
query.searchRange,
|
2014-07-28 06:41:14 +00:00
|
|
|
query.hintSteps
|
|
|
|
);
|
|
|
|
|
2014-09-12 06:41:27 +00:00
|
|
|
graphColumns[param.name] = {
|
2014-07-28 06:51:48 +00:00
|
|
|
color: '#607080',
|
2014-09-12 06:41:27 +00:00
|
|
|
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
|
|
|
}
|