Skip to content

Commit

Permalink
Adding set.overlap
Browse files Browse the repository at this point in the history
  • Loading branch information
Yomguithereal committed Mar 18, 2020
1 parent 0f9a44d commit 464638b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions set.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export function disjunct<T>(a: Set<T>, b: Set<T>): void;
export function intersectionSize<T>(a: Set<T>, b:Set<T>): number;
export function unionSize<T>(a: Set<T>, b:Set<T>): number;
export function jaccard<T>(a: Set<T>, b:Set<T>): number;
export function overlap<T>(a: Set<T>, b: Set<T>): number;
19 changes: 19 additions & 0 deletions set.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ exports.disjunct = function(A, B) {
*
* @param {Set} A - First set.
* @param {Set} B - Second set.
* @return {number}
*/
exports.intersectionSize = function(A, B) {
var tmp;
Expand Down Expand Up @@ -312,6 +313,7 @@ exports.intersectionSize = function(A, B) {
*
* @param {Set} A - First set.
* @param {Set} B - Second set.
* @return {number}
*/
exports.unionSize = function(A, B) {
var I = exports.intersectionSize(A, B);
Expand All @@ -324,6 +326,7 @@ exports.unionSize = function(A, B) {
*
* @param {Set} A - First set.
* @param {Set} B - Second set.
* @return {number}
*/
exports.jaccard = function(A, B) {
var I = exports.intersectionSize(A, B);
Expand All @@ -335,3 +338,19 @@ exports.jaccard = function(A, B) {

return I / U;
};

/**
* Function returning the overlap coefficient between A & B.
*
* @param {Set} A - First set.
* @param {Set} B - Second set.
* @return {number}
*/
exports.overlap = function(A, B) {
var I = exports.intersectionSize(A, B);

if (I === 0)
return 0;

return I / Math.min(A.size, B.size);
};
14 changes: 14 additions & 0 deletions test/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,18 @@ describe('Set functions', function() {
assert.strictEqual(functions.jaccard(new Set('contact'), new Set('context')), 4 / 7);
});
});

describe('#.overlap', function() {
it('should properly return the overlap coefficient between two sets.', function() {
var A = new Set([1, 2, 3]),
B = new Set([2, 3, 4]);

var N = new Set([]);

assert.strictEqual(functions.overlap(A, B), 2 / 3);
assert.strictEqual(functions.overlap(A, N), 0);

assert.strictEqual(functions.overlap(new Set('contact'), new Set('context')), 4 / 5);
});
});
});

0 comments on commit 464638b

Please sign in to comment.