dice-coefficient
Advanced tools
Comparing version 1.0.2 to 1.0.3
86
cli.js
#!/usr/bin/env node | ||
'use strict'; | ||
'use strict' | ||
var pack = require('./package.json'); | ||
var dice = require('./'); | ||
var pack = require('./package.json') | ||
var dice = require('.') | ||
var argv = process.argv.slice(2); | ||
var argv = process.argv.slice(2) | ||
/* 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)); | ||
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 === 0) { | ||
process.stdin.resume() | ||
process.stdin.setEncoding('utf8') | ||
process.stdin.on('data', function(data) { | ||
getEditDistance(data.trim().split(/\s+/g)) | ||
}) | ||
} else { | ||
process.stdin.resume(); | ||
process.stdin.setEncoding('utf8'); | ||
process.stdin.on('data', function (data) { | ||
getEditDistance(data.trim().split(/\s+/g)); | ||
}); | ||
getEditDistance(argv.join(' ').split(/\s+/g)) | ||
} | ||
@@ -33,6 +27,6 @@ | ||
if (values.length === 2) { | ||
console.log(dice(values[0], values[1]) || 0); | ||
console.log(dice(values[0], values[1]) || 0) | ||
} else { | ||
process.stderr.write(help()); | ||
process.exit(1); | ||
process.stderr.write(help()) | ||
process.exit(1) | ||
} | ||
@@ -42,23 +36,25 @@ } | ||
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'; | ||
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' | ||
) | ||
} |
44
index.js
@@ -1,32 +0,34 @@ | ||
'use strict'; | ||
'use strict' | ||
var bigrams = require('n-gram').bigram; | ||
var bigrams = require('n-gram').bigram | ||
module.exports = diceCoefficient; | ||
module.exports = diceCoefficient | ||
/* Get the edit-distance according to Dice between two values. */ | ||
// Get the edit-distance according to Dice between two values. | ||
function diceCoefficient(value, alternative) { | ||
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; | ||
var val = String(value).toLowerCase() | ||
var alt = String(alternative).toLowerCase() | ||
var left = val.length === 1 ? [val] : bigrams(val) | ||
var right = alt.length === 1 ? [alt] : bigrams(alt) | ||
var leftLength = left.length | ||
var rightLength = right.length | ||
var index = -1 | ||
var intersections = 0 | ||
var leftPair | ||
var rightPair | ||
var offset | ||
while (++index < length) { | ||
leftPair = left[index]; | ||
offset = -1; | ||
while (++index < leftLength) { | ||
leftPair = left[index] | ||
offset = -1 | ||
while (++offset < rightLength) { | ||
rightPair = right[offset]; | ||
rightPair = right[offset] | ||
if (leftPair === rightPair) { | ||
intersections++; | ||
intersections++ | ||
/* Make sure this pair never matches again */ | ||
right[offset] = ''; | ||
break; | ||
right[offset] = '' | ||
break | ||
} | ||
@@ -36,3 +38,3 @@ } | ||
return 2 * intersections / (left.length + rightLength); | ||
return (2 * intersections) / (leftLength + rightLength) | ||
} |
{ | ||
"name": "dice-coefficient", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Sørensen–Dice coefficient", | ||
@@ -12,3 +12,6 @@ "license": "MIT", | ||
"cli", | ||
"bin" | ||
"bin", | ||
"string", | ||
"comparison", | ||
"difference" | ||
], | ||
@@ -20,3 +23,4 @@ "homepage": "https://words.github.io/dice-coefficient/", | ||
"contributors": [ | ||
"Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)" | ||
"Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)", | ||
"Ben Meyrick <bameyrick@gmail.com>" | ||
], | ||
@@ -32,20 +36,20 @@ "bin": "cli.js", | ||
"devDependencies": { | ||
"browserify": "^14.0.0", | ||
"esmangle": "^1.0.0", | ||
"execa": "^0.8.0", | ||
"nyc": "^11.0.0", | ||
"remark-cli": "^4.0.0", | ||
"remark-preset-wooorm": "^3.0.0", | ||
"browserify": "^16.0.0", | ||
"execa": "^1.0.0", | ||
"nyc": "^13.0.0", | ||
"prettier": "^1.14.3", | ||
"remark-cli": "^6.0.0", | ||
"remark-preset-wooorm": "^4.0.0", | ||
"tape": "^4.4.0", | ||
"xo": "^0.18.0" | ||
"tinyify": "^2.4.3", | ||
"xo": "^0.23.0" | ||
}, | ||
"scripts": { | ||
"build-md": "remark . -qfo", | ||
"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", | ||
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix", | ||
"build-bundle": "browserify . -s diceCoefficient > dice-coefficient.js", | ||
"build-mangle": "browserify . -s diceCoefficient -p tinyify > dice-coefficient.min.js", | ||
"build": "npm run build-bundle && npm run build-mangle", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test": "npm run build && npm run lint && npm run test-coverage" | ||
"test": "npm run format && npm run build && npm run test-coverage" | ||
}, | ||
@@ -58,8 +62,13 @@ "nyc": { | ||
}, | ||
"prettier": { | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"singleQuote": true, | ||
"bracketSpacing": false, | ||
"semi": false, | ||
"trailingComma": "none" | ||
}, | ||
"xo": { | ||
"space": true, | ||
"prettier": true, | ||
"esnext": false, | ||
"rules": { | ||
"unicorn/explicit-length-check": "off" | ||
}, | ||
"ignores": [ | ||
@@ -66,0 +75,0 @@ "dice-coefficient.js" |
@@ -16,8 +16,8 @@ # dice-coefficient [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] | ||
```js | ||
var dice = require('dice-coefficient'); | ||
var dice = require('dice-coefficient') | ||
dice('abc', 'abc'); //=> 1 | ||
dice('abc', 'xyz'); //=> 0 | ||
dice('night', 'nacht'); //=> 0.25 | ||
dice('night', 'nacht') === dice('NiGhT', 'NACHT'); //=> true | ||
dice('abc', 'abc') // => 1 | ||
dice('abc', 'xyz') // => 0 | ||
dice('night', 'nacht') // => 0.25 | ||
dice('night', 'nacht') === dice('NiGhT', 'NACHT') // => true | ||
``` | ||
@@ -56,2 +56,13 @@ | ||
* [`levenshtein-edit-distance`](https://github.com/words/levenshtein-edit-distance) | ||
— Levenshtein edit distance | ||
* [`lancaster-stemmer`](https://github.com/words/lancaster-stemmer) | ||
— Lancaster stemming algorithm | ||
* [`double-metaphone`](https://github.com/words/double-metaphone) | ||
— Double Metaphone implementation | ||
* [`soundex-code`](https://github.com/words/soundex-code) | ||
— Fast Soundex implementation | ||
* [`syllable`](https://github.com/words/syllable) | ||
— Syllable count in an English word | ||
## License | ||
@@ -71,3 +82,3 @@ | ||
[license]: LICENSE | ||
[license]: license | ||
@@ -74,0 +85,0 @@ [author]: http://wooorm.com |
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
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
7252
85
9
84