Update extramath.js

This commit is contained in:
Namhyeon Go 2022-11-26 01:08:57 +09:00
parent a35de48d36
commit 37b8404f0b

View File

@ -28,7 +28,7 @@ function DTM() {
}
// Cosine similarity: https://en.wikipedia.org/wiki/Cosine_similarity
function cos(A, B) {
function arrayCos(A, B) {
var dotproduct = 0;
var mA = 0;
var mB = 0;
@ -43,6 +43,18 @@ function cos(A, B) {
return similarity;
}
function measureSimilarity(s1, s2) {
var dtm = new DTM();
dtm.add(s1);
dtm.add(s2);
var mat = dtm.toArray();
return arrayCos(mat[0], mat[1]);
}
function export_measureSimilarity() {
return "var ExtraMath=function(){};ExtraMath.DTM=function(){this.data=[],this.terms=[],this.add=function(t){for(var r=t.trim().split(/\s+/),a=0;a<r.length;a++)0>this.terms.indexOf(r[a])&&this.terms.push(r[a]);this.data.push(r)},this.toArray=function(){for(var t=[],r=0;r<this.data.length;r++){for(var a=[],s=0;s<this.terms.length;s++)a.push(0>this.data[r].indexOf(this.terms[s])?0:1);t.push(a)}return t}},ExtraMath.arrayCos=function(t,r){var a=0,s=0,h=0;for(i=0;i<t.length;i++)a+=t[i]*r[i],s+=t[i]*t[i],h+=r[i]*r[i];return a/((s=Math.sqrt(s))*(h=Math.sqrt(h)))},ExtraMath.measureSimilarity=function(t,r){var a=new ExtraMath.DTM;a.add(t),a.add(r);var s=a.toArray();return ExtraMath.arrayCos(s[0],s[1])};";
}
// Cartesian product: https://en.wikipedia.org/wiki/Cartesian_product
function cartesianProduct(arr) {
return arr.reduce(function(a,b){
@ -55,17 +67,12 @@ function cartesianProduct(arr) {
}
exports.DTM = DTM;
exports.cos = cos;
exports.measureSimilarity = function(s1, s2) {
var dtm = new DTM();
dtm.add(s1);
dtm.add(s2);
var mat = dtm.toArray();
return cos(mat[0], mat[1]);
};
exports.arrayCos = arrayCos;
exports.measureSimilarity = measureSimilarity;
exports.export_measureSimilarity = export_measureSimilarity;
exports.cartesianProduct = cartesianProduct;
exports.VERSIONINFO = "ExtraMath module (extramath.js) version 0.0.4";
exports.VERSIONINFO = "ExtraMath module (extramath.js) version 0.0.5";
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = global.require;