1

Preliminary work to get multiple keyword search working

This commit is contained in:
Alex Yatskov 2014-09-12 15:41:27 +09:00
parent 6491707b24
commit 57ee7c227d
2 changed files with 54 additions and 30 deletions

View File

@ -19,9 +19,9 @@
<div id="input"> <div id="input">
<form class="form-horizontal"> <form class="form-horizontal">
<div class="form-group"> <div class="form-group">
<label for="keyword" class="col-md-2 control-label">Keyword</label> <label for="keywords" class="col-md-2 control-label">Keywords</label>
<div class="col-md-10"> <div class="col-md-10">
<select id="keyword" class="form-control" name="keyword"></select> <select id="keywords" class="form-control" name="keywords" multiple="multiple"></select>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">

View File

@ -9,21 +9,27 @@ function innerProduct(values1, values2) {
var result = 0; var result = 0;
for (var feature in values1) { for (var feature in values1) {
result += values1[feature] * (values2[feature] || 0.0); result += (values1[feature] || 0.0) * (values2[feature] || 0.0);
} }
return result; return result;
} }
function scale(values, factor) {
return _.map(values, function(value) {
return value * factor;
});
}
function countData(searchParams, minScore) { function countData(searchParams, minScore) {
var dataCount = 0; var dataCount = 0;
for (var i = 0, count = db_data.length; i < count; ++i) { for (var keyword in searchParams) {
var record = db_data[i]; var features = scale(db_keywords[keyword], searchParams[keyword]);
var score = innerProduct(searchParams, record.rating); for (var i = 0, count = db_data.length; i < count; ++i) {
if (innerProduct(features, db_data[i].rating) >= minScore) {
if (score >= minScore) { ++dataCount;
++dataCount; }
} }
} }
@ -33,16 +39,19 @@ function countData(searchParams, minScore) {
function findData(searchParams, minScore, maxResults) { function findData(searchParams, minScore, maxResults) {
var results = []; var results = [];
for (var i = 0, count = db_data.length; i < count; ++i) { for (var keyword in searchParams) {
var record = db_data[i]; var features = scale(db_keywords[keyword], searchParams[keyword]);
var score = innerProduct(searchParams, record.rating); 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) { if (score >= minScore) {
results.push({ results.push({
name: record.name, name: record.name,
url: 'http://www.tripadvisor.com' + record.relativeUrl, url: 'http://www.tripadvisor.com' + record.relativeUrl,
score: score score: score
}); });
}
} }
} }
@ -65,12 +74,12 @@ function searchStepper(range, steps, callback) {
} }
} }
function searchProjection(searchParams, minScore, feature, range, steps) { function searchProjection(searchParams, minScore, keyword, range, steps) {
var testParams = _.clone(searchParams); var testParams = _.clone(searchParams);
var results = []; var results = [];
searchStepper(range, steps, function(position) { searchStepper(range, steps, function(position) {
testParams[feature] = position; testParams[keyword] = position;
results.push({ results.push({
sample: position, sample: position,
count: countData(testParams, minScore) count: countData(testParams, minScore)
@ -80,11 +89,11 @@ function searchProjection(searchParams, minScore, feature, range, steps) {
return results; return results;
} }
function searchBuildHints(searchParams, minScore, feature, range, steps) { function searchBuildHints(searchParams, minScore, keyword, range, steps) {
var projection = searchProjection( var projection = searchProjection(
searchParams, searchParams,
minScore, minScore,
feature, keyword,
range, range,
steps steps
); );
@ -101,26 +110,41 @@ function searchBuildHints(searchParams, minScore, feature, range, steps) {
} }
module.exports.getKeywords = function() { module.exports.getKeywords = function() {
return _.keys(db_keywords); return _.keys(db_keywords).sort();
} }
module.exports.execQuery = function(query) { module.exports.execQuery = function(query) {
var searchParams = query.searchParams || db_keywords[query.keyword]; var searchParams = query.searchParams;
var searchResults = findData(searchParams, query.minScore, query.maxResults); var searchResults = null;
var graphColumns = {};
for (var feature in searchParams) { 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
);
}
var graphColumns = {};
for (var keyword in searchParams) {
var searchHints = searchBuildHints( var searchHints = searchBuildHints(
searchParams, searchParams,
query.minScore, query.minScore,
feature, keyword,
query.searchRange, query.searchRange,
query.hintSteps query.hintSteps
); );
graphColumns[feature] = { graphColumns[param.name] = {
color: '#607080', color: '#607080',
value: searchParams[feature], value: searchParams[keyword],
hints: searchHints, hints: searchHints,
steps: query.hintSteps steps: query.hintSteps
} }