Giter Site home page Giter Site logo

custom-edit-distance's Introduction

Custom Edit Distance1

The usual algorithm2 with customizable costs.

npm version Build Status

Install

npm install custom-edit-distance

Calculate

const defaultCalculator = new CustomEditDistance();
defaultCalculator.editDistance('sitting', 'kitten').should.equal(3);

Customize Costs

const LEVENSHTEIN = {
  getKeepCost(unchangedValue) {
    return 0;
  },
  getInsertCost(insertedValue) {
    return 1;
  },
  getRemoveCost(removedValue) {
    return 1;
  },
  getSubstituteCost(fromValue, toValue) {
    return 1;
  },
};
const defaultCalculator = new CustomEditDistance(LEVENSHTEIN);

For convenience, we default to the Levenshtein3 distance costs. We also export it as LEVENSHTEIN.

Customize Equivalence

const equivalence = function(a, b) {
  a == b; // Only check for abstract equivalence
};

const defaultCalculator = new CustomEditDistance(null, equivalence);

For convenience we default to strict equality (===).

Sequences don't have to be Strings

const defaultCalculator = new CustomEditDistance();
defaultCalculator.editDistance([1, 2, 3], [1, 4, 3]).should.equal(1);

This is where defining cost can shine:

const valueDistanceCalculator = new CustomEditDistance({
  getInsertCost(insertedValue) {
    return insertedValue;
  },
  getRemoveCost(removedValue) {
    return removedValue;
  },
  getSubstituteCost(fromValue, toValue) {
    return Math.abs(toValue - fromValue);
  },
});
valueDistanceCalculator.editDistance([2, 3, 4], [1, 2, 3]).should.equal(3);

Get the cost matrix

const defaultCalculator = new CustomEditDistance();
const costMatrix = defaultCalculator.editCosts('sitting', 'kitten').should.equal(3);

From this you could determine the edit path, or inspect the cost of other edit paths.

Reference

Edit Distance

The Wagner-Fischer Algorithm

Levenshtein Distance

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.