levenshtein function issue

J

Joe Smith

well, I'm trying to get the levenshtein function to work, and it's
close, but it's not quite giving me the right answer and i have no idea
why.


here's the code:
class Array2D
def initialize(width, height)
@data = Array.new(width) { Array.new(height) }
end
def [](x, y)
@data[x][y]
end
def []=(x, y, value)
@data[x][y] = value
end
end


def levenshtein(s, t)
#include cost?

m = s.size
n = t.size

d = Array2D.new(m+1,n-1)

for i in 0..m
d[i,0]=i
end

for j in 0..n
d[0,j]=j
end


for j in 1..n
for i in 1..m
if(s.eql?(t)) then
d[i,j]=d[i-1,j-1]
end

if not (s.eql?(t)) then
d[i,j] = [ (d[i-1,j]+1), (d[i,j-1]+1), (d[i-1,j-1]+1)
].min
end
end
end


puts "After:"+ d.inspect

puts "Ans: "
return d[m,n]
end #end func
=================================================

when I call it:
puts "Call to levenshtein():"
puts levenshtein("string1","2strings")
=================================================
and the output...
here's the output:
Call to levenshtein():
After:#<Array2D:0x283ee90 @data=[[0, 1, 2, 3, 4, 5, 6, 7, 8], [1, 1, 2,
3, 4, 5,
6, 7, 8], [2, 2, 2, 3, 4, 5, 6, 7, 8], [3, 3, 3, 3, 4, 5, 6, 7, 8], [4,
4, 4, 4
, 4, 5, 6, 7, 8], [5, 5, 5, 5, 5, 5, 6, 7, 8], [6, 6, 6, 6, 6, 6, 6, 7,
8], [7,
7, 7, 7, 7, 7, 7, 7, 8]]>
Ans:
8
=================================================
If you check those two strings I tested with at this URL, my resulting
matrix is wrong (no idea why), and the answer should be 2...
check it here:
http://www.miislita.com/searchito/levenshtein-edit-distance.html


=

THANK YOU SO MUCH FOR ANY HELP!
 
J

Joe Smith

Chris said:
...

You have 'n-1' here, but subsequently use indexes up to n.

cheers

Thanks for the reply. Yes, but when I change that, I just get an
error... (that's why I had originally changed it)
 
C

Chris Hulan

Thanks for the reply.  Yes, but when I change that, I just get an
error... (that's why I had originally changed it)

Looking at the description at http://www.levenshtein.net/ it appears
that both m and n should be +1
The strings are then offset to allow indexing from 1. Guess you
either need to add a character (blank?)
to the beginning of both strings, or adjust your indexes into s and t
(the 1st char is the 0th index)

I'm totally pulling this out of my head, as I don't have Ruby on my
work computer
 
J

Joe Smith

Chris said:
Looking at the description at http://www.levenshtein.net/ it appears
that both m and n should be +1
The strings are then offset to allow indexing from 1. Guess you
either need to add a character (blank?)
to the beginning of both strings, or adjust your indexes into s and t
(the 1st char is the 0th index)

I'm totally pulling this out of my head, as I don't have Ruby on my
work computer


Thank you for the reply. Unfortunately, I get the same error when I try
this :(
 
C

Chris Hulan

Thank you for the reply.  Unfortunately, I get the same error when I try
this :(

This was bugging me so I installed Ruby and gave it a go.
You do need to change that 'n-1' to be 'n+1'

Then in the nested loop you need to use both indexes:
for j in 1..n
for i in 1..m
if(s.eql?(t)) then
^this 'i' should be 'j'

But you still need to adjust the indexes into the strings (i-1 and j-1
seems to work here)

cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top