Skip to content

Instantly share code, notes, and snippets.

@AYLIEN
Last active February 2, 2016 13:05
Show Gist options
  • Save AYLIEN/465b66785970335357a2 to your computer and use it in GitHub Desktop.
Save AYLIEN/465b66785970335357a2 to your computer and use it in GitHub Desktop.
Sort Concepts by Relevance
var _ = require('underscore'),
AYLIENTextAPI = require("aylien_textapi");
var textapi = new AYLIENTextAPI({
application_id: "YourApplicationId",
application_key: "YourApplicationKey"
});
function escapeRegExp(r) {
return r.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}
var titleWeight = 3,
articleWeight = 1;
textapi.combined({
url: 'http://techcrunch.com/2016/02/01/nasas-super-guppy-gives-mars-bound-spacecraft-a-lift/',
endpoint: ['extract', 'concepts']
}, function(err, result) {
if (err === null) {
var title;
var article;
var concepts;
_(result.results).each(function(r) {
if (r.endpoint == 'extract') {
title = r.result.title;
article = r.result.article;
} else if (r.endpoint = 'concepts') {
concepts = r.result.concepts;
}
});
var cw = _.chain(concepts).map(function(o, c) {
var w = _.chain(o.surfaceForms).map(function(sf) {
var e = new RegExp('\\b' + escapeRegExp(sf.string) + '\\b', 'g');
var inArticle = (article.match(e) || []).length;
var inTitle = (title.match(e) || []).length;
return (inTitle * titleWeight) + (inArticle * articleWeight);
}).reduce(function(memo, num) { return memo + num; }, 0).value();
return {'concept': c, 'weight': w};
}).sortBy(function(i) { return -i.weight; }).value();
console.log(cw);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment