Compare the similarity of two strings in Javascript, Typescript and Php

One of the most common features that can be found on software applications is the possibility to perform text searches: A User types some arbitrary text and the application gives a list of results, somehow related to what the user was looking for. Based on this use case, efficient search engines must be able to find texts that fully or partially match the searched words and give relevant results to the user.

While searching for an exact string match on a database is a pretty straightforward task, getting fuzzy results (just like the user expects) requires a strategy to find similar texts by ignoring differences like letter case, accents or special characters which would prevent valid results from being obtained. In this scenario, having a good method to compare strings is a key feature.

The Levenshtein distance

The mathematical method that is commonly used as the base for string comparison is called Levenshtein distance. It is a string metric for measuring the difference between two sequences and is computed as a number which represents the minimum number of single-character edits (insertions, deletions or substitutions) that are required to change one of the strings into the other. Vladimir Levenshtein is the mathematician who considered this distance in 1965, and so the method received his name. String similarity comparison methods on the TurboCommons library use this method as the base for comparison calculations

You can compare similarity between two strings online here

Comparing string similarity with the TurboCommons library

The StringUtils class contains two general purpose methods that will help us to compare strings: compareByLevenshtein() which calculates the Levenshtein distance, and compareSimilarityPercent() which tells how different two strings are.

Let's see several examples for the different supported TurboCommons programming languages:

Compare string similarity with Php

Download the latest TurboCommons phar file from the downloads section, place it on your project as a dependency and run the following code:

require '%path-to-your-project-dependencies-folder%/turbocommons-php-X.X.X.phar';
use org\turbocommons\src\main\php\utils\StringUtils;
echo StringUtils::compareByLevenshtein('abcdefg', 'xabxcdxxefxgx')."<br>";
echo StringUtils::compareByLevenshtein('abc', 'axc')."<br>";
echo StringUtils::compareSimilarityPercent('a', 'b')."<br>";
echo StringUtils::compareSimilarityPercent('abcde', 'abcd')."<br>";

The output of the previous code should be:

6
1
0
80.0

Compare string similarity with Javascript on a website

Download the latest turbocommons-es5.js file from the downloads section or use npm to add the dependecy to your project (npm install turbocommons-es5). Then run the following code:

<script src="turbocommons-es5/turbocommons-es5.js"></script>
<script>
var StringUtils = org_turbocommons.StringUtils;
console.log(StringUtils.compareByLevenshtein('abcdefg', 'xabxcdxxefxgx'));
console.log(StringUtils.compareByLevenshtein('abc', 'axc'));
console.log(StringUtils.compareSimilarityPercent('a', 'b'));
console.log(StringUtils.compareSimilarityPercent('abcde', 'abcd'));
</script>

The console log for the previous code should be:

6
1
0
80.0

Compare string similarity with Typescript (TS)

The recommended way is to use npm to obtain the turbocommons dependency by executing the following command at the root of your project:

npm install turbocommons-ts

Or you can download the latest turbocommons-ts files from the downloads section and copy the dependency by yourself. Then run the following code:

import { StringUtils } from 'turbocommons-ts';
console.log(StringUtils.compareByLevenshtein('abcdefg', 'xabxcdxxefxgx'));
console.log(StringUtils.compareByLevenshtein('abc', 'axc'));
console.log(StringUtils.compareSimilarityPercent('a', 'b'));
console.log(StringUtils.compareSimilarityPercent('abcde', 'abcd'));

The output of the previous code should be:

6
1
0
80.0

Compare string similarity on an NodeJS app

Install the dependency by executing the following command at the root of your project:

npm install turbocommons-ts

And then run the following code:

const {StringUtils} = require('turbocommons-ts');
console.log(StringUtils.compareByLevenshtein('abcdefg', 'xabxcdxxefxgx'));
console.log(StringUtils.compareByLevenshtein('abc', 'axc'));
console.log(StringUtils.compareSimilarityPercent('a', 'b'));
console.log(StringUtils.compareSimilarityPercent('abcde', 'abcd'));

Which should output:

6
1
0
80.0