Giter Club home page Giter Club logo

dice-coefficient's Introduction

dice-coefficient

Build Coverage Downloads Size

Sørensen–Dice coefficient.

Contents

What is this?

This package exposes a string similarity algorithm. That means it gets two strings (typically words), and turns it into a number between 0 (completely different) and 1 (exactly the same).

When should I use this?

You’re probably dealing with natural language, and know you need this, if you’re here!

Install

This package is ESM only. In Node.js (version 14.14+, 16.0+), install with npm:

npm install dice-coefficient

In Deno with esm.sh:

import {diceCoefficient} from 'https://esm.sh/dice-coefficient@2'

In browsers with esm.sh:

<script type="module">
  import {diceCoefficient} from 'https://esm.sh/dice-coefficient@2?bundle'
</script>

Use

import {diceCoefficient} from 'dice-coefficient'

diceCoefficient('abc', 'abc') // => 1
diceCoefficient('abc', 'xyz') // => 0
diceCoefficient('night', 'nacht') // => 0.25
diceCoefficient('night', 'nacht') === dice('NiGhT', 'NACHT') // => true

API

This package exports the identifier diceCoefficient. There is no default export.

diceCoefficient(value, other)

Get the difference according to Sørensen–Dice.

👉 Note: you can pass bigrams (from n-gram) too, which will improve performance when you are comparing the same values multiple times.

value

Primary value (string, Array<String>, required).

other

Other value (string, Array<String>, required).

Returns

Difference (number).

The result is normalized to a number between 0 (completely different) and 1 (exactly the same).

CLI

Usage: dice-coefficient [options] <word> <word>

Sørensen–Dice coefficient

Options:

  -h, --help           output usage information
  -v, --version        output version number

Usage:

# output edit distance
$ dice-coefficient night nacht
# 0.25

# output edit distance from stdin
$ echo "saturday sunday" | dice-coefficient
# 0.3333333333333333

Types

This package is fully typed with TypeScript. It exports no additional types.

Compatibility

This package is at least compatible with all maintained versions of Node.js. As of now, that is Node.js 14.14+ and 16.0+. It also works in Deno and modern browsers.

Related

Contribute

Yes please! See How to Contribute to Open Source.

Security

This package is safe.

License

MIT © Titus Wormer

dice-coefficient's People

Contributors

bameyrick avatar greenkeeperio-bot avatar khxu avatar wooorm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

dice-coefficient's Issues

Avoid "bigram-ifying" if an input is an array?

First, thanks for the awesome work on this project!

Second, I didn't see a previous issue on this so thought I'd ask -- would there be interest in detecting whether the inputs to the diceCoefficient function have already been "bigram-ified"? I was working on some analysis that involved comparing the Dice coefficients between the strings of two large batches of text files, and refactoring to skip "bigram-ifying" if the input argument was an array significantly sped things up, as I could "bigram-ify" all strings once upfront and that cost would be amortized across all subsequent comparisons.

Happy to submit a PR if that'd help.

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:

Maybe this is just a webpack 4 problem, but I'm removing to use the natural package, even being not so good on the benchmark.

I was using typescript.

Tested in nodes:
v10.21.0
v12.22.1
v14.17.0

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/julio/projects/node_modules/dice-coefficient/index.js
require() of ES modules is not supported.
require() of /home/julio/projects/node_modules/dice-coefficient/index.js from /home/julio/projects/.next/server/pages/_document.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/julio/projects/node_modules/dice-coefficient/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1015:13)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at eval (webpack-internal:///dice-coefficient:1:18)
    at Object.dice-coefficient (/home/julio/projects/.next/server/pages/_document.js:547:1)
    at __webpack_require__ (/home/julio/projects/.next/server/pages/_document.js:23:31)
    at eval (webpack-internal:///./src/factory/TextFactory/index.ts:3:74)
    at Module../src/factory/TextFactory/index.ts (/home/julio/projects/.next/server/pages/_document.js:356:1)
    at __webpack_require__ (/home/julio/projects/.next/server/pages/_document.js:23:31)
    at eval (webpack-internal:///./src/factory/index.ts:8:70)
    at Module../src/factory/index.ts (/home/julio/projects/.next/server/pages/_document.js:368:1)
    at __webpack_require__ (/home/julio/projects/.next/server/pages/_document.js:23:31)
    at eval (webpack-internal:///./src/provider/TextProvider/index.ts:3:66)
    at Module../src/provider/TextProvider/index.ts (/home/julio/projects/.next/server/pages/_document.js:452:1) {
  code: 'ERR_REQUIRE_ESM'
}

Slice is always defined

Your line:

    source = value.slice ? value : String(value)

is probably not doing what you mean it to?

> "foo".slice
[Function: slice]

> ["foo"].slice
[Function: slice]

> "foo".slice === ["foo"].slice
false

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.