dice-coefficient
Advanced tools
Comparing version 0.1.4 to 1.0.0
150
cli.js
#!/usr/bin/env node | ||
'use strict'; | ||
/* | ||
* Dependencies. | ||
/** | ||
* @author Titus Wormer | ||
* @copyright 2014 Titus Wormer | ||
* @license MIT | ||
* @module dice-coefficient | ||
* @fileoverview CLI for `dice-coefficient`. | ||
*/ | ||
var diceCoefficient, | ||
pack; | ||
'use strict'; | ||
pack = require('./package.json'); | ||
diceCoefficient = require('./'); | ||
/* Dependencies. */ | ||
var pack = require('./package.json'); | ||
var dice = require('./'); | ||
/* | ||
* Detect if a value is expected to be piped in. | ||
*/ | ||
/* Arguments. */ | ||
var argv = process.argv.slice(2); | ||
var expextPipeIn; | ||
expextPipeIn = !process.stdin.isTTY; | ||
/* | ||
* Arguments. | ||
*/ | ||
var argv; | ||
argv = process.argv.slice(2); | ||
/* | ||
* Command. | ||
*/ | ||
var command; | ||
command = Object.keys(pack.bin)[0]; | ||
/** | ||
* Help. | ||
* | ||
* @return {string} | ||
*/ | ||
function help() { | ||
return [ | ||
'', | ||
'Usage: ' + command + ' [options] <word> <word>', | ||
'', | ||
pack.description, | ||
'', | ||
'Options:', | ||
'', | ||
' -h, --help output usage information', | ||
' -v, --version output version number', | ||
'', | ||
'Usage:', | ||
'', | ||
'# output edit distance', | ||
'$ ' + command + ' night nacht', | ||
'# ' + diceCoefficient('night', 'nacht'), | ||
'', | ||
'# output edit distance from stdin', | ||
'$ echo "saturday sunday" | ' + command, | ||
'# ' + diceCoefficient('saturday', 'sunday') | ||
].join('\n ') + '\n'; | ||
/* Program. */ | ||
if ( | ||
argv.indexOf('--help') !== -1 || | ||
argv.indexOf('-h') !== -1 | ||
) { | ||
console.log(help()); | ||
} else if ( | ||
argv.indexOf('--version') !== -1 || | ||
argv.indexOf('-v') !== -1 | ||
) { | ||
console.log(pack.version); | ||
} else if (argv.length) { | ||
getEditDistance(argv.join(' ').split(/\s+/g)); | ||
} else { | ||
process.stdin.resume(); | ||
process.stdin.setEncoding('utf8'); | ||
process.stdin.on('data', function (data) { | ||
getEditDistance(data.trim().split(/\s+/g)); | ||
}); | ||
} | ||
@@ -73,34 +46,37 @@ | ||
function getEditDistance(values) { | ||
if (values.length === 2) { | ||
console.log(diceCoefficient(values[0], values[1]) || 0); | ||
} else { | ||
process.stderr.write(help()); | ||
process.exit(1); | ||
} | ||
if (values.length === 2) { | ||
console.log(dice(values[0], values[1]) || 0); | ||
} else { | ||
process.stderr.write(help()); | ||
process.exit(1); | ||
} | ||
} | ||
/* | ||
* Program. | ||
/** | ||
* Help. | ||
* | ||
* @return {string} | ||
*/ | ||
if ( | ||
argv.indexOf('--help') !== -1 || | ||
argv.indexOf('-h') !== -1 | ||
) { | ||
console.log(help()); | ||
} else if ( | ||
argv.indexOf('--version') !== -1 || | ||
argv.indexOf('-v') !== -1 | ||
) { | ||
console.log(pack.version); | ||
} else if (argv.length) { | ||
getEditDistance(argv.join(' ').split(/\s+/g)); | ||
} else if (!expextPipeIn) { | ||
getEditDistance([]); | ||
} else { | ||
process.stdin.resume(); | ||
process.stdin.setEncoding('utf8'); | ||
process.stdin.on('data', function (data) { | ||
getEditDistance(data.trim().split(/\s+/g)); | ||
}); | ||
function help() { | ||
return [ | ||
'', | ||
'Usage: ' + pack.name + ' [options] <word> <word>', | ||
'', | ||
pack.description, | ||
'', | ||
'Options:', | ||
'', | ||
' -h, --help output usage information', | ||
' -v, --version output version number', | ||
'', | ||
'Usage:', | ||
'', | ||
'# output edit distance', | ||
'$ ' + pack.name + ' night nacht', | ||
'# ' + dice('night', 'nacht'), | ||
'', | ||
'# output edit distance from stdin', | ||
'$ echo "saturday sunday" | ' + pack.name, | ||
'# ' + dice('saturday', 'sunday') | ||
].join('\n ') + '\n'; | ||
} |
81
index.js
@@ -0,11 +1,17 @@ | ||
/** | ||
* @author Titus Wormer | ||
* @copyright 2014 Titus Wormer | ||
* @license MIT | ||
* @module dice-coefficient | ||
* @fileoverview Sørensen–Dice coefficient. | ||
*/ | ||
'use strict'; | ||
var getBigrams; | ||
/* Dependencies. */ | ||
var bigrams = require('n-gram').bigram; | ||
/* | ||
* Module dependencies. | ||
*/ | ||
/* Expose. */ | ||
module.exports = diceCoefficient; | ||
getBigrams = require('n-gram').bigram; | ||
/** | ||
@@ -15,51 +21,34 @@ * Get the edit-distance according to Dice between two values. | ||
* @param {*} value - First value. | ||
* @param {*} alternative - Second value. | ||
* @param {*} right - Second value. | ||
* @return {number} Edit distance. | ||
*/ | ||
function diceCoefficient(value, alternative) { | ||
var pairs, | ||
alternativePairs, | ||
intersections, | ||
iterator, | ||
length, | ||
alternativeLength, | ||
alternativeIterator, | ||
alternativePair, | ||
pair; | ||
var left = bigrams(String(value).toLowerCase()); | ||
var right = bigrams(String(alternative).toLowerCase()); | ||
var rightLength = right.length; | ||
var length = left.length; | ||
var index = -1; | ||
var intersections = 0; | ||
var rightPair; | ||
var leftPair; | ||
var offset; | ||
pairs = getBigrams(String(value).toLowerCase()); | ||
alternativePairs = getBigrams(String(alternative).toLowerCase()); | ||
intersections = 0; | ||
iterator = -1; | ||
alternativeLength = alternativePairs.length; | ||
length = pairs.length; | ||
while (++index < length) { | ||
leftPair = left[index]; | ||
offset = -1; | ||
while (++iterator < length) { | ||
pair = pairs[iterator]; | ||
while (++offset < rightLength) { | ||
rightPair = right[offset]; | ||
alternativeIterator = -1; | ||
if (leftPair === rightPair) { | ||
intersections++; | ||
while (++alternativeIterator < alternativeLength) { | ||
alternativePair = alternativePairs[alternativeIterator]; | ||
if (pair === alternativePair) { | ||
intersections++; | ||
/* | ||
* Make sure this pair never matches again | ||
*/ | ||
alternativePairs[alternativeIterator] = ''; | ||
break; | ||
} | ||
} | ||
/* Make sure this pair never matches again */ | ||
right[offset] = ''; | ||
break; | ||
} | ||
} | ||
} | ||
return 2 * intersections / (pairs.length + alternativeLength); | ||
return 2 * intersections / (left.length + rightLength); | ||
} | ||
/* | ||
* Expose `diceCoefficient`. | ||
*/ | ||
module.exports = diceCoefficient; |
{ | ||
"name": "dice-coefficient", | ||
"version": "0.1.4", | ||
"version": "1.0.0", | ||
"description": "Sørensen–Dice coefficient", | ||
@@ -15,36 +15,67 @@ "license": "MIT", | ||
"dependencies": { | ||
"n-gram": "^0.1.0" | ||
"n-gram": "^0.1.2" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/wooorm/dice-coefficient.git" | ||
"files": [ | ||
"index.js", | ||
"cli.js" | ||
], | ||
"bin": "cli.js", | ||
"repository": "https://github.com/wooorm/dice-coefficient", | ||
"bugs": "https://github.com/wooorm/dice-coefficient/issues", | ||
"author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)", | ||
"contributors": [ | ||
"Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)" | ||
], | ||
"engines": { | ||
"node": ">=0.11.0" | ||
}, | ||
"author": "Titus Wormer <tituswormer@gmail.com>", | ||
"bin": { | ||
"dice-coefficient": "cli.js" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^0.10.0", | ||
"istanbul": "^0.3.0", | ||
"jscs": "^1.0.0", | ||
"jscs-jsdoc": "^0.3.0", | ||
"matcha": "^0.6.0", | ||
"mocha": "^2.0.0" | ||
"browserify": "^13.0.0", | ||
"esmangle": "^1.0.0", | ||
"execa": "^0.4.0", | ||
"has": "^1.0.1", | ||
"nyc": "^7.1.0", | ||
"remark-cli": "^1.0.0", | ||
"remark-comment-config": "^4.0.0", | ||
"remark-github": "^5.0.0", | ||
"remark-lint": "^4.0.0", | ||
"remark-validate-links": "^4.0.0", | ||
"tape": "^4.4.0", | ||
"xo": "^0.16.0" | ||
}, | ||
"scripts": { | ||
"test": "_mocha --check-leaks test.js", | ||
"test-cli": "bash test.sh", | ||
"test-coveralls": "istanbul cover _mocha --report lcovonly -- --check-leaks test.js", | ||
"test-travis": "npm run test-cli && npm run test-coveralls", | ||
"coverage": "istanbul cover _mocha -- -- test.js", | ||
"lint-api": "eslint index.js", | ||
"lint-cli": "eslint --rule no-process-exit:false cli.js", | ||
"lint-test": "eslint --env mocha test.js", | ||
"lint-benchmark": "eslint --global suite,set,bench benchmark.js", | ||
"lint-style": "jscs --reporter inline index.js benchmark.js cli.js test.js", | ||
"lint": "npm run lint-api && npm run lint-benchmark && npm run lint-cli && npm run lint-test && npm run lint-style", | ||
"make": "npm run lint && npm run coverage", | ||
"install-benchmark": "npm install natural", | ||
"benchmark": "matcha benchmark.js" | ||
"build-md": "remark . --quiet --frail", | ||
"build-bundle": "browserify index.js -s diceCoefficient > dice-coefficient.js", | ||
"build-mangle": "esmangle dice-coefficient.js > dice-coefficient.min.js", | ||
"build": "npm run build-md && npm run build-bundle && npm run build-mangle", | ||
"lint": "xo", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test": "npm run build && npm run lint && npm run test-coverage" | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
}, | ||
"xo": { | ||
"space": true, | ||
"ignores": [ | ||
"dice-coefficient.js", | ||
"dice-coefficient.min.js" | ||
] | ||
}, | ||
"remarkConfig": { | ||
"output": true, | ||
"plugins": { | ||
"comment-config": null, | ||
"lint": null, | ||
"github": null, | ||
"validate-links": null | ||
}, | ||
"settings": { | ||
"bullet": "*" | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
7169
12
119
74
1
1
Updatedn-gram@^0.1.2