Skip to content

Commit ad4ae2f

Browse files
Merge pull request #42 from Seen-Arabic/fix/similarity-score-range
fix: normalize case before calculating similarity scores
2 parents cf64bba + dc092f1 commit ad4ae2f

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- Release publishing now validates the tag/version and runs formatting, lint, and tests before publishing the verified build.
2727

2828
### Fixed
29+
- Calculate `similarityScore` lengths and edit distance after lowercasing so Unicode case expansion cannot produce negative scores or incorrect similarity ratios.
2930
- Prevent invalid levels from hanging `tashfeer`, `tashfeerBannedWords`, and `toOldArabicAndTashfeerBannedWords`: non-number values throw `TypeError`; negative, fractional, and non-finite numbers throw `RangeError`. Random-index generation handles empty input and caps the requested count at the word length ([#35](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/35), [PR #36](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/pull/36)).
3031
- Attempt browser bundle uploads after a successful build even if npm publishing fails ([commit 598035a](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/commit/598035a)).
3132

‎src/utils/string.util.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ export function setCharAt(str: string, index: number, chr: string): string {
1212
}
1313

1414
/**
15-
* Calculates a similarity score between two strings, potentially based on their edit distance.
15+
* Calculates a case-insensitive similarity score using Levenshtein distance over UTF-16 code units.
16+
* Both the distance and string lengths are measured after lowercasing.
1617
*
1718
* @param {string} s1 - The first string to compare.
1819
* @param {string} s2 - The second string to compare.
1920
* @returns {number} A similarity score representing how similar the two strings are. The score is between 0 and 1.
2021
*/
2122
export function similarityScore(s1: string, s2: string): number {
23+
s1 = s1.toLowerCase();
24+
s2 = s2.toLowerCase();
25+
2226
let longer = s1;
2327
let shorter = s2;
2428
// swap them if s1 is bigger than s2
@@ -45,9 +49,6 @@ export function similarityScore(s1: string, s2: string): number {
4549
* @returns {number} The edit distance between the two strings.
4650
*/
4751
function editDistance(s1: string, s2: string): number {
48-
s1 = s1.toLowerCase();
49-
s2 = s2.toLowerCase();
50-
5152
const costs = new Array<number>();
5253
for (let i = 0; i <= s1.length; i++) {
5354
let lastValue = i;

‎tests/scripts.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,17 @@ describe('#removeArabicAffixes', () => {
286286
});
287287

288288
describe('#similarityScore', () => {
289+
it.each([
290+
['İ', 'a', 0],
291+
['İ', 'i', 0.5],
292+
['İ', '', 0],
293+
['İ', 'i\u0307', 1],
294+
['A', 'a', 1],
295+
])('should score %j and %j as %s after case conversion in either order', (left, right, expected) => {
296+
expect(ArabicServices.utils.similarityScore(left, right)).toBe(expected);
297+
expect(ArabicServices.utils.similarityScore(right, left)).toBe(expected);
298+
});
299+
289300
it('should return 1 for identical strings', () => {
290301
const s1 = 'مرحبا';
291302
const s2 = 'مرحبا';

0 commit comments

Comments
 (0)