Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
schulzch committed Nov 28, 2016
1 parent eef8fc8 commit a3c55c5
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,48 @@

## Usage

[Node.js](https://nodejs.org/) and [Require.js](http://requirejs.org/) version:

```javascript
var distance, insert, remove, update, rootA, rootB, children, stringA, stringB;
var ed = require('edit-distance');
//Browserify version only, without module loader:
//var ed = global.editDistance;
```

distance = require('edit-distance');

insert = remove = function(node) { return 1; };
### Levenshtein Distance

stringA = "abcdef";
stringB = "abdfgh";
```javascript
// Define cost functions.
var insert, remove, update;
insert = remove = function(node) { return 1; };
update = function(stringA, stringB) { return stringA !== stringB ? 1 : 0; };
console.log(distance.lev(stringA, stringB, insert, remove, update));

rootA = {id: 1, children: [{id: 2}, {id: 3}]};
rootB = {id: 1, children: [{id: 4}, {id: 3}, {id: 5}]};
children = function(node) { return node.children; };
// Define two strings.
var stringA = "abcdef";
var stringB = "abdfgh";

// Compute edit distance and alignment.
var lev = ed.levenshtein(stringA, stringB, insert, remove, update);
console.log('Levenshtein', lev.distance, lev.alignment());
```

### Tree Edit Distance

```javascript
// Define cost functions.
var insert, remove, update;
insert = remove = function(node) { return 1; };
update = function(nodeA, nodeB) { return nodeA.id !== nodeB.id ? 1 : 0; };
console.log(distance.ted(rootA, rootB, children, insert, remove, update));

// Define two trees.
var rootA = {id: 1, children: [{id: 2}, {id: 3}]};
var rootB = {id: 1, children: [{id: 4}, {id: 3}, {id: 5}]};
var children = function(node) { return node.children; };

// Compute edit distance and alignment.
var ted = ed.ted(rootA, rootB, children, insert, remove, update);
console.log('Tree Edit Distance', ted.distance, ted.alignment());
```

## References
Expand Down

0 comments on commit a3c55c5

Please sign in to comment.