string-comparison
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -1,1 +0,1 @@ | ||
"use strict";module.exports={cosine:require("./main/packages/Cosine"),diceCoefficient:require("./main/packages/Cosine"),jaccardIndex:require("./main/packages/Cosine"),levenshtein:require("./main/packages/Cosine"),lcs:require("./main/packages/Cosine"),mlcs:require("./main/packages/Cosine")}; | ||
"use strict";function _slicedToArray(a,b){return _arrayWithHoles(a)||_iterableToArrayLimit(a,b)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}function _iterableToArrayLimit(a,b){if(Symbol.iterator in Object(a)||"[object Arguments]"===Object.prototype.toString.call(a)){var c=[],d=!0,e=!1,f=void 0;try{for(var g,h=a[Symbol.iterator]();!(d=(g=h.next()).done)&&(c.push(g.value),!(b&&c.length===b));d=!0);}catch(a){e=!0,f=a}finally{try{d||null==h["return"]||h["return"]()}finally{if(e)throw f}}return c}}function _arrayWithHoles(a){if(Array.isArray(a))return a}var Sim=["Cosine","DiceCoefficient","JaccardIndex","Levenshtein","LongestCommonSubsequence","MetricLCS"],_Sim$map=Sim.map(function(a){return require("./main/packages/".concat(a))}),_Sim$map2=_slicedToArray(_Sim$map,6),Cosine=_Sim$map2[0],DiceCoefficient=_Sim$map2[1],JaccardIndex=_Sim$map2[2],Levenshtein=_Sim$map2[3],LongestCommonSubsequence=_Sim$map2[4],MetricLCS=_Sim$map2[5];module.exports={cosine:new Cosine,diceCoefficient:new DiceCoefficient,jaccardIndex:new JaccardIndex,levenshtein:new Levenshtein,lcs:new LongestCommonSubsequence,mlcs:new MetricLCS}; |
@@ -1,2 +0,2 @@ | ||
"use strict";function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")}function _defineProperties(target,props){for(var descriptor,i=0;i<props.length;i++)descriptor=props[i],descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,"value"in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,descriptor.key,descriptor)}function _createClass(Constructor,protoProps,staticProps){return protoProps&&_defineProperties(Constructor.prototype,protoProps),staticProps&&_defineProperties(Constructor,staticProps),Constructor}module.exports=/*#__PURE__*/function(){function Similarity(){_classCallCheck(this,Similarity)}/** | ||
"use strict";function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}module.exports=/*#__PURE__*/function(){function a(){_classCallCheck(this,a)}/** | ||
* | ||
@@ -6,3 +6,3 @@ * @param {String} thanos 灭霸,主字符串 | ||
* @description 比较两个字符串 | ||
*/return _createClass(Similarity,[{key:"similarity",value:function similarity(){console.info(111)}/** | ||
*/return _createClass(a,[{key:"similarity",value:function similarity(){console.info(111)}/** | ||
* | ||
@@ -12,4 +12,4 @@ * @param {String} thanos | ||
* @description 寻找最佳匹配结果 | ||
*/},{key:"sortMatch",value:function sortMatch(thanos,avengers){var _this=this;return Similarity.checkThanosType(thanos),Similarity.checkAvengersType(avengers),avengers.map(function(str,index){return{member:str,index:index,rating:_this.similarity(thanos,str)}}).sort(function(a,b){return a.rating-b.rating})}// distance | ||
*/},{key:"sortMatch",value:function sortMatch(b,c){var d=this;return a.checkThanosType(b),a.checkAvengersType(c),c.map(function(a,c){return{member:a,index:c,rating:d.similarity(b,a)}}).sort(function(c,a){return c.rating-a.rating})}// distance | ||
},{key:"distance",value:function distance(){// 计算 distance | ||
}}],[{key:"checkThanosType",value:function checkThanosType(thanos){if("string"!=typeof thanos)throw new Error("first argument should be a string")}},{key:"checkRivalType",value:function checkRivalType(rival){if("string"!=typeof rival)throw new Error("second argument should be a string")}},{key:"checkAvengersType",value:function checkAvengersType(avengers){if(!Array.isArray(avengers))throw new Error("second argument should be an array of strings");if(avengers.find(function(s){return"string"!=typeof s}))throw new Error("second argument should be an array of strings")}},{key:"initParams",value:function initParams(thanos,rival){return[thanos.replace(/\s+/g,"").toLowerCase(),rival.replace(/\s+/g,"").toLowerCase()]}}]),Similarity}(); | ||
}}],[{key:"checkThanosType",value:function checkThanosType(a){if("string"!=typeof a)throw new Error("first argument should be a string")}},{key:"checkRivalType",value:function checkRivalType(a){if("string"!=typeof a)throw new Error("second argument should be a string")}},{key:"checkAvengersType",value:function checkAvengersType(a){if(!Array.isArray(a))throw new Error("second argument should be an array of strings");if(a.find(function(a){return"string"!=typeof a}))throw new Error("second argument should be an array of strings")}},{key:"initParams",value:function initParams(a,b){return[a.replace(/\s+/g,"").toLowerCase(),b.replace(/\s+/g,"").toLowerCase()]}}]),a}(); |
@@ -1,3 +0,3 @@ | ||
"use strict";function _typeof(obj){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(obj){return typeof obj}:function(obj){return obj&&"function"==typeof Symbol&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj},_typeof(obj)}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")}function _defineProperties(target,props){for(var descriptor,i=0;i<props.length;i++)descriptor=props[i],descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,"value"in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,descriptor.key,descriptor)}function _createClass(Constructor,protoProps,staticProps){return protoProps&&_defineProperties(Constructor.prototype,protoProps),staticProps&&_defineProperties(Constructor,staticProps),Constructor}function _possibleConstructorReturn(self,call){return call&&("object"===_typeof(call)||"function"==typeof call)?call:_assertThisInitialized(self)}function _assertThisInitialized(self){if(void 0===self)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return self}function _getPrototypeOf(o){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(o){return o.__proto__||Object.getPrototypeOf(o)},_getPrototypeOf(o)}function _inherits(subClass,superClass){if("function"!=typeof superClass&&null!==superClass)throw new TypeError("Super expression must either be null or a function");subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,writable:!0,configurable:!0}}),superClass&&_setPrototypeOf(subClass,superClass)}function _setPrototypeOf(o,p){return _setPrototypeOf=Object.setPrototypeOf||function(o,p){return o.__proto__=p,o},_setPrototypeOf(o,p)}var Similarity=require("../interface/Similarity");module.exports=/*#__PURE__*/function(_Similarity){function Cosine(){return _classCallCheck(this,Cosine),_possibleConstructorReturn(this,_getPrototypeOf(Cosine).call(this))}return _inherits(Cosine,_Similarity),_createClass(Cosine,[{key:"similarity",value:function similarity(thanos,rival){if(Similarity.checkThanosType(thanos),Similarity.checkRivalType(rival),thanos=Similarity.initParams(thanos,rival)[0],rival=Similarity.initParams(thanos,rival)[1],!thanos.length&&!rival.length)return 1;if(!thanos.length||!rival.length)return 0;if(thanos===rival)return 1;// string vectorization | ||
for(var common=Array.from(new Set(thanos.split("").concat(rival.split("")))),vectorThanos=Cosine.stringVectorization(thanos.split(""),common),vectorRival=Cosine.stringVectorization(rival.split(""),common),dotproduct=0,mThanos=0,mRival=0,i=0;i<vectorThanos.length;++i)dotproduct+=vectorThanos[i]*vectorRival[i],mThanos+=vectorThanos[i]*vectorThanos[i],mRival+=vectorRival[i]*vectorRival[i];return mThanos=Math.sqrt(mThanos),mRival=Math.sqrt(mRival),1*dotproduct/(mThanos*mRival)}},{key:"distance",value:function distance(thanos,rival){return 1-this.similarity(thanos,rival)}// string vectorization | ||
}],[{key:"stringVectorization",value:function stringVectorization(strArr,common){return common.map(function(v){return strArr.includes(v)?1:0})}}]),Cosine}(Similarity); | ||
"use strict";function _typeof(a){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}function _possibleConstructorReturn(a,b){return b&&("object"===_typeof(b)||"function"==typeof b)?b:_assertThisInitialized(a)}function _assertThisInitialized(a){if(void 0===a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return a}function _getPrototypeOf(a){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(a){return a.__proto__||Object.getPrototypeOf(a)},_getPrototypeOf(a)}function _inherits(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function");a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,writable:!0,configurable:!0}}),b&&_setPrototypeOf(a,b)}function _setPrototypeOf(a,b){return _setPrototypeOf=Object.setPrototypeOf||function(a,b){return a.__proto__=b,a},_setPrototypeOf(a,b)}var Similarity=require("../interface/Similarity");module.exports=/*#__PURE__*/function(a){function b(){return _classCallCheck(this,b),_possibleConstructorReturn(this,_getPrototypeOf(b).call(this))}return _inherits(b,a),_createClass(b,[{key:"similarity",value:function similarity(a,c){if(Similarity.checkThanosType(a),Similarity.checkRivalType(c),a=Similarity.initParams(a,c)[0],c=Similarity.initParams(a,c)[1],!a.length&&!c.length)return 1;if(!a.length||!c.length)return 0;if(a===c)return 1;// string vectorization | ||
for(var d=Array.from(new Set(a.split("").concat(c.split("")))),e=b.stringVectorization(a.split(""),d),f=b.stringVectorization(c.split(""),d),g=0,h=0,j=0,k=0;k<e.length;++k)g+=e[k]*f[k],h+=e[k]*e[k],j+=f[k]*f[k];return h=Math.sqrt(h),j=Math.sqrt(j),1*g/(h*j)}},{key:"distance",value:function distance(a,b){return 1-this.similarity(a,b)}// string vectorization | ||
}],[{key:"stringVectorization",value:function stringVectorization(a,b){return b.map(function(b){return a.includes(b)?1:0})}}]),b}(Similarity); |
@@ -1,2 +0,2 @@ | ||
"use strict";function _typeof(obj){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(obj){return typeof obj}:function(obj){return obj&&"function"==typeof Symbol&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj},_typeof(obj)}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")}function _defineProperties(target,props){for(var descriptor,i=0;i<props.length;i++)descriptor=props[i],descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,"value"in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,descriptor.key,descriptor)}function _createClass(Constructor,protoProps,staticProps){return protoProps&&_defineProperties(Constructor.prototype,protoProps),staticProps&&_defineProperties(Constructor,staticProps),Constructor}function _possibleConstructorReturn(self,call){return call&&("object"===_typeof(call)||"function"==typeof call)?call:_assertThisInitialized(self)}function _assertThisInitialized(self){if(void 0===self)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return self}function _getPrototypeOf(o){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(o){return o.__proto__||Object.getPrototypeOf(o)},_getPrototypeOf(o)}function _inherits(subClass,superClass){if("function"!=typeof superClass&&null!==superClass)throw new TypeError("Super expression must either be null or a function");subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,writable:!0,configurable:!0}}),superClass&&_setPrototypeOf(subClass,superClass)}function _setPrototypeOf(o,p){return _setPrototypeOf=Object.setPrototypeOf||function(o,p){return o.__proto__=p,o},_setPrototypeOf(o,p)}var Similarity=require("../interface/Similarity");module.exports=/*#__PURE__*/function(_Similarity){function DiceCoefficient(){return _classCallCheck(this,DiceCoefficient),_possibleConstructorReturn(this,_getPrototypeOf(DiceCoefficient).call(this))}return _inherits(DiceCoefficient,_Similarity),_createClass(DiceCoefficient,[{key:"similarity",value:function similarity(thanos,rival){Similarity.checkThanosType(thanos),Similarity.checkRivalType(rival),thanos=Similarity.initParams(thanos,rival)[0],rival=Similarity.initParams(thanos,rival)[1];var _ref=[thanos.length,rival.length],length1=_ref[0],length2=_ref[1];if(!thanos.length&&!rival.length)return 1;if(thanos===rival)return 1;if(2>length1||2>length2)return 0;// get the intersecting character, two strings as a group | ||
for(var thanosBigrams=new Map,i=0;i<length1-1;i++){var bigram=thanos.substr(i,2),count=thanosBigrams.has(bigram)?thanosBigrams.get(bigram)+1:1;thanosBigrams.set(bigram,count)}for(var intersectionSize=0,_i=0;_i<length2-1;_i++){var _bigram=rival.substr(_i,2),_count=thanosBigrams.has(_bigram)?thanosBigrams.get(_bigram):0;0<_count&&(thanosBigrams.set(_bigram,_count-1),++intersectionSize)}return 2*intersectionSize/(length1+length2-2)}},{key:"distance",value:function distance(thanos,rival){return 1-this.similarity(thanos,rival)}}]),DiceCoefficient}(Similarity); | ||
"use strict";function _typeof(a){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}function _possibleConstructorReturn(a,b){return b&&("object"===_typeof(b)||"function"==typeof b)?b:_assertThisInitialized(a)}function _assertThisInitialized(a){if(void 0===a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return a}function _getPrototypeOf(a){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(a){return a.__proto__||Object.getPrototypeOf(a)},_getPrototypeOf(a)}function _inherits(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function");a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,writable:!0,configurable:!0}}),b&&_setPrototypeOf(a,b)}function _setPrototypeOf(a,b){return _setPrototypeOf=Object.setPrototypeOf||function(a,b){return a.__proto__=b,a},_setPrototypeOf(a,b)}var Similarity=require("../interface/Similarity");module.exports=/*#__PURE__*/function(a){function b(){return _classCallCheck(this,b),_possibleConstructorReturn(this,_getPrototypeOf(b).call(this))}return _inherits(b,a),_createClass(b,[{key:"similarity",value:function similarity(a,b){Similarity.checkThanosType(a),Similarity.checkRivalType(b),a=Similarity.initParams(a,b)[0],b=Similarity.initParams(a,b)[1];var c=[a.length,b.length],d=c[0],e=c[1];if(!a.length&&!b.length)return 1;if(a===b)return 1;if(2>d||2>e)return 0;// get the intersecting character, two strings as a group | ||
for(var f=new Map,g=0;g<d-1;g++){var h=a.substr(g,2),j=f.has(h)?f.get(h)+1:1;f.set(h,j)}for(var k=0,l=0;l<e-1;l++){var m=b.substr(l,2),n=f.has(m)?f.get(m):0;0<n&&(f.set(m,n-1),++k)}return 2*k/(d+e-2)}},{key:"distance",value:function distance(a,b){return 1-this.similarity(a,b)}}]),b}(Similarity); |
@@ -1,2 +0,2 @@ | ||
"use strict";function _typeof(obj){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(obj){return typeof obj}:function(obj){return obj&&"function"==typeof Symbol&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj},_typeof(obj)}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")}function _defineProperties(target,props){for(var descriptor,i=0;i<props.length;i++)descriptor=props[i],descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,"value"in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,descriptor.key,descriptor)}function _createClass(Constructor,protoProps,staticProps){return protoProps&&_defineProperties(Constructor.prototype,protoProps),staticProps&&_defineProperties(Constructor,staticProps),Constructor}function _possibleConstructorReturn(self,call){return call&&("object"===_typeof(call)||"function"==typeof call)?call:_assertThisInitialized(self)}function _assertThisInitialized(self){if(void 0===self)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return self}function _getPrototypeOf(o){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(o){return o.__proto__||Object.getPrototypeOf(o)},_getPrototypeOf(o)}function _inherits(subClass,superClass){if("function"!=typeof superClass&&null!==superClass)throw new TypeError("Super expression must either be null or a function");subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,writable:!0,configurable:!0}}),superClass&&_setPrototypeOf(subClass,superClass)}function _setPrototypeOf(o,p){return _setPrototypeOf=Object.setPrototypeOf||function(o,p){return o.__proto__=p,o},_setPrototypeOf(o,p)}var Similarity=require("../interface/Similarity");module.exports=/*#__PURE__*/function(_Similarity){function JaccardIndex(){return _classCallCheck(this,JaccardIndex),_possibleConstructorReturn(this,_getPrototypeOf(JaccardIndex).call(this))}return _inherits(JaccardIndex,_Similarity),_createClass(JaccardIndex,[{key:"similarity",value:function similarity(thanos,rival){if(Similarity.checkThanosType(thanos),Similarity.checkRivalType(rival),thanos=Similarity.initParams(thanos,rival)[0],rival=Similarity.initParams(thanos,rival)[1],!thanos.length&&!rival.length)return 1;if(thanos===rival)return 1;// split and Set | ||
var union=new Set(thanos.split("").concat(rival.split(""))),intersection=new Set(thanos.split("").filter(function(v){return new Set(rival).has(v)}));return 1*intersection.size/union.size}},{key:"distance",value:function distance(thanos,rival){return 1-this.similarity(thanos,rival)}}]),JaccardIndex}(Similarity); | ||
"use strict";function _typeof(a){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}function _possibleConstructorReturn(a,b){return b&&("object"===_typeof(b)||"function"==typeof b)?b:_assertThisInitialized(a)}function _assertThisInitialized(a){if(void 0===a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return a}function _getPrototypeOf(a){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(a){return a.__proto__||Object.getPrototypeOf(a)},_getPrototypeOf(a)}function _inherits(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function");a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,writable:!0,configurable:!0}}),b&&_setPrototypeOf(a,b)}function _setPrototypeOf(a,b){return _setPrototypeOf=Object.setPrototypeOf||function(a,b){return a.__proto__=b,a},_setPrototypeOf(a,b)}var Similarity=require("../interface/Similarity");module.exports=/*#__PURE__*/function(a){function b(){return _classCallCheck(this,b),_possibleConstructorReturn(this,_getPrototypeOf(b).call(this))}return _inherits(b,a),_createClass(b,[{key:"similarity",value:function similarity(a,b){if(Similarity.checkThanosType(a),Similarity.checkRivalType(b),a=Similarity.initParams(a,b)[0],b=Similarity.initParams(a,b)[1],!a.length&&!b.length)return 1;if(a===b)return 1;// split and Set | ||
var c=new Set(a.split("").concat(b.split(""))),d=new Set(a.split("").filter(function(a){return new Set(b).has(a)}));return 1*d.size/c.size}},{key:"distance",value:function distance(a,b){return 1-this.similarity(a,b)}}]),b}(Similarity); |
@@ -1,4 +0,4 @@ | ||
"use strict";function _typeof(obj){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(obj){return typeof obj}:function(obj){return obj&&"function"==typeof Symbol&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj},_typeof(obj)}function _toConsumableArray(arr){return _arrayWithoutHoles(arr)||_iterableToArray(arr)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _iterableToArray(iter){if(Symbol.iterator in Object(iter)||"[object Arguments]"===Object.prototype.toString.call(iter))return Array.from(iter)}function _arrayWithoutHoles(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i<arr.length;i++)arr2[i]=arr[i];return arr2}}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")}function _defineProperties(target,props){for(var descriptor,i=0;i<props.length;i++)descriptor=props[i],descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,"value"in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,descriptor.key,descriptor)}function _createClass(Constructor,protoProps,staticProps){return protoProps&&_defineProperties(Constructor.prototype,protoProps),staticProps&&_defineProperties(Constructor,staticProps),Constructor}function _possibleConstructorReturn(self,call){return call&&("object"===_typeof(call)||"function"==typeof call)?call:_assertThisInitialized(self)}function _assertThisInitialized(self){if(void 0===self)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return self}function _getPrototypeOf(o){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(o){return o.__proto__||Object.getPrototypeOf(o)},_getPrototypeOf(o)}function _inherits(subClass,superClass){if("function"!=typeof superClass&&null!==superClass)throw new TypeError("Super expression must either be null or a function");subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,writable:!0,configurable:!0}}),superClass&&_setPrototypeOf(subClass,superClass)}function _setPrototypeOf(o,p){return _setPrototypeOf=Object.setPrototypeOf||function(o,p){return o.__proto__=p,o},_setPrototypeOf(o,p)}var Similarity=require("../interface/Similarity");module.exports=/*#__PURE__*/function(_Similarity){function Levenshtein(){return _classCallCheck(this,Levenshtein),_possibleConstructorReturn(this,_getPrototypeOf(Levenshtein).call(this))}return _inherits(Levenshtein,_Similarity),_createClass(Levenshtein,[{key:"similarity",value:function similarity(thanos,rival){return Similarity.checkThanosType(thanos),Similarity.checkRivalType(rival),thanos=Similarity.initParams(thanos,rival)[0],rival=Similarity.initParams(thanos,rival)[1],thanos.length||rival.length?1-1*this.distance(thanos,rival)/Math.max(thanos.length,rival.length):1}// edit distance | ||
},{key:"distance",value:function distance(thanos,rival){if(Similarity.checkThanosType(thanos),Similarity.checkRivalType(rival),thanos=Similarity.initParams(thanos,rival)[0],rival=Similarity.initParams(thanos,rival)[1],thanos===rival)return 0;var _ref=[thanos.length,rival.length],len1=_ref[0],len2=_ref[1];if(!len1)return len2;if(!len2)return len1;// init array | ||
for(var dynamicArray=_toConsumableArray(Array(len1+1)).map(function(){return Array(len2+1).fill(0)}),i=0;i<=len1;++i)dynamicArray[i][0]=i;for(var j=0;j<=len2;++j)dynamicArray[0][j]=j;for(var temp,_i=1;_i<=len1;++_i)for(var _j=1;_j<=len2;++_j)// delete insert replace | ||
temp=thanos[_i-1]===rival[_j-1]?0:1,dynamicArray[_i][_j]=Math.min(dynamicArray[_i-1][_j]+1,dynamicArray[_i][_j-1]+1,dynamicArray[_i-1][_j-1]+temp);return dynamicArray[len1][len2]}}]),Levenshtein}(Similarity); | ||
"use strict";function _typeof(a){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function _toConsumableArray(a){return _arrayWithoutHoles(a)||_iterableToArray(a)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _iterableToArray(a){if(Symbol.iterator in Object(a)||"[object Arguments]"===Object.prototype.toString.call(a))return Array.from(a)}function _arrayWithoutHoles(a){if(Array.isArray(a)){for(var b=0,c=Array(a.length);b<a.length;b++)c[b]=a[b];return c}}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}function _possibleConstructorReturn(a,b){return b&&("object"===_typeof(b)||"function"==typeof b)?b:_assertThisInitialized(a)}function _assertThisInitialized(a){if(void 0===a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return a}function _getPrototypeOf(a){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(a){return a.__proto__||Object.getPrototypeOf(a)},_getPrototypeOf(a)}function _inherits(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function");a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,writable:!0,configurable:!0}}),b&&_setPrototypeOf(a,b)}function _setPrototypeOf(a,b){return _setPrototypeOf=Object.setPrototypeOf||function(a,b){return a.__proto__=b,a},_setPrototypeOf(a,b)}var Similarity=require("../interface/Similarity");module.exports=/*#__PURE__*/function(a){function b(){return _classCallCheck(this,b),_possibleConstructorReturn(this,_getPrototypeOf(b).call(this))}return _inherits(b,a),_createClass(b,[{key:"similarity",value:function similarity(a,b){return Similarity.checkThanosType(a),Similarity.checkRivalType(b),a=Similarity.initParams(a,b)[0],b=Similarity.initParams(a,b)[1],a.length||b.length?1-1*this.distance(a,b)/Math.max(a.length,b.length):1}// edit distance | ||
},{key:"distance",value:function distance(a,b){if(Similarity.checkThanosType(a),Similarity.checkRivalType(b),a=Similarity.initParams(a,b)[0],b=Similarity.initParams(a,b)[1],a===b)return 0;var c=[a.length,b.length],d=c[0],e=c[1];if(!d)return e;if(!e)return d;// init array | ||
for(var f=_toConsumableArray(Array(d+1)).map(function(){return Array(e+1).fill(0)}),g=0;g<=d;++g)f[g][0]=g;for(var l=0;l<=e;++l)f[0][l]=l;for(var h,k=1;k<=d;++k)for(var j=1;j<=e;++j)// delete insert replace | ||
h=a[k-1]===b[j-1]?0:1,f[k][j]=Math.min(f[k-1][j]+1,f[k][j-1]+1,f[k-1][j-1]+h);return f[d][e]}}]),b}(Similarity); |
@@ -1,2 +0,2 @@ | ||
"use strict";function _typeof(obj){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(obj){return typeof obj}:function(obj){return obj&&"function"==typeof Symbol&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj},_typeof(obj)}function _toConsumableArray(arr){return _arrayWithoutHoles(arr)||_iterableToArray(arr)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _iterableToArray(iter){if(Symbol.iterator in Object(iter)||"[object Arguments]"===Object.prototype.toString.call(iter))return Array.from(iter)}function _arrayWithoutHoles(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i<arr.length;i++)arr2[i]=arr[i];return arr2}}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")}function _defineProperties(target,props){for(var descriptor,i=0;i<props.length;i++)descriptor=props[i],descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,"value"in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,descriptor.key,descriptor)}function _createClass(Constructor,protoProps,staticProps){return protoProps&&_defineProperties(Constructor.prototype,protoProps),staticProps&&_defineProperties(Constructor,staticProps),Constructor}function _possibleConstructorReturn(self,call){return call&&("object"===_typeof(call)||"function"==typeof call)?call:_assertThisInitialized(self)}function _assertThisInitialized(self){if(void 0===self)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return self}function _getPrototypeOf(o){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(o){return o.__proto__||Object.getPrototypeOf(o)},_getPrototypeOf(o)}function _inherits(subClass,superClass){if("function"!=typeof superClass&&null!==superClass)throw new TypeError("Super expression must either be null or a function");subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,writable:!0,configurable:!0}}),superClass&&_setPrototypeOf(subClass,superClass)}function _setPrototypeOf(o,p){return _setPrototypeOf=Object.setPrototypeOf||function(o,p){return o.__proto__=p,o},_setPrototypeOf(o,p)}var Similarity=require("../interface/Similarity");module.exports=/*#__PURE__*/function(_Similarity){function LongestCommonSubsequence(){return _classCallCheck(this,LongestCommonSubsequence),_possibleConstructorReturn(this,_getPrototypeOf(LongestCommonSubsequence).call(this))}return _inherits(LongestCommonSubsequence,_Similarity),_createClass(LongestCommonSubsequence,[{key:"similarity",value:function similarity(thanos,rival){return Similarity.checkThanosType(thanos),Similarity.checkRivalType(rival),thanos=Similarity.initParams(thanos,rival)[0],rival=Similarity.initParams(thanos,rival)[1],thanos.length||rival.length?thanos===rival?1:2*LongestCommonSubsequence.lcsLength(thanos,rival)/(thanos.length+rival.length):1}},{key:"distance",value:function distance(thanos,rival){return thanos.length+rival.length-2*LongestCommonSubsequence.lcsLength(thanos,rival)}// Return the length of Longest Common Subsequence (LCS) between strings thanos and rival | ||
}],[{key:"lcsLength",value:function lcsLength(thanos,rival){Similarity.checkThanosType(thanos),Similarity.checkRivalType(rival),thanos=Similarity.initParams(thanos,rival)[0],rival=Similarity.initParams(thanos,rival)[1];for(var _ref=[thanos.length,rival.length],len1=_ref[0],len2=_ref[1],dynamicArray=_toConsumableArray(Array(len1+1)).map(function(){return Array(len2+1).fill(0)}),i=1;i<=len1;++i)for(var j=1;j<=len2;++j)dynamicArray[i][j]=thanos[i-1]===rival[j-1]?dynamicArray[i-1][j-1]+1:Math.max(dynamicArray[i][j-1],dynamicArray[i-1][j]);return dynamicArray[len1][len2]}}]),LongestCommonSubsequence}(Similarity); | ||
"use strict";function _typeof(a){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function _toConsumableArray(a){return _arrayWithoutHoles(a)||_iterableToArray(a)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _iterableToArray(a){if(Symbol.iterator in Object(a)||"[object Arguments]"===Object.prototype.toString.call(a))return Array.from(a)}function _arrayWithoutHoles(a){if(Array.isArray(a)){for(var b=0,c=Array(a.length);b<a.length;b++)c[b]=a[b];return c}}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}function _possibleConstructorReturn(a,b){return b&&("object"===_typeof(b)||"function"==typeof b)?b:_assertThisInitialized(a)}function _assertThisInitialized(a){if(void 0===a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return a}function _getPrototypeOf(a){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(a){return a.__proto__||Object.getPrototypeOf(a)},_getPrototypeOf(a)}function _inherits(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function");a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,writable:!0,configurable:!0}}),b&&_setPrototypeOf(a,b)}function _setPrototypeOf(a,b){return _setPrototypeOf=Object.setPrototypeOf||function(a,b){return a.__proto__=b,a},_setPrototypeOf(a,b)}var Similarity=require("../interface/Similarity");module.exports=/*#__PURE__*/function(a){function b(){return _classCallCheck(this,b),_possibleConstructorReturn(this,_getPrototypeOf(b).call(this))}return _inherits(b,a),_createClass(b,[{key:"similarity",value:function similarity(a,c){return Similarity.checkThanosType(a),Similarity.checkRivalType(c),a=Similarity.initParams(a,c)[0],c=Similarity.initParams(a,c)[1],a.length||c.length?a===c?1:2*b.lcsLength(a,c)/(a.length+c.length):1}},{key:"distance",value:function distance(a,c){return a.length+c.length-2*b.lcsLength(a,c)}// Return the length of Longest Common Subsequence (LCS) between strings thanos and rival | ||
}],[{key:"lcsLength",value:function lcsLength(a,b){Similarity.checkThanosType(a),Similarity.checkRivalType(b),a=Similarity.initParams(a,b)[0],b=Similarity.initParams(a,b)[1];for(var c=[a.length,b.length],d=c[0],e=c[1],f=_toConsumableArray(Array(d+1)).map(function(){return Array(e+1).fill(0)}),g=1;g<=d;++g)for(var h=1;h<=e;++h)f[g][h]=a[g-1]===b[h-1]?f[g-1][h-1]+1:Math.max(f[g][h-1],f[g-1][h]);return f[d][e]}}]),b}(Similarity); |
@@ -1,1 +0,1 @@ | ||
"use strict";function _typeof(obj){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(obj){return typeof obj}:function(obj){return obj&&"function"==typeof Symbol&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj},_typeof(obj)}function _toConsumableArray(arr){return _arrayWithoutHoles(arr)||_iterableToArray(arr)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _iterableToArray(iter){if(Symbol.iterator in Object(iter)||"[object Arguments]"===Object.prototype.toString.call(iter))return Array.from(iter)}function _arrayWithoutHoles(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i<arr.length;i++)arr2[i]=arr[i];return arr2}}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")}function _defineProperties(target,props){for(var descriptor,i=0;i<props.length;i++)descriptor=props[i],descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,"value"in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,descriptor.key,descriptor)}function _createClass(Constructor,protoProps,staticProps){return protoProps&&_defineProperties(Constructor.prototype,protoProps),staticProps&&_defineProperties(Constructor,staticProps),Constructor}function _possibleConstructorReturn(self,call){return call&&("object"===_typeof(call)||"function"==typeof call)?call:_assertThisInitialized(self)}function _assertThisInitialized(self){if(void 0===self)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return self}function _getPrototypeOf(o){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(o){return o.__proto__||Object.getPrototypeOf(o)},_getPrototypeOf(o)}function _inherits(subClass,superClass){if("function"!=typeof superClass&&null!==superClass)throw new TypeError("Super expression must either be null or a function");subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,writable:!0,configurable:!0}}),superClass&&_setPrototypeOf(subClass,superClass)}function _setPrototypeOf(o,p){return _setPrototypeOf=Object.setPrototypeOf||function(o,p){return o.__proto__=p,o},_setPrototypeOf(o,p)}var Similarity=require("../interface/Similarity");module.exports=/*#__PURE__*/function(_Similarity){function MetricLCS(){return _classCallCheck(this,MetricLCS),_possibleConstructorReturn(this,_getPrototypeOf(MetricLCS).call(this))}return _inherits(MetricLCS,_Similarity),_createClass(MetricLCS,[{key:"similarity",value:function similarity(thanos,rival){return Similarity.checkThanosType(thanos),Similarity.checkRivalType(rival),thanos=Similarity.initParams(thanos,rival)[0],rival=Similarity.initParams(thanos,rival)[1],thanos.length||rival.length?thanos===rival?1:1*MetricLCS.lcsLength(thanos,rival)/Math.max(thanos.length,rival.length):1}},{key:"distance",value:function distance(thanos,rival){return Similarity.checkThanosType(thanos),Similarity.checkRivalType(rival),thanos===rival?0:1-this.similarity(thanos,rival)}}],[{key:"lcsLength",value:function lcsLength(thanos,rival){Similarity.checkThanosType(thanos),Similarity.checkRivalType(rival),thanos=Similarity.initParams(thanos,rival)[0],rival=Similarity.initParams(thanos,rival)[1];for(var _ref=[thanos.length,rival.length],len1=_ref[0],len2=_ref[1],dynamicArray=_toConsumableArray(Array(len1+1)).map(function(){return Array(len2+1).fill(0)}),i=1;i<=len1;++i)for(var j=1;j<=len2;++j)dynamicArray[i][j]=thanos[i-1]===rival[j-1]?dynamicArray[i-1][j-1]+1:Math.max(dynamicArray[i][j-1],dynamicArray[i-1][j]);return dynamicArray[len1][len2]}}]),MetricLCS}(Similarity); | ||
"use strict";function _typeof(a){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function _toConsumableArray(a){return _arrayWithoutHoles(a)||_iterableToArray(a)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _iterableToArray(a){if(Symbol.iterator in Object(a)||"[object Arguments]"===Object.prototype.toString.call(a))return Array.from(a)}function _arrayWithoutHoles(a){if(Array.isArray(a)){for(var b=0,c=Array(a.length);b<a.length;b++)c[b]=a[b];return c}}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}function _possibleConstructorReturn(a,b){return b&&("object"===_typeof(b)||"function"==typeof b)?b:_assertThisInitialized(a)}function _assertThisInitialized(a){if(void 0===a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return a}function _getPrototypeOf(a){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(a){return a.__proto__||Object.getPrototypeOf(a)},_getPrototypeOf(a)}function _inherits(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function");a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,writable:!0,configurable:!0}}),b&&_setPrototypeOf(a,b)}function _setPrototypeOf(a,b){return _setPrototypeOf=Object.setPrototypeOf||function(a,b){return a.__proto__=b,a},_setPrototypeOf(a,b)}var Similarity=require("../interface/Similarity");module.exports=/*#__PURE__*/function(a){function b(){return _classCallCheck(this,b),_possibleConstructorReturn(this,_getPrototypeOf(b).call(this))}return _inherits(b,a),_createClass(b,[{key:"similarity",value:function similarity(a,c){return Similarity.checkThanosType(a),Similarity.checkRivalType(c),a=Similarity.initParams(a,c)[0],c=Similarity.initParams(a,c)[1],a.length||c.length?a===c?1:1*b.lcsLength(a,c)/Math.max(a.length,c.length):1}},{key:"distance",value:function distance(a,b){return Similarity.checkThanosType(a),Similarity.checkRivalType(b),a===b?0:1-this.similarity(a,b)}}],[{key:"lcsLength",value:function lcsLength(a,b){Similarity.checkThanosType(a),Similarity.checkRivalType(b),a=Similarity.initParams(a,b)[0],b=Similarity.initParams(a,b)[1];for(var c=[a.length,b.length],d=c[0],e=c[1],f=_toConsumableArray(Array(d+1)).map(function(){return Array(e+1).fill(0)}),g=1;g<=d;++g)for(var h=1;h<=e;++h)f[g][h]=a[g-1]===b[h-1]?f[g-1][h-1]+1:Math.max(f[g][h-1],f[g-1][h]);return f[d][e]}}]),b}(Similarity); |
{ | ||
"name": "string-comparison", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A library implementing different string similarity", | ||
"main": "./src/index.js", | ||
"main": "./lib/index.js", | ||
"scripts": { | ||
"test": "node ./node_modules/mocha/bin/mocha", | ||
"lint": "eslint src", | ||
"build": "./node_modules/.bin/babel src --out-dir lib" | ||
"build": "./node_modules/.bin/babel src --out-dir lib" | ||
}, | ||
@@ -11,0 +11,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
31
51637
813
2