Coder Social home page Coder Social logo

treeeditdistance's Introduction

Tree Edit Distance

Summary

TreeEditDistance is a java module that can be used to compute the tree edit distance between two ordered labeled trees.

It is strongly inspired by Tim Henderson (timtadh) and Steve Johnson (irskep) excellent zhang-shasha python library (https://github.com/timtadh/zhang-shasha).

Installation

Just use it in jar format as a library? It has no external dependencies.

How It Works

Most of the code you need is in the ted.convenience.TED class. It wraps calls to constructors and distance functions for hopefully the most common tasks.

// initialize the trees to compare
StringTree tree1 = new StringTree("{f{d{a}{c{b}}}{e}}");
StringTree tree2 = new StringTree("{f{c{d{a}{b}}}{e}}");

// using the unit cost function
// (indel = 1, substitution = 2 if labels are not equal, 0 otherwise)
TED.computeDistance(tree1, tree2);

If more fancy cost functions are needed, their interface is described in the ted.core.interfaces. Just define them and build a new tree edit distance object. A small example of this is done in the ted.convenience.TED class where simple cost functions are defined and tree edit distance is built from these.

// create a cost function for insertions and deletions
// they all cost 1 for this example
static class UnitCost implements CostFunction<String> {

    @Override
    public double getCost(String label) {
        return 1;
    }
}

// create a cost function for label substitution (relabeling)
static class EqualDistanceCost implements DistanceFunction<String, String> {

    @Override
    public double getDistance(String label1, String label2) {
        // to be used with unit cost, a substitution becomes equivalent to
        // a delete and insert...
        return label1.compareTo(label2) == 0 ? 0. : 2.;
    }
}

// instantiate a new tree edit distance object
CostFunction<String> indelCost = new UnitCost();
DistanceFunction<String, String> substitutionCost = new EqualDistanceCost();
TreeEditDistance<String> distance = new TreeEditDistance<>(indelCost,
                                                           indelCost,
                                                           substitutionCost);

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.