Skip to content

refactor: upgrade and rewrite #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/node_modules/**
dist
37 changes: 0 additions & 37 deletions .eslintrc.js

This file was deleted.

42 changes: 42 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"parser": "@typescript-eslint/parser",
"extends": ["alloy", "alloy/typescript", "plugin:prettier/recommended"],
"plugins": ["@typescript-eslint", "prettier"],
"env": {
"browser": true,
"node": true,
"commonjs": true,
"es6": true
},
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2022
},
"root": true,
"rules": {
"no-duplicate-imports": 0,
"for-direction": "error",
"getter-return": [
"error",
{
"allowImplicit": false
}
],
"no-await-in-loop": "off",
"no-compare-neg-zero": "error",
"no-cond-assign": ["error", "except-parens"],
"no-console": "off",
"no-constant-condition": [
"error",
{
"checkLoops": false
}
],
"no-control-regex": "error",
"no-debugger": "error",
"no-dupe-args": "error",
"no-dupe-keys": "error",
"max-nested-callbacks": ["error", 5],
"@typescript-eslint/consistent-type-definitions": ["error", "interface"]
}
}
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
node_modules
.vscode

package-lock.json

yarn.lock
pnpm-lock.yaml

lib

dist
dist
5 changes: 0 additions & 5 deletions .mocharc.js

This file was deleted.

5 changes: 5 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extension": ["ts"],
"spec": "test/*.test.ts",
"require": "ts-node/register"
}
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
.vscode
yaml
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"[typescript]": {
"editor.formatOnSave": true
},
"typescript.tsdk": "node_modules/typescript/lib",
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
File renamed without changes.
File renamed without changes.
52 changes: 32 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

A library implementing different string similarity, distance and sortMatch measures. A dozen of algorithms (including Levenshtein edit distance and sibblings, Longest Common Subsequence, cosine similarity etc.) are currently implemented. Check the summary table below for the complete list...

