PHP's similar_text in JavaScript

How to use

You you can install via yarn add locutus and require this function via const similar_text = require('locutus/php/strings/similar_text').

It is important to use a bundler that supports tree-shaking so that you only ship the functions that you actually use to your browser, instead of all of Locutus, which is massive. Examples are: Parcel, webpack, or rollup.js. For server-side use this is typically less of a concern.

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1similar_text('Hello World!', 'Hello locutus!')8
2similar_text('Hello World!', null)0

Here’s what our current JavaScript equivalent to PHP's similar_text looks like.

module.exports = function similar_text(first, second, percent) {
// discuss at: https://locutus.io/php/similar_text/
// original by: Rafał Kukawski (https://blog.kukawski.pl)
// bugfixed by: Chris McMacken
// bugfixed by: Jarkko Rantavuori original by findings in stackoverflow (https://stackoverflow.com/questions/14136349/how-does-similar-text-work)
// improved by: Markus Padourek (taken from https://www.kevinhq.com/2012/06/php-similartext-function-in-javascript_16.html)
// example 1: similar_text('Hello World!', 'Hello locutus!')
// returns 1: 8
// example 2: similar_text('Hello World!', null)
// returns 2: 0

if (first === null || second === null || typeof first === 'undefined' || typeof second === 'undefined') {
return 0
}

first += ''
second += ''

let pos1 = 0
let pos2 = 0
let max = 0
const firstLength = first.length
const secondLength = second.length
let p
let q
let l
let sum

for (p = 0; p < firstLength; p++) {
for (q = 0; q < secondLength; q++) {
for (l = 0; p + l < firstLength && q + l < secondLength && first.charAt(p + l) === second.charAt(q + l); l++) {
// @todo: ^-- break up this crazy for loop and put the logic in its body
}
if (l > max) {
max = l
pos1 = p
pos2 = q
}
}
}

sum = max

if (sum) {
if (pos1 && pos2) {
sum += similar_text(first.substr(0, pos1), second.substr(0, pos2))
}

if (pos1 + max < firstLength && pos2 + max < secondLength) {
sum += similar_text(
first.substr(pos1 + max, firstLength - pos1 - max),
second.substr(pos2 + max, secondLength - pos2 - max),
)
}
}

if (!percent) {
return sum
}

return (sum * 200) / (firstLength + secondLength)
}

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we assimilate first iterations with imperfections, hoping for others to take issue with-and improve them. This unorthodox approach has worked very well to foster fun and fruitful collaboration, but please be reminded to use our creations at your own risk. THE SOFTWARE IS PROVIDED "AS IS" has never been more true than for Locutus.

Now go and: [ View on GitHub | Edit on GitHub | View Raw ]


« More PHP strings functions


Star