Реализации алгоритмов/Расстояние Левенштейна: различия между версиями

Содержимое удалено Содержимое добавлено
→‎Visual Basic 6.0: working version (prev. is not workable)
м <source> -> <syntaxhighlight> (phab:T237267)
Строка 3:
 
== [[w:Visual Basic|Visual Basic 6.0]] ==
<sourcesyntaxhighlight lang="vb">
 
Public Function levenshtein(ByVal string1 As String, ByVal string2 As String) As Long
Строка 60:
MsgBox levenstein("папа", "мама")
 
</syntaxhighlight>
</source>
 
== [[w:PHP|PHP]] ==
<sourcesyntaxhighlight lang="php">
<?PHP
 
Строка 101:
echo levenshtein($source, $dest); // built-in PHP function
?>
</syntaxhighlight>
</source>
 
== [[w:JavaScript|JavaScript]] ==
<sourcesyntaxhighlight lang="javascript">
/**
* @param {string} s1 Исходная строка
Строка 149:
return buf[l2 + cutHalf - flip];
}
</syntaxhighlight>
</source>
 
Пример использования:
<sourcesyntaxhighlight lang="javascript">
var diff = levenshtein('Hello', 'HelA_1');
</syntaxhighlight>
</source>
 
== [[w:Haskell|Haskell]] ==
<sourcesyntaxhighlight lang="haskell">
import Data.List
 
Строка 165:
compute c' x y z = minimum [y + 1, z + 1, x + if c' == c then 0 else 1]
 
</syntaxhighlight>
</source>
 
== Среда [[w:PowerBuilder|PowerBuilder]] ==
нумерация элементов массивов начинается с единицы
 
<sourcesyntaxhighlight lang="C">
function integer gf_lev_distance (string a_vsource, string a_vtarget);
 
Строка 219:
end function
 
</syntaxhighlight>
</source>
 
== [[w:Java|Java]] ==
Линейное потребление памяти без выделения памяти в процессе работы алгоритма.
 
<sourcesyntaxhighlight lang="Java">
 
public static int levenstain(@Nonnull String str1, @Nonnull String str2) {
Строка 255:
}
 
</syntaxhighlight>
</source>
 
== [[w:Scala|Scala]] ==
 
<sourcesyntaxhighlight lang="Scala">
class Lowenstein(s1: String, s2: String) {
 
Строка 290:
}
 
</syntaxhighlight>
</source>
пример использования:
<sourcesyntaxhighlight lang="Scala">
println( new Lowenstein("Hi, men!", "Hi, women!").getDistance )
</syntaxhighlight>
</source>
 
== [[w:Python|Python]] ==
 
<sourcesyntaxhighlight lang="python">
 
def distance(a, b):
Строка 319:
return current_row[n]
 
</syntaxhighlight>
</source>
 
== [[w:Turbo Pascal|Turbo Pascal]] ==
Строка 328:
Дата: 29 июля 2009 г.
-->
<sourcesyntaxhighlight lang="Pascal">
 
program LeveDist; {Turbo PASCAL}
Строка 402:
writeln(levenshtein(s1,s2));
end.
</syntaxhighlight>
</source>
 
== [[w:Delphi|Delphi]] ==
Строка 411:
Дата: 10 августа 2002 г.
-->
<sourcesyntaxhighlight lang="Pascal">
 
const
Строка 493:
end;
end;
</syntaxhighlight>
</source>
 
Пример использования:
<sourcesyntaxhighlight lang="Pascal">
// на форме имеются поля Edit1 и Edit2, метка Label1
...
Label1.Caption := IntToStr(LeveDist(Edit1.Text, Edit2.Text));
...
</syntaxhighlight>
</source>
 
== [[w:Perl|Perl]] ==
<sourcesyntaxhighlight lang="perl">
sub levenshtein($$){
my @A=split //, lc shift;
Строка 529:
if ($_[0] < $_[2]){ pop; } else { shift; }
return $_[0] < $_[1]? $_[0]:$_[1];
}</sourcesyntaxhighlight>
 
Пример использования:
<sourcesyntaxhighlight lang="perl">
print levenshtein("google","googol");
</syntaxhighlight>
</source>
 
== [[w:C++|C++]] ==
Строка 543:
Дата: 05 января 2019 г.
-->
<sourcesyntaxhighlight lang="CPP">
#include <algorithm>
#include <vector>
Строка 579:
return lev_dist[min_size];
}
</syntaxhighlight>
</source>
 
Пример использования:
<sourcesyntaxhighlight lang="CPP">
// Может быть использован любой подходящий контейнер (напр. std::vector).
// В нашем примере использованы строки:
Строка 590:
const std::string::size_type distance = LevenshteinDistance(src, dst);
...
</syntaxhighlight>
</source>
 
Имплементация обобщённого расстояния Левенштейна с произвольными стоимостями вставки, удаления и замены символа:
Строка 599:
Дата: 05 января 2019 г.
-->
<sourcesyntaxhighlight lang="CPP">
#include <algorithm>
#include <vector>
Строка 639:
return lev_dist[min_size];
}
</syntaxhighlight>
</source>
 
== [[w:C#|C#]] ==
Строка 648:
Дата: 10 декабря 2007 г.
-->
<sourcesyntaxhighlight lang="csharp">
public static int LevenshteinDistance(string string1, string string2)
{
Строка 672:
return m[string1.Length, string2.Length];
}
</syntaxhighlight>
</source>
 
Пример использования:
<sourcesyntaxhighlight lang="csharp">
string s1="мама";
string s2="папа";
int diff=LevenshteinDistance(s1,s2);
</syntaxhighlight>
</source>
 
== [[w:Transact-SQL|Transact-SQL]] ==
Строка 685:
''Примечание: Данная процедура может использоваться только в качестве примера, или для тестирования в силу плохой производительности.''
 
<sourcesyntaxhighlight lang="TSQL">
create procedure LevenshteinDistance(@string1 varchar(4000), @string2 varchar(4000))
as
Строка 775:
return @retval
end
</syntaxhighlight>
</source>
 
Пример использования:
<sourcesyntaxhighlight lang="TSQL">
declare @retval int
exec @retval = LevenshteinDistance 'Петрова Дарья Ивановна', 'Петрова Марья Ивановна'
select @retval
</syntaxhighlight>
</source>
 
== [[w:Go|Go]] ==
 
<sourcesyntaxhighlight lang="go">
func distance(s1, s2 string) int {
min := func(values ...int) int {
Строка 826:
return currentRow[n]
}
</syntaxhighlight>
</source>
== См. также ==
* [[Реализации алгоритмов/Редакционное предписание]]