Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
feat(highlights) - Decrease score for consecutive highlights with sam…
Browse files Browse the repository at this point in the history
…e image

* Score lower for items with same preview
* Fixes #1274
  • Loading branch information
piatra committed Sep 20, 2016
1 parent c69d3d6 commit 9d610d6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
41 changes: 31 additions & 10 deletions common/recommender/Baseline.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,25 +275,46 @@ class Baseline {
}

/**
* Decrease the score for consecutive items from the same domain.
* Combined with sorting by score the result is we don't see consecutive
* items from the same domain.
* Determine if two entries are similar. Used to lower the score for similar consecutive items.
*
* @param {Object} prev
* @param {Object} curr
* @returns {boolean}
* @private
*/
_similarItems(prev, curr) {
const imgPrev = getBestImage(prev.images);
const imgCurr = getBestImage(curr.images);
const hasImage = imgPrev && imgCurr;
return prev.host === curr.host || (hasImage && imgPrev.url === imgCurr.url);
}

/**
* Decrease the score for consecutive items which are similar (see `_similarItems`).
* Combined with sorting by score the result is we don't see similar consecutive
* items.
*
* @param {Array} entries - scored and sorted highlight items.
*/
dedupe(entries) {
let penalty = 0.6;
let penalty = 0.8;

return entries.map((entry, i, arr) => {
if (i > 0 && arr[i - 1].host === entry.host) {
let score = entry.score * penalty;
if (entries.length < 2) {
return entries;
}

entries.reduce((prev, curr) => {
if (this._similarItems(prev, curr)) {
curr.score *= penalty;
penalty -= 0.2;
return Object.assign({}, entry, {score});
} else {
penalty = 0.8;
}

penalty = 0.8;
return entry;
return curr;
});

return entries;
}
}

Expand Down
38 changes: 38 additions & 0 deletions content-test/common/Baseline.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,44 @@ describe("Baseline", () => {
assert.ok(items[1].score > items[2].score);
});

it("should decrease score for consecutive items with same image", () => {
let fakeUrlsWithScore = [
{host: "foo.com", images: [{size: 1000, width: 300, height: 300, url: "http://www.sameimage.jpg"}], score: 1},
{host: "bar.com", images: [{size: 1000, width: 300, height: 300, url: "http://www.sameimage.jpg"}], score: 1},
{host: "bar.com", images: [{size: 1000, width: 300, height: 300, url: "http://www.sameimage.jpg"}], score: 1},
{host: "diff.com", images: [{size: 1000, width: 300, height: 300, url: "http://www.diffimage.jpg"}], score: 1}
];
let items = baseline.dedupe(fakeUrlsWithScore);
assert.ok(items[0].score > items[1].score);
assert.ok(items[1].score > items[2].score);
assert.equal(items[1].score, 0.8);
assert.equal(items[3].score, 1);
});

it("should decrease score for consecutive items with same image (1 entry)", () => {
let fakeUrlsWithScore = [
{host: "foo.com", images: [{size: 1000, width: 300, height: 300, url: "http://www.sameimage.jpg"}], score: 1}
];
let items = baseline.dedupe(fakeUrlsWithScore);
assert.equal(items[0].score, 1);
});

it("should decrease score for consecutive items with same image (no entries)", () => {
let items = baseline.dedupe([]);
assert.equal(items.length, 0);
});

it("should reset penalty coefficient after 2 consecutive & different items in the list", () => {
let fakeUrlsWithScore = [
{host: "foo.com", images: [{size: 1000, width: 300, height: 300, url: "http://www.sameimage.jpg"}], score: 1},
{host: "bar.com", images: [{size: 1000, width: 300, height: 300, url: "http://www.sameimage.jpg"}], score: 1},
{host: "diff.com", images: [{size: 1000, width: 300, height: 300, url: "http://www.diffimage.jpg"}], score: 1},
{host: "diff.com", images: [{size: 1000, width: 300, height: 300, url: "http://www.diffimage.jpg"}], score: 1}
];
let items = baseline.dedupe(fakeUrlsWithScore);
assert.equal(items[3].score, 0.8);
});

it("should decrease by the right amount", () => {
let fakeUrlsWithScore = fakeUrls.map(url => Object.assign({}, url, {score: 1}));
let items = baseline.dedupe(fakeUrlsWithScore);
Expand Down

0 comments on commit 9d610d6

Please sign in to comment.