cmpstr 1.0.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +430 -85
- package/package.json +47 -25
- package/src/CmpStr.js +784 -0
- package/src/CmpStrAsync.js +191 -0
- package/src/algorithms/cosine.js +86 -0
- package/src/algorithms/damerau.js +78 -0
- package/src/algorithms/dice.js +65 -0
- package/src/algorithms/hamming.js +44 -0
- package/src/algorithms/jaccard.js +34 -0
- package/src/algorithms/jaroWinkler.js +106 -0
- package/src/algorithms/lcs.js +58 -0
- package/src/algorithms/levenshtein.js +70 -0
- package/src/algorithms/needlemanWunsch.js +72 -0
- package/src/algorithms/qGram.js +63 -0
- package/src/algorithms/smithWaterman.js +78 -0
- package/src/algorithms/soundex.js +152 -0
- package/src/index.js +47 -0
- package/index.js +0 -432
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,138 +1,483 @@
|
|
|
1
|
-
#
|
|
1
|
+
# CmpStr `v2.0`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
CmpStr is a lightweight and powerful npm package for calculating string similarity, finding the closest matches in arrays, performing phonetic searches, and more. It supports a variety of built-in algorithms (e.g., Levenshtein, Dice-Sørensen, Damerau-Levenshtein, Soundex) and allows users to add custom algorithms and normalization filters.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
#### Key Features
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- Built-in support for multiple similarity algorithms.
|
|
8
|
+
- Phonetic search with language-specific configurations (e.g., Soundex).
|
|
9
|
+
- Batch operations and similarity matrices for large datasets.
|
|
10
|
+
- Customizable normalization with global flags and caching.
|
|
11
|
+
- Asynchronous support for non-blocking workflows.
|
|
12
|
+
- Extensible with custom algorithms and filters.
|
|
8
13
|
|
|
9
|
-
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
Install the package via npm:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
10
19
|
npm install cmpstr
|
|
11
20
|
```
|
|
12
21
|
|
|
13
|
-
## Usage
|
|
22
|
+
## Basic Usage
|
|
14
23
|
|
|
15
|
-
|
|
24
|
+
Importing the Package:
|
|
16
25
|
|
|
17
26
|
```js
|
|
18
|
-
const
|
|
27
|
+
const { CmpStr } = require( 'cmpstr' );
|
|
19
28
|
```
|
|
20
29
|
|
|
21
|
-
|
|
30
|
+
Example 1: Basic String Similarity
|
|
22
31
|
|
|
23
32
|
```js
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
* levenshteinDistance
|
|
29
|
-
* expected: 3
|
|
30
|
-
*/
|
|
31
|
-
let distance = cmpstr.levenshteinDistance( str1, str2 );
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* diceCoefficient
|
|
35
|
-
* expected: 0.3636363636363636
|
|
36
|
-
*/
|
|
37
|
-
let dice = cmpstr.diceCoefficient( str1, str2 );
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* diceClosest
|
|
41
|
-
* expected: bestest
|
|
42
|
-
*/
|
|
43
|
-
let closest = cmpstr.diceClosest( 'best', [
|
|
44
|
-
'better', 'bestest', 'well', 'good'
|
|
45
|
-
] );
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* levenshteinMatch
|
|
49
|
-
* expected: [
|
|
50
|
-
* { target: 'bestest', match: 0.5714285714285714 },
|
|
51
|
-
* { target: 'better', match: 0.5 },
|
|
52
|
-
* { target: 'well', match: 0.25 },
|
|
53
|
-
* { target: 'good', match: 0 }
|
|
54
|
-
* ]
|
|
55
|
-
*/
|
|
56
|
-
let matches = cmpstr.levenshteinMatch( 'best', [
|
|
57
|
-
'better', 'bestest', 'well', 'good'
|
|
58
|
-
] );
|
|
33
|
+
const cmp = new CmpStr( 'levenshtein', 'hello' );
|
|
34
|
+
|
|
35
|
+
console.log( cmp.test( 'Hallo', { flags: 'i' } ) );
|
|
36
|
+
// Output: 0.8
|
|
59
37
|
```
|
|
60
38
|
|
|
61
|
-
|
|
39
|
+
Example 2: Phonetic Search
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
const cmp = new CmpStr( 'soundex', 'Robert' );
|
|
43
|
+
|
|
44
|
+
console.log( cmp.test( 'Rubin', { options: { raw: true } } ) );
|
|
45
|
+
// Output: { a: 'R163', b: 'R150' }
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Methods
|
|
49
|
+
|
|
50
|
+
Creating a new instance of `CmpStr` or `CmpStrAsync` allows passing the algorithm to be used and the base string as optional arguments. Alternatively or later in the process, the `setAlgo` and `setStr` methods can be used for this purpose.
|
|
51
|
+
|
|
52
|
+
### Basics
|
|
53
|
+
|
|
54
|
+
#### `isReady()`
|
|
55
|
+
|
|
56
|
+
Checks whether string and algorithm are set correctly. Returns `true`, if the class is ready to perform similarity checks, false otherwise.
|
|
57
|
+
|
|
58
|
+
#### `setStr( str )`
|
|
59
|
+
|
|
60
|
+
Sets the base string for comparison.
|
|
61
|
+
|
|
62
|
+
Parameters:
|
|
63
|
+
|
|
64
|
+
- `<String> str` – string to set as the base
|
|
65
|
+
|
|
66
|
+
#### `setFlags( [ flags = '' ] )`
|
|
67
|
+
|
|
68
|
+
Set default normalization flags. They will be overwritten by passing `flags` through the configuration object. See description of available flags / normalization options below in the documentation.
|
|
69
|
+
|
|
70
|
+
Parameters:
|
|
71
|
+
|
|
72
|
+
- `<String> flags` – normalization flags
|
|
73
|
+
|
|
74
|
+
#### `clearCache()`
|
|
75
|
+
|
|
76
|
+
Clears the normalization cache.
|
|
62
77
|
|
|
63
|
-
|
|
78
|
+
### Algorithms
|
|
79
|
+
|
|
80
|
+
#### `listAlgo()`
|
|
81
|
+
|
|
82
|
+
List all registered similarity algorithms.
|
|
83
|
+
|
|
84
|
+
#### `isAlgo( algo )`
|
|
85
|
+
|
|
86
|
+
Checks if an algorithm is registered. Returns `true` if so, `false` otherwise.
|
|
87
|
+
|
|
88
|
+
Parameters:
|
|
89
|
+
|
|
90
|
+
- `<String> algo` – name of the algorithm
|
|
91
|
+
|
|
92
|
+
#### `setAlgo( algo )`
|
|
93
|
+
|
|
94
|
+
Sets the current algorithm to use for similarity calculations.
|
|
95
|
+
|
|
96
|
+
Allowed options for build-in althorithms are `cosine`, `damerau`, `dice`, `hamming`, `jaccard`, `jaro`, `lcs`, `levenshtein`, `needlemanWunsch`, `qGram`, `smithWaterman` and `soundex`.
|
|
97
|
+
|
|
98
|
+
Parameters:
|
|
99
|
+
|
|
100
|
+
- `<String> algo` – name of the algorithm
|
|
101
|
+
|
|
102
|
+
#### `addAlgo( algo, callback [, useIt = true ] )`
|
|
103
|
+
|
|
104
|
+
Adding a new similarity algorithm by using the `addAlgo()` method passing the name and a callback function, that must accept at least two strings and return a number. If `useIt` is `true`, the new algorithm will automatically be set as the current one.
|
|
105
|
+
|
|
106
|
+
Parameters:
|
|
107
|
+
|
|
108
|
+
- `<String> algo` – name of the algorithm
|
|
109
|
+
- `<Function> callback` – callback function implementing the algorithm
|
|
110
|
+
- `<Boolean> useIt` – whether to set this algorithm as the current one
|
|
111
|
+
|
|
112
|
+
Example:
|
|
64
113
|
|
|
65
114
|
```js
|
|
66
|
-
|
|
115
|
+
const cmp = new CmpStr();
|
|
116
|
+
|
|
117
|
+
cmp.addAlgo( 'customAlgo', ( a, b ) => {
|
|
118
|
+
return a === b ? 1 : 0;
|
|
119
|
+
} );
|
|
120
|
+
|
|
121
|
+
console.log( cmp.compare( 'customAlgo', 'hello', 'hello' ) );
|
|
122
|
+
// Output: 1
|
|
67
123
|
```
|
|
68
124
|
|
|
69
|
-
|
|
125
|
+
#### `rmvAlgo( algo )`
|
|
126
|
+
|
|
127
|
+
Removing a registered similarity algorithm.
|
|
128
|
+
|
|
129
|
+
Parameters:
|
|
130
|
+
|
|
131
|
+
- `<String> algo` – name of the algorithm
|
|
132
|
+
|
|
133
|
+
### Filters
|
|
134
|
+
|
|
135
|
+
#### `listFilter()`
|
|
136
|
+
|
|
137
|
+
List all added filters.
|
|
138
|
+
|
|
139
|
+
#### `addFilter( name, callback [, priority = 10 ] )`
|
|
140
|
+
|
|
141
|
+
Adds a custom normalization filter. Needs to be passed a unique name and callback function accepting a string and returns a normalized one. Prioritizing filters by setting higher priority (default is `10`).
|
|
142
|
+
|
|
143
|
+
Parameters:
|
|
144
|
+
|
|
145
|
+
- `<String> name` – filter name
|
|
146
|
+
- `<Function> callback` – callback function implementing the filter
|
|
147
|
+
- `<Int> priority` – priority of the filter
|
|
148
|
+
|
|
149
|
+
Example:
|
|
150
|
+
|
|
151
|
+
```js
|
|
152
|
+
const cmp = new CmpStr();
|
|
153
|
+
|
|
154
|
+
cmp.addFilter( 'prefix', ( str ) => `prefix_${str}` );
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
#### `rmvFilter( name )`
|
|
158
|
+
|
|
159
|
+
Removes a custom normalization filter.
|
|
160
|
+
|
|
161
|
+
Parameters:
|
|
162
|
+
|
|
163
|
+
- `<String> name` – filter name
|
|
164
|
+
|
|
165
|
+
#### `pauseFilter( name )`
|
|
166
|
+
|
|
167
|
+
Pauses a custom normalization filter.
|
|
168
|
+
|
|
169
|
+
Parameters:
|
|
170
|
+
|
|
171
|
+
- `<String> name` – filter name
|
|
172
|
+
|
|
173
|
+
#### `resumeFilter( name )`
|
|
174
|
+
|
|
175
|
+
Resumes a custom normalization filter.
|
|
176
|
+
|
|
177
|
+
Parameters:
|
|
178
|
+
|
|
179
|
+
- `<String> name` – filter name
|
|
180
|
+
|
|
181
|
+
#### `clearFilter( name )`
|
|
182
|
+
|
|
183
|
+
Clears normalization filters (removing all of them).
|
|
184
|
+
|
|
185
|
+
### Similarity Comparison
|
|
186
|
+
|
|
187
|
+
#### `compare( algo, a, b [, config = {} ] )`
|
|
188
|
+
|
|
189
|
+
Compares two strings using the specified algorithm. The method returns either the similarity score as a floating point number between 0 and 1 or raw output, if the algorithm supports it and the user passes `raw=true` through the config options.
|
|
190
|
+
|
|
191
|
+
Parameters:
|
|
192
|
+
|
|
193
|
+
- `<String> algo` – name of the algorithm
|
|
194
|
+
- `<String> a` – first string
|
|
195
|
+
- `<String> b` – second string
|
|
196
|
+
- `<Object> config` – configuration object
|
|
197
|
+
|
|
198
|
+
Example:
|
|
199
|
+
|
|
200
|
+
```js
|
|
201
|
+
const cmp = new CmpStr();
|
|
202
|
+
|
|
203
|
+
console.log( cmp.compare( 'levenshtein', 'hello', 'hallo' ) );
|
|
204
|
+
// Output: 0.8
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
#### `test( str [, config = {} ] )`
|
|
208
|
+
|
|
209
|
+
Tests the similarity between the base string and a given target string. Returns the same as ``compare``.
|
|
210
|
+
|
|
211
|
+
Parameters:
|
|
212
|
+
|
|
213
|
+
- `<String> str` – target string
|
|
214
|
+
- `<Object> config` – configuration object
|
|
215
|
+
|
|
216
|
+
Example:
|
|
217
|
+
|
|
218
|
+
```js
|
|
219
|
+
const cmp = new CmpStr( 'levenshtein', 'hello' );
|
|
220
|
+
|
|
221
|
+
console.log( cmp.test( 'hallo' ) );
|
|
222
|
+
// Output: 0.8
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
#### `batchTest( arr [, config = {} ] )`
|
|
226
|
+
|
|
227
|
+
Tests the similarity of multiple strings against the base string. Returns an array of objects with the target string and either the similarity score as a floating point number between 0 and 1 or raw output, if the algorithm supports it and the user passes `raw=true` through the config options.
|
|
228
|
+
|
|
229
|
+
Parameters:
|
|
230
|
+
|
|
231
|
+
- `<String[]> arr` – array of strings
|
|
232
|
+
- `<Object> config` – configuration object
|
|
233
|
+
|
|
234
|
+
Example:
|
|
235
|
+
|
|
236
|
+
```js
|
|
237
|
+
const cmp = new CmpStr( 'levenshtein', 'hello' );
|
|
238
|
+
|
|
239
|
+
console.log( cmp.batchTest( [ 'hallo', 'hola', 'hey' ] ) );
|
|
240
|
+
// Output: [ { target: 'hallo', match: 0.8 }, { target: 'hola', match: 0.4 }, { target: 'hey', match: 0.4 } ]
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
#### `match( arr [, config = {} ] )`
|
|
244
|
+
|
|
245
|
+
Finds strings in an array that exceed a similarity threshold and sorts them by highest similarity. Returns an array of objects contain target string and similarity score as a floating point number between 0 and 1.
|
|
246
|
+
|
|
247
|
+
Parameters:
|
|
248
|
+
|
|
249
|
+
- `<String[]> arr` – array of strings
|
|
250
|
+
- `<Object> config` – configuration object
|
|
251
|
+
|
|
252
|
+
Example:
|
|
253
|
+
|
|
254
|
+
```js
|
|
255
|
+
const cmp = new CmpStr( 'levenshtein', 'hello' );
|
|
256
|
+
|
|
257
|
+
console.log( cmp.batchTest( [ 'hallo', 'hola', 'hey' ], {
|
|
258
|
+
threshold: 0.5
|
|
259
|
+
} ) );
|
|
260
|
+
// Output: [ { target: 'hallo', match: 0.8 } ]
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
#### `closest( arr [, config = {} ] )`
|
|
264
|
+
|
|
265
|
+
Finds the closest matching string from an array and returns them.
|
|
266
|
+
|
|
267
|
+
Parameters:
|
|
268
|
+
|
|
269
|
+
- `<String[]> arr` – array of strings
|
|
270
|
+
- `<Object> config` – configuration object
|
|
271
|
+
|
|
272
|
+
Example:
|
|
273
|
+
|
|
274
|
+
```js
|
|
275
|
+
const cmp = new CmpStr( 'levenshtein', 'hello' );
|
|
276
|
+
|
|
277
|
+
console.log( cmp.batchTest( [ 'hallo', 'hola', 'hey' ] ) );
|
|
278
|
+
// Output: 'hallo'
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
#### `similarityMatrix( algo, arr [, config = {} ] )`
|
|
282
|
+
|
|
283
|
+
Generates a similarity matrix for an array of strings. Returns an 2D array that represents the similarity matrix by floating point numbers between 0 and 1.
|
|
284
|
+
|
|
285
|
+
Parameters:
|
|
286
|
+
|
|
287
|
+
- `<String> algo` – name of the algorithm
|
|
288
|
+
- `<String[]> arr` – array of strings
|
|
289
|
+
- `<Object> config` – configuration object
|
|
290
|
+
|
|
291
|
+
Example:
|
|
292
|
+
|
|
293
|
+
```js
|
|
294
|
+
const cmp = new CmpStr();
|
|
295
|
+
|
|
296
|
+
console.log( cmp.similarityMatrix( 'levenshtein', [
|
|
297
|
+
'hello', 'hallo', 'hola'
|
|
298
|
+
] ) );
|
|
299
|
+
// Output: [ [ 1, 0.8, 0.4 ], [ 0.8, 1, 0.4 ], [ 0.4, 0.4, 1 ] ]
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## Customization
|
|
303
|
+
|
|
304
|
+
### Normalize Strings
|
|
305
|
+
|
|
306
|
+
The `CmpStr` package allows strings to be normalized before the similarity comparison. Options listed below are available for this and can either be set globally via `setFlags` or passed using the config object, which will overwrite the global flags. Flags are passed as a chained string in any order. For improved performance, normalized strings are stored in the cache, which can be freed using the `clearCache` method. Modifying custom filters automatically deletes the cache.
|
|
307
|
+
|
|
308
|
+
#### Supported Flags
|
|
309
|
+
|
|
310
|
+
- `s` – remove special chars
|
|
311
|
+
- `w` – collapse whitespaces
|
|
312
|
+
- `r` – remove repeated chars
|
|
313
|
+
- `k` – keep only letters
|
|
314
|
+
- `n` – ignore numbers
|
|
315
|
+
- `t` – trim whitespaces
|
|
316
|
+
- `i` – case insensitivity
|
|
317
|
+
- `d` – decompose unicode
|
|
318
|
+
- `u` – normalize unicode
|
|
319
|
+
|
|
320
|
+
#### `normalize( str [, flags = '' ] )`
|
|
321
|
+
|
|
322
|
+
The method for normalizing strings can also be called on its own, without comparing the similarity of two strings. This also applies all filters and reads or writes to the cache. This can be helpful if certain strings should be saved beforehand or different normalization options want to be tested.
|
|
323
|
+
|
|
324
|
+
Parameters:
|
|
325
|
+
|
|
326
|
+
- `<String> str` – string to normalize
|
|
327
|
+
- `<String> flags` normalization flags
|
|
328
|
+
|
|
329
|
+
Example:
|
|
330
|
+
|
|
331
|
+
```js
|
|
332
|
+
const cmp = new CmpStr();
|
|
333
|
+
|
|
334
|
+
console.log( cmp.normalize( ' he123LLo ', 'nti' ) );
|
|
335
|
+
// Output: hello
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
### Configuration Object
|
|
339
|
+
|
|
340
|
+
An additional object with optional parameters can be passed to all comparison methods (e.g. `test`, `match`, `closest` etc.) and their asynchronous pendants. This object includes the ability to pass `flags` for normalization to all methods, as well as the `threshold` parameter for `match` and `matchAsync`.
|
|
341
|
+
|
|
342
|
+
It also contains `options` as an object of key-value pairs that are passed to the comparison algorithm. Which additional arguments an algorithm accepts depends on the function exported from the module itself. Further down in this documentation, the various parameters for each algorithm are listed.
|
|
343
|
+
|
|
344
|
+
Global config options:
|
|
345
|
+
|
|
346
|
+
- `<String> flags` – normalization flags
|
|
347
|
+
- `<Number> threshold` – similarity threshold between 0 and 1
|
|
348
|
+
- `<Object> options` – options passed to the algorithm
|
|
349
|
+
|
|
350
|
+
Example:
|
|
351
|
+
|
|
352
|
+
```js
|
|
353
|
+
const cmp = new CmpStr( 'smithWaterman', 'alignment' );
|
|
354
|
+
|
|
355
|
+
console.log( cmp.match( [
|
|
356
|
+
' align ment', 'ali gnm ent ', ' alIGNMent'
|
|
357
|
+
], {
|
|
358
|
+
flags: 'it',
|
|
359
|
+
threshold: 0.8,
|
|
360
|
+
options: {
|
|
361
|
+
mismatch: -4,
|
|
362
|
+
gap: -2
|
|
363
|
+
}
|
|
364
|
+
} ) );
|
|
365
|
+
// Output: [ { target: ' alIGNMent', match: 1 }, { target: ' align ment', match: 0.8... }
|
|
366
|
+
]
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
## Asynchronous Support
|
|
370
|
+
|
|
371
|
+
The `CmpStrAsync` class provides asynchronous versions of all comparison methods. It is ideal for large datasets or non-blocking workflows.
|
|
372
|
+
|
|
373
|
+
The asynchronous class supports the methods `compareAsync`, `testAsync`, `batchTestAsync`, `matchAsync`, `closestAsync` and `similarityMatrixAsync`. Each of these methods returns a `Promise`.
|
|
374
|
+
|
|
375
|
+
For options, arguments and returned values, see the documentation above.
|
|
376
|
+
|
|
377
|
+
Example:
|
|
378
|
+
|
|
379
|
+
```js
|
|
380
|
+
const { CmpStrAsync } = require( 'cmpstr' );
|
|
381
|
+
|
|
382
|
+
const cmp = new CmpStrAsync( 'dice', 'best' );
|
|
383
|
+
|
|
384
|
+
cmp.batchTestAsync( [
|
|
385
|
+
'better', 'bestest', 'the best', 'good', ...
|
|
386
|
+
] ).then( console.log );
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
## Supported Algorithms
|
|
390
|
+
|
|
391
|
+
The following algorithms for similarity analysis are natively supported by the CmpStr package. Lazy-loading keeps memory consumption and loading time low, as only the algorithm intended to be used will be loaded as a module.
|
|
392
|
+
|
|
393
|
+
### Similarity Algorithms
|
|
394
|
+
|
|
395
|
+
#### Levenshtein Distance – `levenshtein`
|
|
396
|
+
|
|
397
|
+
The Levenshtein distance between two strings is the minimum number of single-character edits (i.e. insertions, deletions or substitutions) required to change one word into the other.
|
|
398
|
+
|
|
399
|
+
Options:
|
|
400
|
+
|
|
401
|
+
- `<Boolean> raw` – if true the raw distance is returned
|
|
402
|
+
|
|
403
|
+
#### Damerau-Levenshtein – `damerau`
|
|
404
|
+
|
|
405
|
+
The Damerau-Levenshtein distance differs from the classical Levenshtein distance by including transpositions among its allowable operations in addition to the three classical single-character edit operations (insertions, deletions and substitutions). Useful for correcting typos.
|
|
406
|
+
|
|
407
|
+
Options:
|
|
408
|
+
|
|
409
|
+
- `<Boolean> raw` – if true the raw distance is returned
|
|
410
|
+
|
|
411
|
+
#### Jaro-Winkler – `jaro`
|
|
70
412
|
|
|
71
|
-
|
|
413
|
+
Jaro-Winkler is a string similarity metric that gives more weight to matching characters at the start of the strings.
|
|
72
414
|
|
|
73
|
-
|
|
415
|
+
Options:
|
|
74
416
|
|
|
75
|
-
|
|
417
|
+
- `<Boolean> raw` – if true the raw distance is returned
|
|
76
418
|
|
|
77
|
-
|
|
78
|
-
* [Sørensen-Dice coefficient](https://en.wikipedia.org/wiki/Sørensen–Dice_coefficient)
|
|
419
|
+
#### Cosine Similarity – `cosine`
|
|
79
420
|
|
|
80
|
-
|
|
421
|
+
Cosine similarity is a measure how similar two vectors are. It's often used in text analysis to compare texts based on the words they contain.
|
|
81
422
|
|
|
82
|
-
|
|
423
|
+
Options:
|
|
83
424
|
|
|
84
|
-
|
|
425
|
+
- `<String> delimiter` – term delimiter
|
|
85
426
|
|
|
86
|
-
####
|
|
427
|
+
#### Dice Coefficient – `dice`
|
|
87
428
|
|
|
88
|
-
|
|
429
|
+
The Dice-Sørensen index equals twice the number of elements common to both sets divided by the sum of the number of elements in each set. Equivalently the index is the size of the intersection as a fraction of the average size of the two sets.
|
|
89
430
|
|
|
90
|
-
####
|
|
431
|
+
#### Jaccard Index – `jaccard`
|
|
91
432
|
|
|
92
|
-
|
|
433
|
+
The Jaccard Index measures the similarity between two sets by dividing the size of their intersection by the size of their union.
|
|
93
434
|
|
|
94
|
-
####
|
|
435
|
+
#### Hamming Distance – `hamming`
|
|
95
436
|
|
|
96
|
-
|
|
437
|
+
The Hamming distance between two equal-length strings of symbols is the number of positions at which the corresponding symbols are different.
|
|
97
438
|
|
|
98
|
-
|
|
439
|
+
#### Longest Common Subsequence – `lcs`
|
|
99
440
|
|
|
100
|
-
|
|
441
|
+
LCS measures the length of the longest subsequence common to both strings.
|
|
101
442
|
|
|
102
|
-
|
|
443
|
+
#### Needleman-Wunsch – `needlemanWunsch`
|
|
103
444
|
|
|
104
|
-
|
|
445
|
+
The Needleman-Wunsch algorithm performs global alignment, aligning two strings entirely, including gaps. It is commonly used in bioinformatics.
|
|
105
446
|
|
|
106
|
-
|
|
447
|
+
Options:
|
|
107
448
|
|
|
108
|
-
|
|
449
|
+
- `<Number> match` – score for a match
|
|
450
|
+
- `<Number> mismatch` – penalty for a mismatch
|
|
451
|
+
- `<Number> gap` – penalty for a gap
|
|
109
452
|
|
|
110
|
-
|
|
453
|
+
#### Smith-Waterman – `smithWaterman`
|
|
111
454
|
|
|
112
|
-
|
|
455
|
+
The Smith-Waterman algorithm performs local alignment, finding the best matching subsequence between two strings. It is commonly used in bioinformatics.
|
|
113
456
|
|
|
114
|
-
|
|
457
|
+
Options:
|
|
115
458
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
| ``s`` | non-whitespace characters only |
|
|
459
|
+
- `<Number> match` – score for a match
|
|
460
|
+
- `<Number> mismatch` – penalty for a mismatch
|
|
461
|
+
- `<Number> gap` – penalty for a gap
|
|
120
462
|
|
|
121
|
-
|
|
463
|
+
#### q-Gram – `qGram`
|
|
122
464
|
|
|
123
|
-
|
|
465
|
+
Q-gram similarity is a string-matching algorithm that compares two strings by breaking them into substrings of length Q. It's used to determine how similar the two strings are.
|
|
124
466
|
|
|
125
|
-
|
|
467
|
+
Options:
|
|
126
468
|
|
|
127
|
-
|
|
469
|
+
- `<Int> q` length of substrings
|
|
128
470
|
|
|
129
|
-
|
|
130
|
-
* Minor fixes
|
|
471
|
+
### Phonetic Algorithms
|
|
131
472
|
|
|
132
|
-
|
|
473
|
+
#### Soundex – `soundex`
|
|
133
474
|
|
|
134
|
-
|
|
475
|
+
The Soundex algorithm generates a phonetic representation of a string based on how it sounds. It supports predefined setups for English and German and allows users to provide custom options.
|
|
135
476
|
|
|
136
|
-
|
|
477
|
+
Options:
|
|
137
478
|
|
|
138
|
-
|
|
479
|
+
- `<String> lang` – language code for predefined setups (e.g., `en`, `de`)
|
|
480
|
+
- `<Boolean> raw` – if true, returns the raw sound index codes
|
|
481
|
+
- `<Object> mapping` – custom phonetic mapping (overrides predefined)
|
|
482
|
+
- `<String> exclude` – characters to exclude from the input (overrides predefined)
|
|
483
|
+
- `<Number> maxLength` – maximum length of the phonetic code
|
package/package.json
CHANGED
|
@@ -1,25 +1,47 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "cmpstr",
|
|
3
|
-
"description": "lightweight npm package to calculate string similarity",
|
|
4
|
-
"author": {
|
|
5
|
-
"name" : "komed3 (Paul Köhler)",
|
|
6
|
-
"email" : "webmaster@komed3.de",
|
|
7
|
-
"url" : "https://komed3.de"
|
|
8
|
-
},
|
|
9
|
-
"homepage": "https://github.com/komed3/cmpstr#readme",
|
|
10
|
-
"version": "
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
"similarity",
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "cmpstr",
|
|
3
|
+
"description": "lightweight npm package to calculate string similarity",
|
|
4
|
+
"author": {
|
|
5
|
+
"name" : "komed3 (Paul Köhler)",
|
|
6
|
+
"email" : "webmaster@komed3.de",
|
|
7
|
+
"url" : "https://komed3.de"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://github.com/komed3/cmpstr#readme",
|
|
10
|
+
"version": "2.0.0",
|
|
11
|
+
"main": "src/index.js",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"string-similarity",
|
|
15
|
+
"string-comparison",
|
|
16
|
+
"similarity-algorithms",
|
|
17
|
+
"phonetic-search",
|
|
18
|
+
"soundex",
|
|
19
|
+
"levenshtein-distance",
|
|
20
|
+
"damerau-levenshtein",
|
|
21
|
+
"jaro-winkler",
|
|
22
|
+
"cosine-similarity",
|
|
23
|
+
"dice-coefficient",
|
|
24
|
+
"jaccard-index",
|
|
25
|
+
"hamming-distance",
|
|
26
|
+
"longest-common-subsequence",
|
|
27
|
+
"needleman-wunsch",
|
|
28
|
+
"smith-waterman",
|
|
29
|
+
"q-gram",
|
|
30
|
+
"similarity-matrix",
|
|
31
|
+
"batch-operations",
|
|
32
|
+
"normalization",
|
|
33
|
+
"asynchronous",
|
|
34
|
+
"custom-algorithms",
|
|
35
|
+
"text-processing",
|
|
36
|
+
"fuzzy-matching",
|
|
37
|
+
"string-matching",
|
|
38
|
+
"text-similarity"
|
|
39
|
+
],
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "git+https://github.com/komed3/cmpstr.git"
|
|
43
|
+
},
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/komed3/cmpstr/issues"
|
|
46
|
+
}
|
|
47
|
+
}
|