finite-logic
Version:
Perform fuzzy logic using linguistic values
51 lines (43 loc) • 935 B
JavaScript
// Constants the representation of finite fuzzy values
const wrong = 0.0;
const some = 0.3;
const most = 0.7;
const right = 1.0;
// Mapping for reverse conversion
const labels = {
[wrong]: "wrong", // false
[some]: "some are right", // some are right
[most]: "most are right", // Most are right
[right]: "right" // true
};
// Logic functions for the above values
function and(a, b) {
return Math.min(a, b);
}
function or(a, b) {
return Math.max(a, b);
}
function not(a) {
return 1 - a;
}
function compare(a, b) {
return a - b;
}
function toLabel(value) {
let closest = Object.keys(labels).reduce((a, b) =>
Math.abs(a - value) < Math.abs(b - value) ? a : b
);
return labels[closest];
}
// Export for usage
module.exports = {
wrong,
some,
most,
right,
and,
or,
not,
compare,
toLabel //convert back to readable label
};