0.0.20 • Published 21 days ago

string-comparisons v0.0.20

Weekly downloads
-
License
MIT
Repository
-
Last release
21 days ago

String Comparisons

npm GitHub stars GitHub license example workflow

This library offers a range of functions to calculate text similarity, allowing you to measure the likeness of text data in an application. It implements well-established similarity metrics. The library currently supports the following algorithms:

  • Cosine Similarity
  • Jaccard Similarity
  • Jaro Similarity
  • Damerau-Levenshtein Distance
  • Hamming Distance
  • Levenshtein Distance
  • Smith-Waterman Alignment
  • Sørensen-Dice Coefficient
  • Jaccard Similarity based on Trigrams
  • Szymkiewicz Simpson Overlap
  • N-Gram
  • Q-Gram
  • Optimal String Alignment

Installation

Assuming you have Node.js and npm/yarn/pnpm installed, install the library using:

# Install the 'string-comparisons' package using npm
npm install string-comparisons

# Alternatively, install the 'string-comparisons' package using yarn
yarn add string-comparisons

# Or, install the 'string-comparisons' package using pnpm
pnpm add string-comparisons

Docs

Find more information on the algorithms by accessing the class documentation of each implemented algorithm.

String Similarity Algorithm Comparison

AlgorithmNormalizedMetricSimilarityDistanceSpace Complexity
cosine.jsYesVector Space ModelO(n)
jaro.jsNoEdit DistanceO(min(n, m))
jaccard.jsNoSet TheoryO(min(n, m))
damerauLevenshtein.jsNoEdit DistanceO(max(n, m)²)
hammingDistance.jsNoBitwise OperationsO(1)
jaroWinkler.jsNoEdit DistanceO(min(n, m))
levenshtein.jsNoEdit DistanceO(max(n, m)²)
smithWaterman.jsNoDynamic Programming (Local Alignment)O(n * m)
sorensenDice.jsNoSet TheoryO(min(n, m))
trigram.jsNoN-gram OverlapO(n²)
szymkiewiczSimpsonOverlap.jsYesOverlap CoefficientO(min(m, n))
nGram.jsYesJaccard similarity coefficientO(m * n)
qGram.jsYesJaccard similarity coefficientO(n + m)
optimalStringAlignment.jsNoEdit distanceO(max(n, m)²)

Explanation of Columns:

  • Normalized: Indicates whether the algorithm produces a score between 0 and 1 (normalized).
  • Metric: The underlying mathematical concept used for comparison.
  • Similarity: Whether the algorithm outputs a higher score for more similar strings.
  • Distance: Whether the algorithm outputs a lower score for more similar strings. (One algorithm might use similarity, another distance - they provide the opposite information).
  • Space Complexity: The amount of extra memory the algorithm needs to run the comparison.

Notes:

  • ✓ indicates the algorithm applies to that category.
  • Some algorithms can be used for both similarity and distance calculations depending on the interpretation of the score.

Example Usage

import StringComparisons from 'string-comparisons';

const { Cosine, Jaccard, Jaro, DamerauLevenshtein, HammingDistance, JaroWrinker, Levenshtein, SmithWaterman, SorensenDice, Trigram } = StringComparisons;

const string1 = 'programming';
const string2 = 'programmer';


console.log('Jaro-Winkler similarity:', JaroWrinker.similarity(string1, string2)); // Output: ~0.9054545454545454
console.log('Levenshtein distance:', Levenshtein.similarity(string1, string2)); // Output: 3
console.log('Smith-Waterman similarity:', SmithWaterman.similarity(string1, string2)); // Output: 16

const set1 = new Set([1, 2, 3]);
const set2 = new Set([2, 3, 4]);

console.log('Sørensen-Dice similarity:', SorensenDice.similarity(set1, set2)); // Output: 0.6666666666666667

const trigram1 = 'hello';
const trigram2 = 'world';

console.log('Trigram Jaccard similarity:', Trigram.similarity(trigram1, trigram2)); // Output: 0 (no shared trigrams)

// so on

Contributing

We encourage contributions to this library! Feel free to fork the repository, make your changes, and submit pull requests.

Support the Project

If you feel awesome and want to support us in a small way, please consider starring and sharing the repo! This helps us get visibility and allow the community to grow. 🙏

Contact Us

If you have any questions or feedback, please don't hesitate to contact us at sumn2u@gmail.com, or reach out to Suman directly. We hope you find this resource helpful 💜.

License Information

This project is licensed under the MIT , which means that you are free to use, modify, and distribute the code as long as you comply with the terms of the license.

Resources