# string-similarity-js

> Calculates similarity between two strings

Latest version **2.1.4** (published 2021-08-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install string-similarity-js
pnpm add string-similarity-js
yarn add string-similarity-js
bun add string-similarity-js
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.4 |
| Published | 2021-08-07 |
| First published | 2018-06-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 109 |
| Author | Stephen Brown |
| Maintainers | stephenjjbrown |
| Keywords | fuzzy search, string similarity, dice coefficient, dice's, sorenson-dice, fuzzy, string match, string, typos, misspell, misspelling, compare strings |

## Links

- npm: https://www.npmjs.com/package/string-similarity-js
- Repository: https://github.com/stephenjjbrown/string-similarity-js
- Homepage: https://github.com/stephenjjbrown/string-similarity-js#readme
- Issues: https://github.com/stephenjjbrown/string-similarity-js/issues
- npm.io page: https://npm.io/package/string-similarity-js

## Alternatives

- [@mce/gif](https://npm.io/package/@mce/gif.md) — 2.6K weekly downloads
- [cleanse](https://npm.io/package/cleanse.md) — 173 weekly downloads
- [str](https://npm.io/package/str.md) — 127 weekly downloads
- [naming](https://npm.io/package/naming.md) — 95 weekly downloads
- [tap-telco-api](https://npm.io/package/tap-telco-api.md) — 19 weekly downloads

## Recent versions

- 2.1.4 (latest) — 2021-08-07
- 2.1.3 — 2021-08-07
- 2.1.2 — 2019-02-17
- 2.1.1 — 2018-07-31
- 2.1.0 — 2018-07-20
- 2.0.1 — 2018-07-20
- 2.0.0 — 2018-07-20
- 1.2.0 — 2018-07-19
- 1.1.0 — 2018-06-24
- 1.0.1 — 2018-06-21
- 1.0.0 — 2018-06-21

## README

[![Build Status](https://travis-ci.org/stephenjjbrown/string-similarity-js.svg?branch=master)](https://travis-ci.org/stephenjjbrown/string-similarity-js)
[![codecov](https://codecov.io/gh/stephenjjbrown/string-similarity-js/branch/master/graph/badge.svg)](https://codecov.io/gh/stephenjjbrown/string-similarity-js)
[![Wallaby.js](https://img.shields.io/badge/wallaby.js-configured-green.svg)](https://wallabyjs.com)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/1f8dbc1fcb584d818c21869f4742f936)](https://www.codacy.com/gh/stephenjjbrown/string-similarity-js/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=stephenjjbrown/string-similarity-js&amp;utm_campaign=Badge_Grade)

# String Similarity

A simple, lightweight (~700 bytes minified) string similarity function based on comparing the number of bigrams in common between any two strings. Returns a score between 0 and 1 indicating the strength of the match.

Based on the [Sørensen–Dice coefficient](https://en.wikipedia.org/wiki/Sørensen–Dice_coefficient), this algorithm is most effective at detecting rearranged words or misspellings. It tends to be less effective with very short strings, unless perhaps you switch to comparing individual characters in common instead of bigrams.

It is case insensitive unless you specify otherwise. Does not ignore punctuation or spaces. In some cases, removing punctuation beforehand may improve accuracy.

### Update
Version 2.0 optimizes the algorithm from O(n<sup>2</sup>) time complexity to O(n), and switches from using an array for bigrams to a Map, which was found to be substantially faster in performance tests.

## Usage

### Requirements
This library uses built-in [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) data structure for optimal performance. Therefore, it requires at least IE11 or a polyfill for Map.

### Examples

```typescript
import { stringSimilarity } from "string-similarity-js";

// Rearranged words
stringSimilarity("Lorem ipsum", "Ipsum lorem")
// Returns a score of 0.9

// Typos
stringSimilarity("The quick brown fox jumps over the lazy dog", "The quck brown fx jumps over the lazy dog")
// 0.92

// Even more different
stringSimilarity("The quick brown fox jumps over the lazy dog", "The quack brain fax jomps odor the lady frog")
// 0.65

// Completely different strings
stringSimilarity("The quick brown fox jumps over the lazy dog", "Lorem ipsum")
// 0.07

// Tiny strings are less effective with default settings
stringSimilarity("DMV", "DNV")
// Returns 0, because technically there are no bigrams in common between the two

// Passing in a substring length of 1 may improve accuracy on tiny strings
stringSimilarity("DMV", "DNV", 1)
// Returns 0.67, the percentage of letters in common between the two
```

## License

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details

---
_Source: https://npm.io/package/string-similarity-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
