Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,34 +0,0 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Main is
|
||||
function Levenshtein_Distance (S, T : String) return Natural is
|
||||
D : array (0 .. S'Length, 0 .. T'Length) of Natural;
|
||||
begin
|
||||
for I in D'Range (1) loop
|
||||
D (I, 0) := I;
|
||||
end loop;
|
||||
for I in D'Range (2) loop
|
||||
D (0, I) := I;
|
||||
end loop;
|
||||
for J in T'Range loop
|
||||
for I in S'Range loop
|
||||
if S (I) = T (J) then
|
||||
D (I, J) := D (I - 1, J - 1);
|
||||
else
|
||||
D (I, J) :=
|
||||
Natural'Min
|
||||
(Natural'Min (D (I - 1, J) + 1, D (I, J - 1) + 1),
|
||||
D (I - 1, J - 1) + 1);
|
||||
end if;
|
||||
end loop;
|
||||
end loop;
|
||||
return D (S'Length, T'Length);
|
||||
end Levenshtein_Distance;
|
||||
begin
|
||||
Ada.Text_IO.Put_Line
|
||||
("kitten -> sitting:" &
|
||||
Integer'Image (Levenshtein_Distance ("kitten", "sitting")));
|
||||
Ada.Text_IO.Put_Line
|
||||
("rosettacode -> raisethysword:" &
|
||||
Integer'Image (Levenshtein_Distance ("rosettacode", "raisethysword")));
|
||||
end Main;
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
identification division.
|
||||
program-id. Levenshtein.
|
||||
|
||||
environment division.
|
||||
configuration section.
|
||||
repository.
|
||||
function all intrinsic.
|
||||
|
||||
data division.
|
||||
working-storage section.
|
||||
77 string-a pic x(255).
|
||||
77 string-b pic x(255).
|
||||
77 length-a pic 9(3).
|
||||
77 length-b pic 9(3).
|
||||
77 distance pic z(3).
|
||||
77 i pic 9(3).
|
||||
77 j pic 9(3).
|
||||
01 tab.
|
||||
05 filler occurs 256.
|
||||
10 filler occurs 256.
|
||||
15 costs pic 9(3).
|
||||
|
||||
procedure division.
|
||||
main.
|
||||
move "kitten" to string-a
|
||||
move "sitting" to string-b
|
||||
perform levenshtein-dist
|
||||
|
||||
move "rosettacode" to string-a
|
||||
move "raisethysword" to string-b
|
||||
perform levenshtein-dist
|
||||
stop run
|
||||
.
|
||||
levenshtein-dist.
|
||||
move length(trim(string-a)) to length-a
|
||||
move length(trim(string-b)) to length-b
|
||||
|
||||
initialize tab
|
||||
|
||||
perform varying i from 0 by 1 until i > length-a
|
||||
move i to costs(i + 1, 1)
|
||||
end-perform
|
||||
|
||||
perform varying j from 0 by 1 until j > length-b
|
||||
move j to costs(1, j + 1)
|
||||
end-perform
|
||||
|
||||
perform with test after varying i from 2 by 1 until i > length-a
|
||||
perform with test after varying j from 2 by 1 until j > length-b
|
||||
if string-a(i - 1:1) = string-b(j - 1:1)
|
||||
move costs(i - 1, j - 1) to costs(i, j)
|
||||
else
|
||||
move min(min(costs(i - 1, j) + 1, *> a deletion
|
||||
costs(i, j - 1) + 1), *> an insertion
|
||||
costs(i - 1, j - 1) + 1) *> a substitution
|
||||
to costs(i, j)
|
||||
end-if
|
||||
end-perform
|
||||
end-perform
|
||||
move costs(length-a + 1, length-b + 1) to distance
|
||||
display trim(string-a) " -> " trim(string-b) " = " trim(distance)
|
||||
.
|
||||
|
|
@ -1,47 +0,0 @@
|
|||
function min(sequence s)
|
||||
atom m
|
||||
m = s[1]
|
||||
for i = 2 to length(s) do
|
||||
if s[i] < m then
|
||||
m = s[i]
|
||||
end if
|
||||
end for
|
||||
return m
|
||||
end function
|
||||
|
||||
function levenshtein(sequence s1, sequence s2)
|
||||
integer n, m
|
||||
sequence d
|
||||
n = length(s1) + 1
|
||||
m = length(s2) + 1
|
||||
|
||||
if n = 1 then
|
||||
return m-1
|
||||
elsif m = 1 then
|
||||
return n-1
|
||||
end if
|
||||
|
||||
d = repeat(repeat(0, m), n)
|
||||
for i = 1 to n do
|
||||
d[i][1] = i-1
|
||||
end for
|
||||
|
||||
for j = 1 to m do
|
||||
d[1][j] = j-1
|
||||
end for
|
||||
|
||||
for i = 2 to n do
|
||||
for j = 2 to m do
|
||||
d[i][j] = min({
|
||||
d[i-1][j] + 1,
|
||||
d[i][j-1] + 1,
|
||||
d[i-1][j-1] + (s1[i-1] != s2[j-1])
|
||||
})
|
||||
end for
|
||||
end for
|
||||
|
||||
return d[n][m]
|
||||
end function
|
||||
|
||||
? levenshtein("kitten", "sitting")
|
||||
? levenshtein("rosettacode", "raisethysword")
|
||||
|
|
@ -1,59 +0,0 @@
|
|||
function Get-LevenshteinDistance
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[OutputType([PSCustomObject])]
|
||||
Param
|
||||
(
|
||||
[Parameter(Mandatory=$true, Position=0)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[Alias("s")]
|
||||
[string]
|
||||
$ReferenceObject,
|
||||
|
||||
[Parameter(Mandatory=$true, Position=1)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[Alias("t")]
|
||||
[string]
|
||||
$DifferenceObject
|
||||
)
|
||||
|
||||
[int]$n = $ReferenceObject.Length
|
||||
[int]$m = $DifferenceObject.Length
|
||||
|
||||
$d = New-Object -TypeName 'System.Object[,]' -ArgumentList ($n + 1),($m + 1)
|
||||
|
||||
$outputObject = [PSCustomObject]@{
|
||||
ReferenceObject = $ReferenceObject
|
||||
DifferenceObject = $DifferenceObject
|
||||
Distance = $null
|
||||
}
|
||||
|
||||
for ($i = 0; $i -le $n; $i++)
|
||||
{
|
||||
$d[$i, 0] = $i
|
||||
}
|
||||
|
||||
for ($i = 0; $i -le $m; $i++)
|
||||
{
|
||||
$d[0, $i] = $i
|
||||
}
|
||||
|
||||
for ($i = 1; $i -le $m; $i++)
|
||||
{
|
||||
for ($j = 1; $j -le $n; $j++)
|
||||
{
|
||||
if ($ReferenceObject[$j - 1] -eq $DifferenceObject[$i - 1])
|
||||
{
|
||||
$d[$j, $i] = $d[($j - 1), ($i - 1)]
|
||||
}
|
||||
else
|
||||
{
|
||||
$d[$j, $i] = [Math]::Min([Math]::Min(($d[($j - 1), $i] + 1), ($d[$j, ($i - 1)] + 1)), ($d[($j - 1), ($i - 1)] + 1))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$outputObject.Distance = $d[$n, $m]
|
||||
|
||||
$outputObject
|
||||
}
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
Get-LevenshteinDistance "kitten" "sitting"
|
||||
Get-LevenshteinDistance rosettacode raisethysword
|
||||
|
|
@ -1,44 +0,0 @@
|
|||
' Levenshtein distance - 23/04/2020
|
||||
|
||||
Function Min(a,b)
|
||||
If a < b then Min = a : Else Min = b
|
||||
End Function 'Min
|
||||
|
||||
Function Levenshtein(s1, s2)
|
||||
Dim d(), i, j, n1, n2, d1, d2, d3
|
||||
n1 = Len(s1) + 1
|
||||
n2 = Len(s2) + 1
|
||||
ReDim d(n1, n2)
|
||||
If n1 = 1 Then
|
||||
Levenshtein = n2 - 1
|
||||
Exit Function
|
||||
End If
|
||||
If n2 = 1 Then
|
||||
Levenshtein = n1 - 1
|
||||
Exit Function
|
||||
End If
|
||||
For i = 1 To n1
|
||||
d(i, 1) = i - 1
|
||||
Next
|
||||
For j = 1 To n2
|
||||
d(1, j) = j - 1
|
||||
Next
|
||||
For i = 2 To n1
|
||||
For j = 2 To n2
|
||||
d1 = d(i - 1, j ) + 1
|
||||
d2 = d(i, j - 1) + 1
|
||||
d3 = d(i - 1, j - 1) + Abs(Mid(s1, i - 1, 1) <> Mid(s2, j - 1, 1))
|
||||
d(i, j) = Min(d1, Min(d2, d3))
|
||||
Next
|
||||
Next
|
||||
Levenshtein = d(n1, n2)
|
||||
End Function 'Levenshtein
|
||||
|
||||
Sub PrintLevenshtein(c1, c2)
|
||||
WScript.StdOut.WriteLine c1&" "& c2&" "& Levenshtein(c1, c2)
|
||||
End Sub 'PrintLevenshtein
|
||||
|
||||
PrintLevenshtein "kitten", "sitting"
|
||||
PrintLevenshtein "rosettacode", "raisethysword"
|
||||
PrintLevenshtein "saturday", "sunday"
|
||||
PrintLevenshtein "sleep", "fleeting"
|
||||
Loading…
Add table
Add a link
Reference in a new issue