Skip to content

feat: add string/base/distances/hamming-grapheme-clusters #1684

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<!--

@license Apache-2.0

Copyright (c) 2023 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# hammingGraphemeClusters

> Calculate the [Hamming distance][hamming-distance] between two strings, treating grapheme clusters as the unit of comparison

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var hammingGraphemeClusters = require( '@stdlib/string/base/distances/hamming-grapheme-clusters' );
```

#### hammingGraphemeClusters( s1, s2 )

Calculates the [Hamming distance][hamming-distance] between two strings, treating [grapheme clusters][grapheme-cluster] (as opposed to characters) as a single unit

```javascript
var dist = hammingGraphemeClusters( 'café', 'cafe' );
// returns 1

dist = hammingGraphemeClusters( '🍕🍕🍕', '❤🍕🍕' );
// returns 1

dist = hammingGraphemeClusters( 'tooth', 'froth' );
// returns 2

dist = hammingGraphemeClusters( '', '' );
// returns 0

dist = hammingGraphemeClusters( 'Später', 'Spæter' );
// returns 1
```

</section>

<!-- /.usage -->

<!-- Package notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- If the two strings differ in length, the [Hamming distance][hamming-distance] is not defined. Consequently, when provided two input strings of unequal number of grapheme units, the function returns a sentinel value of `-1`.
- As the function calculates the [Hamming distance][hamming-distance] by comparing grapheme clusters, when the character(s) are composed of multiple Unicode code points, it will treat the difference between the characters as a unit difference.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

```javascript
var hammingGraphemeClusters = require( '@stdlib/string/base/distances/hamming-grapheme-clusters' );

var dist = hammingGraphemeClusters( '🅰nimate', '🅰niⓂ🅰te' );
// returns 2

dist = hammingGraphemeClusters( 'elephant', 'Tashkent' );
// returns 6

dist = hammingGraphemeClusters( 'Résumé', 'Resume' );
// returns 2

dist = hammingGraphemeClusters( '', '' );
// returns 0
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[hamming-distance]: https://en.wikipedia.org/wiki/Hamming_distance
[grapheme-cluster]: https://www.unicode.org/reports/tr29/

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var pkg = require( './../package.json' ).name;
var hammingGraphemeClusters = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var values;
var value;
var out;
var i;

values = [
[ 'algorithms', 'altruistic' ],
[ '😊😃😄😁', '😊😃😁😄' ],
[ '', '' ],
[ 'résumé', 'resume' ],
[ 'a🅰b', 'a🅰b' ],
[ '🌑🌒🌓🌔🌕', '..🌓🌔🌕' ],
[ 'प्रेम', 'विश्व' ],
[ 'fránçáis!', 'francais?' ],
[ '1️⃣2️⃣3️⃣4️⃣', '123️⃣4' ],
[ '🚗🚕🚙🚌🚎', '🚎🚌🚙🚕🚗' ],
[ '∑x²y', 'Σx^2' ],
[ 'congratulations', 'conmgeautlatins' ]
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
value = values[ i%values.length ];
out = hammingGraphemeClusters( value[0], value[1] );
if ( typeof out !== 'number' ) {
b.fail( 'should return a number' );
}
}
b.toc();
if ( typeof out !== 'number' ) {
b.fail( 'should return a number' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

{{alias}}( s1, s2 )
Calculates the Hamming distance between two input strings by
comparing their grapheme clusters

The function returns a sentinel value of -1 if the input strings are
unequal in grapheme clusters count

Parameters
----------
s1: string
First input string.

s2: string
Second input string.

Returns
-------
out: number
Hamming distance.

Examples
--------
> var d = {{alias}}( 'Résumé', 'Resume' )
2
> d = {{alias}}( '✔✔', '❌✔' )
1
> d = {{alias}}( 'abc', '🅰🅰c' )
2
> d = {{alias}}( 'El Niño', 'La Niña' )
3
> d = {{alias}}( 'sacrifice', 'paradisal' )
8

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/**
* Calculates the Hamming distance between two strings by comparing their grapheme clusters
*
* ## Notes
*
* - The function returns a sentinel value of `-1` if the input string lengths differ.
*
* @param str1 - first input string
* @param str2 - second input string
* @returns Hamming distance
*
* @example
* var dist = hammingGraphemeClusters( 'La Niña', 'El Niño' );
* // returns 3
*
* @example
* var dist = hammingGraphemeClusters( 'algorithms', 'altruistic' );
* // returns 7
*
* @example
* var dist = hammingGraphemeClusters( '✔✔', '✔❌' );
* // returns 1
*/
declare function hammingGraphemeClusters( str1: string, str2: string ): number;


// EXPORTS //

export = hammingGraphemeClusters;
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import hammingGraphemeClusters = require( './index' );


// TESTS //

// The function returns a number...
{
hammingGraphemeClusters( '', '' ); // $ExpectType number
}

// The compiler throws an error if the function is provided a first argument which is not a string...
{
hammingGraphemeClusters( true, '' ); // $ExpectError
hammingGraphemeClusters( false, '' ); // $ExpectError
hammingGraphemeClusters( null, '' ); // $ExpectError
hammingGraphemeClusters( undefined, '' ); // $ExpectError
hammingGraphemeClusters( 5, '' ); // $ExpectError
hammingGraphemeClusters( [], '' ); // $ExpectError
hammingGraphemeClusters( {}, '' ); // $ExpectError
hammingGraphemeClusters( ( x: number ): number => x, '' ); // $ExpectError
}

// The compiler throws an error if the function is provided a second argument which is not a string...
{
hammingGraphemeClusters( '', true ); // $ExpectError
hammingGraphemeClusters( '', false ); // $ExpectError
hammingGraphemeClusters( '', null ); // $ExpectError
hammingGraphemeClusters( '', undefined ); // $ExpectError
hammingGraphemeClusters( '', 5 ); // $ExpectError
hammingGraphemeClusters( '', [] ); // $ExpectError
hammingGraphemeClusters( '', {} ); // $ExpectError
hammingGraphemeClusters( '', ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
hammingGraphemeClusters(); // $ExpectError
hammingGraphemeClusters( '' ); // $ExpectError
hammingGraphemeClusters( '', '', 3 ); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var hammingGraphemeClusters = require( './../lib' );

console.log( hammingGraphemeClusters( 'algorithms', 'altruistic' ) );
// => 7

console.log( hammingGraphemeClusters( '😊😃😄😁', '😊😃😁😄' ) );
// => 2

console.log( hammingGraphemeClusters( '', '' ) );
// => 0

console.log( hammingGraphemeClusters( 'résumé', 'resume' ) );
// => 2

console.log( hammingGraphemeClusters( 'a🅰b', 'a🅰b' ) );
// => 0

console.log( hammingGraphemeClusters( '🌑🌒🌓🌔🌕', '..🌓🌔🌕' ) );
// => 2

console.log( hammingGraphemeClusters( 'प्रेम', 'विश्व' ) );
// => 3

console.log( hammingGraphemeClusters( 'fránçáis!', 'francais?' ) );
// => 4

console.log( hammingGraphemeClusters( '1️⃣2️⃣3️⃣4️⃣', '123️⃣4' ) );
// => 3

console.log( hammingGraphemeClusters( '🚗🚕🚙🚌🚎', '🚎🚌🚙🚕🚗' ) );
// => 4

console.log( hammingGraphemeClusters( 'x²y', 'x^2' ) );
// => 2

console.log( hammingGraphemeClusters( 'hola', 'hold' ) );
// => 1
Loading