scraipt
Version:
Scrape away inefficient code during compile-time using AI
72 lines (71 loc) • 2.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getJaacardSimilarity = exports.getJaacardDistance = exports.getJaacardIndex = void 0;
/**
* Calculate the intersection of two arrays
* @param {number[]} a - The first array
* @param {number[]} b - The second array
* @returns The intersection of the two arrays
*/
const intersection = (a, b) => {
const setA = new Set(a);
const setB = new Set(b);
const intersection = new Set([...setA].filter((x) => setB.has(x)));
return Array.from(intersection);
};
/**
* Calculate the union of two arrays
* @param {number[]} a - The first array
* @param {number[]} b - The second array
* @returns The union of the two arrays
* */
const union = (a, b) => {
const setA = new Set(a);
const setB = new Set(b);
const union = new Set([...setA, ...setB]);
return Array.from(union);
};
/**
* Calculate the difference of two arrays
* @param {number[]} a - The first array
* @param {number[]} b - The second array
* @returns The difference of the two arrays
* */
const difference = (a, b) => {
const setA = new Set(a);
const setB = new Set(b);
const difference = new Set([...setA].filter((x) => !setB.has(x)));
return Array.from(difference);
};
/**
* Calculate the Jaacard index of two arrays
* @param {number[]} a - The first array
* @param {number[]} b - The second array
* @returns The Jaacard index of the two arrays
* */
const getJaacardIndex = (a, b) => {
const intersectionLength = intersection(a, b).length;
const unionLength = union(a, b).length;
return intersectionLength / unionLength;
};
exports.getJaacardIndex = getJaacardIndex;
/**
* Calculate the Jaacard distance of two arrays
* @param {number[]} a - The first array
* @param {number[]} b - The second array
* @returns The Jaacard distance of the two arrays
* */
const getJaacardDistance = (a, b) => {
return 1 - (0, exports.getJaacardIndex)(a, b);
};
exports.getJaacardDistance = getJaacardDistance;
/**
* Calculate the Jaacard similarity of two arrays
* @param {number[]} a - The first array
* @param {number[]} b - The second array
* @returns The Jaacard similarity of the two arrays
* */
const getJaacardSimilarity = (a, b) => {
return 1 - (0, exports.getJaacardDistance)(a, b);
};
exports.getJaacardSimilarity = getJaacardSimilarity;