- [Download & Usage](#download--usage)
- [string-comparison](#string-comparison)
- [Download \& Usage](#download--usage)
- [OverView](#overview)
- [Normalized, metric, similarity and distance](#normalized-metric-similarity-and-distance)
- [(Normalized) similarity and distance](#normalized-similarity-and-distance)
Expand All @@ -28,8 +29,7 @@ A library implementing different string similarity, distance and sortMatch measu
- [sortMatch](#sortmatch)
- [params](#params-2)
- [return](#return-2)
- [Params](#params)
- [Return](#return)
- [CHANGELOG](#changelog)
- [MIT](#mit)


Expand All @@ -40,19 +40,20 @@ download
```shell
npm install string-comparison --save
yarn add string-comparison
pnpm add string-comparison
```
usage

```js
let stringComparison = require('string-comparison')
// or import stringComparison frrom 'string-comparison'
// or import stringComparison from 'string-comparison'

const Thanos = 'healed'
const Rival = 'sealed'
const Avengers = ['edward', 'sealed', 'theatre']

// use by cosine
let cos = stringComparison.cosine
let cos = stringComparison.Cosine

console.log(cos.similarity(Thanos, Rival))
console.log(cos.distance(Thanos, Rival))
Expand Down Expand Up @@ -90,14 +91,15 @@ The Levenshtein distance between two words is the minimum number of single-chara
It is a metric string distance. This implementation uses dynamic programming (Wagner–Fischer algorithm), with only 2 rows of data. The space requirement is thus O(m) and the algorithm runs in O(m.n).

```js
import { Levenshtein } from "string-comparison"

const Thanos = 'healed'
const Rival = 'sealed'
const Avengers = ['edward', 'sealed', 'theatre']
let ls = Similarity.levenshtein

console.log(ls.similarity(Thanos, Rival))
console.log(ls.distance(Thanos, Rival))
console.log(ls.sortMatch(Thanos, Avengers))
console.log(Levenshtein.similarity(Thanos, Rival))
console.log(Levenshtein.distance(Thanos, Rival))
console.log(Levenshtein.sortMatch(Thanos, Avengers))

// output
0.8333333333333334
Expand Down Expand Up @@ -127,14 +129,15 @@ This class implements the dynamic programming approach, which has a space requir
In "Length of Maximal Common Subsequences", K.S. Larsen proposed an algorithm that computes the length of LCS in time O(log(m).log(n)). But the algorithm has a memory requirement O(m.n²) and was thus not implemented here.

```js
import { LongestCommonSubsequence } from "string-comparison"

const Thanos = 'healed'
const Rival = 'sealed'
const Avengers = ['edward', 'sealed', 'theatre']
let lcs = Similarity.lcs

console.log(lcs.similarity(Thanos, Rival))
console.log(lcs.distance(Thanos, Rival))
console.log(lcs.sortMatch(Thanos, Avengers))
console.log(LongestCommonSubsequence.similarity(Thanos, Rival))
console.log(LongestCommonSubsequence.distance(Thanos, Rival))
console.log(LongestCommonSubsequence.sortMatch(Thanos, Avengers))

// output
0.8333333333333334
Expand All @@ -154,14 +157,15 @@ http://heim.ifi.uio.no/~danielry/StringMetric.pdf
The distance is computed as 1 - |LCS(s1, s2)| / max(|s1|, |s2|)

```js
import { MetricLCS } from "string-comparison"

const Thanos = 'healed'
const Rival = 'sealed'
const Avengers = ['edward', 'sealed', 'theatre']
let mlcs = Similarity.mlcs

console.log(mlcs.similarity(Thanos, Rival))
console.log(mlcs.distance(Thanos, Rival))
console.log(mlcs.sortMatch(Thanos, Avengers))
console.log(MetricLCS.similarity(Thanos, Rival))
console.log(MetricLCS.distance(Thanos, Rival))
console.log(MetricLCS.sortMatch(Thanos, Avengers))

// output
0.8333333333333334
Expand All @@ -180,12 +184,20 @@ Like Q-Gram distance, the input strings are first converted into sets of n-grams
Distance is computed as 1 - similarity.
Jaccard index is a metric distance.

```js
import { Cosine } from "string-comparison"
```

## Sorensen-Dice coefficient

Similar to Jaccard index, but this time the similarity is computed as 2 * |V1 inter V2| / (|V1| + |V2|).

Distance is computed as 1 - similarity.

```js
import { DiceCoefficient } from "string-comparison"
```

## API
* `similarity`.
* `distance`.
Expand Down Expand Up @@ -236,9 +248,9 @@ Return an array of objects. ex:
]
```

## CHANGLOG
[CHANGLOG](./CHANGLOG.md)
## CHANGELOG
[CHANGELOG](./CHANGELOG.md)


## MIT
[MIT](./LICENCE)
[MIT](./LICENSE)
123 changes: 67 additions & 56 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,59 +1,70 @@
{
"name": "string-comparison",
"version": "1.1.0",
"description": "A library implementing different string similarity",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"scripts": {
"test": "mocha",
"lint": "eslint src",
"build-fast": "tsup src/index.ts --dts --format cjs,esm --minify",
"build": "yarn build-fast",
"watch": "tsup src/index.ts --watch"
},
"repository": {
"type": "git",
"url": "https://github.com/Rabbitzzc/js-string-comparision"
},
"files": [
"dist"
],
"keywords": [
"strings",
"compare similarity",
"similarity",
"Dice's Coefficient",
"Cosine",
"Jaccard Index",
"Levenshtein",
"Longest Common Subsequence",
"Metric Longest Common Subsequence",
"difference",
"compare",
"comparision",
"similar",
"distance",
"match",
"sort match"
],
"author": {
"name": "Rabbitzzc",
"email": "zzclovelcs@gmail.com"
},
"license": "MIT",
"devDependencies": {
"@types/mocha": "^9.1.0",
"@types/node": "^17.0.18",
"@typescript-eslint/eslint-plugin": "^5.12.0",
"@typescript-eslint/parser": "^5.12.0",
"async": "^3.1.0",
"eslint": "^8.9.0",
"eslint-config-alloy": "^4.5.1",
"mocha": "^6.2.1",
"npm-run-all": "^4.1.5",
"ts-node": "^10.5.0",
"tsup": "^5.11.13",
"typescript": "^4.5.5"
"name": "string-comparison",
"version": "1.1.0",
"description": "A library implementing different string similarity",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
}
},
"scripts": {
"test": "mocha",
"lint": "eslint -c .eslintrc.json \"src/**/*.ts\"",
"lint:fix": "eslint -c .eslintrc.json \"src/**/*.ts\" --fix",
"build": "tsup src/index.ts --dts --format cjs,esm --minify",
"watch": "tsup src/index.ts --watch"
},
"repository": {
"type": "git",
"url": "https://github.com/Rabbitzzc/js-string-comparision"
},
"files": [
"dist"
],
"keywords": [
"strings",
"compare similarity",
"similarity",
"Dice's Coefficient",
"Cosine",
"Jaccard Index",
"Levenshtein",
"Longest Common Subsequence",
"Metric Longest Common Subsequence",
"difference",
"compare",
"comparision",
"similar",
"distance",
"match",
"sort match"
],
"author": {
"name": "Rabbitzzc",
"email": "zzclovelcs@gmail.com"
},
"license": "MIT",
"devDependencies": {
"@swc/core": "^1.3.76",
"@types/mocha": "^10.0.1",
"@types/node": "^20.5.0",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"@typescript-eslint/parser": "^6.3.0",
"async": "^3.2.4",
"eslint": "^8.47.0",
"eslint-config-alloy": "^5.1.1",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"mocha": "^10.2.0",
"npm-run-all": "^4.1.5",
"prettier": "^3.0.1",
"ts-node": "^10.9.1",
"tsup": "^7.2.0",
"typescript": "^5.1.6"
}
}
Loading