forked from azjezz/psl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevenshtein.php
47 lines (40 loc) · 1.63 KB
/
levenshtein.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
declare(strict_types=1);
namespace Psl\Str;
use Psl;
use function levenshtein as php_levenshtien;
/**
* Calculate Levenshtein distance between two strings.
*
* Note: In its simplest form the function will take only the two strings
* as parameter and will calculate just the number of insert, replace and
* delete operations needed to transform str1 into str2.
* Note: A second variant will take three additional parameters that define
* the cost of insert, replace and delete operations. This is more general
* and adaptive than variant one, but not as efficient.
*
* @throws Psl\Exception\InvariantViolationException If neither all, or none of the costs is supplied.
*
* @return int this function returns the Levenshtein-Distance between the
* two argument strings or -1, if one of the argument strings
* is longer than the limit of 255 characters
*
* @pure
*/
function levenshtein(
string $source,
string $target,
?int $cost_of_insertion = null,
?int $cost_of_replacement = null,
?int $cost_of_deletion = null
): int {
if (null === $cost_of_deletion && null === $cost_of_insertion && null === $cost_of_replacement) {
return php_levenshtien($source, $target);
}
// https://github.com/php/php-src/blob/623911f993f39ebbe75abe2771fc89faf6b15b9b/ext/standard/levenshtein.c#L101
Psl\invariant(
null !== $cost_of_deletion && null !== $cost_of_insertion && null !== $cost_of_replacement,
'Expected either all costs to be supplied, or non.'
);
return php_levenshtien($source, $target, $cost_of_insertion, $cost_of_replacement, $cost_of_deletion);
}