1

Adding functionality for adding and removing keywords

This commit is contained in:
Alex Yatskov 2014-09-18 17:22:57 +09:00
parent 2981b4db9c
commit 5aa4ebcbc1
3 changed files with 75 additions and 5 deletions

View File

@ -95,7 +95,7 @@
ready: function() { ready: function() {
$('#keywords').selectpicker(); $('#keywords').selectpicker();
$.getJSON('/node/keywords', function(keywords) { $.getJSON('/node/getKeywords', function(keywords) {
for (var i = 0, count = keywords.length; i < count; ++i) { for (var i = 0, count = keywords.length; i < count; ++i) {
$('#keywords').append($('<option></option>', { $('#keywords').append($('<option></option>', {
value: keywords[i], value: keywords[i],

View File

@ -25,6 +25,27 @@ function scale(values, factor) {
return result; return result;
} }
function normalize(values) {
var result = {};
for (var feature in values) {
var value = values[feature];
result[value] = Math.max(-1.0, Math.min(1.0, value), value);
}
return result;
}
function combine(values1, values2) {
var result = {};
for (var feature in values1) {
result[feature] = values1[feature] + (values2[feature] || 0.0);
}
return result;
}
function countRecords(data, searchParams, minScore) { function countRecords(data, searchParams, minScore) {
var dataCount = 0; var dataCount = 0;
@ -125,6 +146,41 @@ function loadDb(params) {
connection = mysql.createConnection(params); connection = mysql.createConnection(params);
} }
function addKeyword(query, callback) {
getKeywords(function(keywords) {
var result = {
food: 0.0,
service: 0.0,
value: 0.0,
atmosphere: 0.0
};
for (var param in query.params) {
var features = scale(keywords[param], query.params[param]);
result = combine(result, features);
}
result = normalize(result);
var values = [query.keyword, result.food, result.service, result.value, result.atmosphere];
connection.query('INSERT INTO keywords VALUES(?, ?, ?, ?, ?)', values, function(err) {
callback({
keyword: query.keyword,
success: err === null
});
});
});
}
function removeKeyword(query, callback) {
connection.query('DELETE FROM keywords WHERE name=?', [query.keyword], function(err) {
callback({
keyword: query.keyword,
success: err === null
});
});
}
function getKeywords(callback) { function getKeywords(callback) {
connection.query('SELECT * FROM keywords', function(err, rows) { connection.query('SELECT * FROM keywords', function(err, rows) {
if (err) { if (err) {
@ -217,6 +273,8 @@ function execQuery(query, callback) {
module.exports = { module.exports = {
'loadDb': loadDb, 'loadDb': loadDb,
'addKeyword': addKeyword,
'removeKeyword': removeKeyword,
'getKeywords': getKeywords, 'getKeywords': getKeywords,
'execQuery': execQuery 'execQuery': execQuery
}; };

View File

@ -16,12 +16,24 @@ function main() {
'database': 'hscd' 'database': 'hscd'
}); });
app.use('/keywords', function(req, res) { app.use('/getKeywords', function(req, res) {
search.getKeywords(function(keywords) { search.getKeywords(function(keywords) {
res.json(_.keys(keywords).sort()); res.json(_.keys(keywords).sort());
}); });
}); });
app.use('/addKeyword', function(req, res) {
search.addKeyword(req.query, function(results) {
res.json(results);
});
});
app.use('/removeKeyword', function(req, res) {
search.removeKeyword(req.query, function(results) {
res.json(results);
});
});
app.use('/search', function(req, res) { app.use('/search', function(req, res) {
search.execQuery(req.query, function(results) { search.execQuery(req.query, function(results) {
res.json(results); res.json(results);