
Product
Introducing Rust Support in Socket
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
hermetrics
Advanced tools
Javascript library for distance and similarity metrics. Javascript translation from hermetrics.py.
From npm
$ npm i hermetrics --save
Require the package and import the desired class:
const { Levenshtein } = require('hermetrics');
const levenshtein = new Levenshtein();
levenshtein.distance('start', 'end');
levenshtein.maxDistance('start', 'end');
Using custom operation costs:
const { Levenshtein } = require('hermetrics');
const levenshtein = new Levenshtein();
const opts = {
deletionCost: 3,
substitutionCost: 2,
deletionCost: 5
};
levenshtein.distance('start', 'end', opts);
levenshtein.maxDistance('start', 'end', opts);
Hermetrics is a library designed for use in experimentation with string metrics. The library features a base class Metric which is highly configurable and can be used to implement custom metrics.
Based on Metric are some common string metrics already implemented to compute the distance between two strings. Some common edit distance metrics such as Levenshtein can be parametrized with different costs for each edit operation, althought have been only thoroughly tested with costs equal to 1. Also, the implemented metrics can be used to compare any iterable in addition to strings, but more tests are needed.
A metric has three main methods distance, normalizeDistance and similarity. In general the distance method computes the absolute distance between two strings, whereas normalizeDistance can be used to scale the distance to a particular range, usually (0,1), and the similarity method being normally defined as (1-normalizeDistance).
The normalization of the distance can be customized overriding the auxiliary methods for its computation. Those methods are maxDistance, minDistance and normalize.
Metric is a base class that can receive as arguments an metric name, and contains six specific functions to be used as methods for the metric being implemented.
Description of default methods for the Metric class.
In general a method of a metric receives three parameters:
Method | Description |
---|---|
Distance | The distance method computes the total cost of transforming the source string on the target string. The default method just return 0 if the strings are equal and 1 otherwise. |
maxDistance | Returns the maximum value of the distance between source and target given a specific cost for edit operations. The default method just return 1 given source and target don't have both length=0, in that case just return 0. |
minDistance | work in progress |
normalize | work in progress |
normalized distance | work in progress |
similarity | work in progress |
Levenshtein distance is usually known as "the" edit distance. It is defined as the minimum number of edit operations (deletion, insertion and substitution) to transform the source string into the target string. The algorithm for distance computation is implemented using the dynamic programming approach with the full matrix construction, althought there are optimizations for time and space complexity those are not implemented here.
FAQs
Javascript version of hermetrics.py
The npm package hermetrics receives a total of 1,007 weekly downloads. As such, hermetrics popularity was classified as popular.
We found that hermetrics demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
Product
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.
Product
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.