-
Semantic tweaks to:
+
Semantic tweaker
diff --git a/client/scripts/application.js b/client/scripts/application.js
index c80ead6..8cac819 100644
--- a/client/scripts/application.js
+++ b/client/scripts/application.js
@@ -27,26 +27,17 @@
(function(hscd) {
'use strict';
- var ctx = {
- log: []
- };
+ var _custom = '[custom]';
+ var _ctx = { log: [] };
function onAdjust(name, value) {
- ctx.features[name] = value;
+ _ctx.query.features[name] = value;
+ _ctx.grapher.enable(false);
- var query = {
- features: ctx.features,
- range: ctx.range,
- minScore: ctx.minScore,
- hintSteps: ctx.hintSteps,
- maxResults: ctx.maxResults
- };
-
- ctx.grapher.enable(false);
- $.getJSON('/search', query, function(results) {
+ $.getJSON('/search', _ctx.query, function(results) {
saveSnapshot(results);
outputSnapshot(results);
- ctx.grapher.enable(true);
+ _ctx.grapher.enable(true);
});
}
@@ -54,7 +45,7 @@
$('#history').on('slideStop', onSelectSnapshot);
$('#history').slider({
formatter: function(value) {
- var delta = ctx.log.length - (value + 1);
+ var delta = _ctx.log.length - (value + 1);
switch (delta) {
case 0:
return 'Most recent query';
@@ -76,57 +67,66 @@
$('#learnKeyword').prop('disabled', !$(this).val());
});
- $.getJSON('/get_keywords', function(keywords) {
- ctx.keywords = keywords;
- for (var keyword in keywords) {
+ $.getJSON('/get_parameters', function(parameters) {
+ _ctx.parameters = parameters;
+ for (var keyword in parameters.keywords) {
$('#keywordsToSearch').append($('', { value: keyword, text: keyword }));
}
- onSearch();
+ search();
});
}
- function onSearch() {
- var keyword = $('#keywordsToSearch').val();
- var query = {
- features: ctx.keywords[keyword],
+ function search(keyword) {
+ var features = {};
+ if (typeof(keyword) == 'undefined') {
+ for (var i = 0, count = _ctx.parameters.features.length; i < count; ++i) {
+ features[_ctx.parameters.features[i]] = 0.0;
+ }
+ }
+ else {
+ features = _ctx.parameters.keywords[keyword];
+ }
+
+ _ctx.query = {
+ features: features,
range: { min: -1.0, max: 1.0 },
minScore: parseFloat($('#minScore').val()),
hintSteps: parseInt($('#hintSteps').val()),
maxResults: parseInt($('#maxResults').val())
};
- $.getJSON('/search', query, function(results) {
- ctx.features = query.features;
- ctx.range = query.range;
- ctx.minScore = query.minScore;
- ctx.hintSteps = query.hintSteps;
- ctx.maxResults = query.maxResults;
-
- ctx.grapher = new grapher.Grapher({
+ if (!_.has(_ctx, 'grapher')) {
+ _ctx.grapher = new grapher.Grapher({
canvas: new Snap('#svg'),
- steps: ctx.hintSteps,
- range: ctx.range,
+ steps: _ctx.query.hintSteps,
+ range: _ctx.query.range,
onValueChanged: onAdjust,
useLocalScale: true,
useRelativeScale: true
});
- ctx.grapher.setColumns(results.columns);
-
- saveSnapshot(results);
- outputMatches(results.items, results.count);
$('#useLocalScale').click(function() {
var useLocalScale = $('#useLocalScale').is(':checked');
- ctx.grapher.setUseLocalScale(useLocalScale);
+ _ctx.grapher.setUseLocalScale(useLocalScale);
});
$('#useRelativeScale').click(function() {
var useRelativeScale = $('#useRelativeScale').is(':checked');
- ctx.grapher.setUseRelativeScale(useRelativeScale);
+ _ctx.grapher.setUseRelativeScale(useRelativeScale);
});
- $('#query').text(keyword);
- $('#output').fadeIn();
+ var columns = {};
+ for (var feature in _ctx.query.features) {
+ columns[feature] = { value: 0.0, hints: [] };
+ }
+
+ _ctx.grapher.setColumns(columns);
+ }
+
+ $.getJSON('/search', _ctx.query, function(results) {
+ _ctx.grapher.setColumns(results.columns);
+ saveSnapshot(results);
+ outputMatches(results.items, results.count);
});
}
@@ -135,7 +135,7 @@
$('#learnError').slideUp(function() {
var query = {
keyword: $('#keywordToLearn').val(),
- params: ctx.features
+ params: _ctx.query.features
};
$.getJSON('/add_keyword', query, function(results) {
@@ -153,13 +153,13 @@
function onSelectSnapshot() {
var index = $('#history').slider('getValue');
- outputSnapshot(ctx.log[index]);
+ outputSnapshot(_ctx.log[index]);
}
function saveSnapshot(results) {
- ctx.log.push(results);
+ _ctx.log.push(results);
- var count = ctx.log.length;
+ var count = _ctx.log.length;
var history = $('#history').slider();
history.slider('setAttribute', 'max', count - 1);
history.slider('setValue', count - 1);
@@ -171,10 +171,10 @@
function outputSnapshot(results) {
for (var name in results.columns) {
- ctx.features[name] = results.columns[name].value;
+ _ctx.query.features[name] = results.columns[name].value;
}
- ctx.grapher.setColumns(results.columns);
+ _ctx.grapher.setColumns(results.columns);
outputMatches(results.items, results.count);
}
diff --git a/client/scripts/grapher.js b/client/scripts/grapher.js
index 6ee9b4e..401eee1 100644
--- a/client/scripts/grapher.js
+++ b/client/scripts/grapher.js
@@ -152,10 +152,8 @@
}
function updateShapes() {
- var gradient = _canvas.gradient(decimateHints());
- _elements.density.attr({
- fill: gradient
- });
+ var fill = _data.hints.length === 0 ? _backdropColor : _canvas.gradient(decimateHints());
+ _elements.density.attr({ fill: fill });
var range = computeIndicatorRange();
_elements.indicator.attr({
diff --git a/server/search.js b/server/search.js
index 998c84a..2c6a310 100644
--- a/server/search.js
+++ b/server/search.js
@@ -42,47 +42,6 @@ function innerProduct(values1, values2) {
return result;
}
-function scale(values, factor) {
- var result = {};
-
- for (var feature in values) {
- result[feature] = values[feature] * factor;
- }
-
- return result;
-}
-
-function add(values1, values2) {
- var result = {};
-
- for (var feature in values1) {
- result[feature] = values1[feature] + (values2[feature] || 0.0);
- }
-
- return result;
-}
-
-function combine(dict, params) {
- var result = {};
-
- for (var paramIter in params) {
- var values = scale(dict[paramIter], params[paramIter]);
- result = add(values, result);
- }
-
- var max = _.max(_.values(result), function(value) {
- return Math.abs(value);
- });
-
- if (max > 0.0) {
- for (var resultIter in result) {
- result[resultIter] = result[resultIter] / max;
- }
- }
-
- return result;
-}
-
function walkMatches(data, features, minScore, callback) {
for (var i = 0, count = data.records.length; i < count; ++i) {
var record = data.records[i];
@@ -174,32 +133,32 @@ function loadDb(params) {
}
function addKeyword(query, callback) {
- var keyword = (query.keyword || '').toLowerCase();
- if (!/^[a-zA-Z0-9]+$/.test(keyword)) {
- callback({
- keyword: keyword,
- success: false
- });
- }
- else {
- getKeywords(function(keywords) {
- var features = combine(keywords, query.params);
- var values = [
- keyword,
- features.food || 0.0,
- features.service || 0.0,
- features.value || 0.0,
- features.atmosphere || 0.0
- ];
+ // var keyword = (query.keyword || '').toLowerCase();
+ // if (!/^[a-zA-Z0-9]+$/.test(keyword)) {
+ // callback({
+ // keyword: keyword,
+ // success: false
+ // });
+ // }
+ // else {
+ // getKeywords(function(keywords) {
+ // var features = combine(keywords, query.params);
+ // var values = [
+ // keyword,
+ // features.food || 0.0,
+ // features.service || 0.0,
+ // features.value || 0.0,
+ // features.atmosphere || 0.0
+ // ];
- pool.query('INSERT INTO keywords VALUES(?, ?, ?, ?, ?)', values, function(err) {
- callback({
- keyword: keyword,
- success: err === null
- });
- });
- });
- }
+ // pool.query('INSERT INTO keywords VALUES(?, ?, ?, ?, ?)', values, function(err) {
+ // callback({
+ // keyword: keyword,
+ // success: err === null
+ // });
+ // });
+ // });
+ // }
}
function removeKeyword(query, callback) {
@@ -267,6 +226,15 @@ function getData(callback) {
});
}
+function getParameters(callback) {
+ getKeywords(function(keywords) {
+ callback({
+ keywords: keywords,
+ features: [ 'food', 'service', 'atmosphere', 'value' ]
+ });
+ });
+}
+
function execQuery(query, callback) {
getData(function(data) {
var searchResults = findRecords(
@@ -306,6 +274,6 @@ module.exports = {
'loadDb': loadDb,
'addKeyword': addKeyword,
'removeKeyword': removeKeyword,
- 'getKeywords': getKeywords,
+ 'getParameters': getParameters,
'execQuery': execQuery
};
diff --git a/server/server.js b/server/server.js
index d66cd28..195a00a 100755
--- a/server/server.js
+++ b/server/server.js
@@ -43,9 +43,9 @@ function main(staticFiles, port) {
user: 'hscd'
});
- app.use('/get_keywords', function(req, res) {
- search.getKeywords(function(keywords) {
- res.json(keywords);
+ app.use('/get_parameters', function(req, res) {
+ search.getParameters(function(parameters) {
+ res.json(parameters);
});
});
@@ -55,12 +55,6 @@ function main(staticFiles, port) {
});
});
- app.use('/remove_keyword', function(req, res) {
- search.removeKeyword(req.query, function(results) {
- res.json(results);
- });
- });
-
app.use('/search', function(req, res) {
search.execQuery(req.query, function(results) {
res.json(results);