Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > do your bit for my mental health - how to find the differencebetween two strings?

Reply
Thread Tools

do your bit for my mental health - how to find the differencebetween two strings?

 
 
Iain Barnett
Guest
Posts: n/a
 
      12-14-2010
Hi,

I have a piece of code that doesn't work in a Sinatra app I've been =
writing that doesn't work.


These two forms don't work:

get '/tweets/:service/' do

mymethod( params[:service] )
...

get '/tweets/:service/' do

service =3D params[:service]
mymethod( service )
...


This works:

get '/tweets/:service/' do

mymethod( 'servicename' )

so does this:

get '/tweets/:service/' do

service =3D 'servicename'
mymethod( service )


Using the params[:service] variable then mymethod fails to do what I =
expect. If I hard-code the string that it's supposed to represent, it =
works. Every way I've examined this variable it looks exactly the same =
as a hard-coded version. Dump, inspect, to_s, length, class, equality, =
they all give the answer I expect - it's a String, and it is the same =
string that's in the URL that's been passed. I know that the method =
being called works, the only thing that doesn't is this variable. =
Nothing touches it before the method is called. I've tried via telnet =
too, no difference.

To help me keep the small grain of sanity I (hope to) have remaining, =
could anyone suggest a good way to find out what makes two strings =
different?=20

I'm currently reading stuff on using the debugger, along with some stuff =
on Rack Test, but if anyone has a useful insight I can use I'd be very, =
very grateful. Would I best running a test through the debugger, for =
example?

Regards
Iain





 
Reply With Quote
 
 
 
 
thunk
Guest
Posts: n/a
 
      12-14-2010


This is likely not what you are looking for directly, but it my give
you some ideas...

class String
def levenshtein( other, ins=2, del=2, sub=1)
#ins, del, sub are weighted costs
return nil if self.nil?
return nil if other.nil?
dm = [] #distance matrix

#Initialize first row values
dm[0] = (0..self.length).collect { | i | i * ins }
fill = [0] * (self.length - 1)

#initialize first column values
for i in 1..other.length
dm[i] = [i * del, fill.flatten]
end

#populate matrix
for i in 1..other.length
for j in 1..self.length
#critical comparison
dm[i][j] =
[dm[i-1][j-1] + (self[j-1] == other[i-1] ? 0 : sub), dm[i]
[j-1] + ins, dm[i-1][j] + del ].min
end
end

#The last value in the matrix is the Levenshtein distance betw
the strings
dm[other.length][self.length]
end

end

def ls( ar, threshold=3 )#Array must have at least 2 elements
word1, word2, nRslt, lRslt = ar.first.to_s, ar[1].to_s, 999, false
if ar.size == 2
nRslt = word1.levenshtein( word2 )
lRslt = nRslt <= threshold
elsif ar.size > 2
range = 1..ar.size - 1
range.each do | n |
word2 = ar[n]
nRslt = word1.levenshtein( word2 )
puts "word2 = " + word2.to_s + ", ls value = " +
nRslt.to_s
if nRslt <= threshold
lRslt = true
break
end
end
end

puts "word1 = " + word1.to_s + ", and word2 = " + word2.to_s + "
Result = " + nRslt.to_s + " Passed? " + lRslt.to_s
lRslt
end
 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      12-14-2010
Iain Barnett wrote in post #968364:
> I have a piece of code that doesn't work in a Sinatra app I've been
> writing that doesn't work.
>
> These two forms don't work:
>
> get '/tweets/:service/' do
>
> mymethod( params[:service] )
> ...
>
> get '/tweets/:service/' do
>
> service = params[:service]
> mymethod( service )
> ...
>
>
> This works:
>
> get '/tweets/:service/' do
>
> mymethod( 'servicename' )
>
> so does this:
>
> get '/tweets/:service/' do
>
> service = 'servicename'
> mymethod( service )
>
>
> Using the params[:service] variable then mymethod fails to do what I
> expect. If I hard-code the string that it's supposed to represent, it
> works. Every way I've examined this variable it looks exactly the same
> as a hard-coded version.


If you're using ruby 1.9, then there are hidden qualities to strings
So I suggest you try:

STDERR.puts service.inspect, service.encoding

Apart from that, I guess it's possible that there's something else in
the program's environment which is different between your two test runs.
To minimise these differences, I suggest

service = params[:service]
service.replace('servicename') # <<<<
STDERR.puts service.inspect, service.encoding
mymethod( service )

Try running this with the marked line present, and with that line
commented out.

HTH,

Brian.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      12-14-2010

On Dec 14, 2010, at 10:30 , thunk wrote:

> This is likely not what you are looking for directly, but it my give
> you some ideas...
>
> class String
> def levenshtein( other, ins=2, del=2, sub=1)


WTF?!? Did you even read his mail?


 
Reply With Quote
 
thunk
Guest
Posts: n/a
 
      12-15-2010
On Dec 14, 4:00*pm, Ryan Davis <(E-Mail Removed)> wrote:
> On Dec 14, 2010, at 10:30 , thunk wrote:
>
> > This is likely not what you are looking for directly, but it my give
> > you some ideas...

>
> > class String
> > * *def levenshtein( other, ins=2, del=2, sub=1)

>
> WTF?!? Did you even read his mail?



I was thinking from the title that knowing the exact difference
between two strings could help him find the source of the problem.
The Levenstein algorithm does this. I thought it could be useful and
I had a library handy with the source in it.

Actually, I have to wonder if you read mine: IT MAY GIVE YOU SOME
IDEAS. I don't get down the that level very often so I really did
think that code might help him.

George
 
Reply With Quote
 
Hassan Schroeder
Guest
Posts: n/a
 
      12-15-2010
On Tue, Dec 14, 2010 at 4:30 PM, thunk <(E-Mail Removed)> wrote:

> I was thinking from the title that knowing the exact difference
> between two strings could help him find the source of the problem.
> The Levenstein algorithm does this.


Well... your example code tells me the Levenshtein distance between
'foo' and 'food' is 2, which would seem an iffy assertion.

But I'm no mathematician

--
Hassan Schroeder ------------------------ http://www.velocityreviews.com/forums/(E-Mail Removed)
twitter: @hassan

 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      12-15-2010

On Dec 14, 2010, at 16:30 , thunk wrote:

> Actually, I have to wonder if you read mine: IT MAY GIVE YOU SOME
> IDEAS.


Well... my mind is blown. Obviously, for me to call you out for not =
having read the OP's email beyond the subject line, I'd have to read =
your mail to get an understanding that your answer has=20

## ##### #### #### # # # ##### ###### # # #=20
# # # # # # # # # # # # # # # =20
# # ##### #### # # # # # # ##### # # =20
###### # # # # # # # # # # # # =20
# # # # # # # # # # # # # # # =20
# # ##### #### #### ###### #### # ###### ###### # =20
=20
=
=20
# # #### ##### # # # # # #### ##### #### ##### =
#### =20
## # # # # # # # ## # # # # # # # # # =
#=20
# # # # # # ###### # # # # # # # # # # # =
#=20
# # # # # # # # # # # # # ### # # # # # # =
#=20
# ## # # # # # # # ## # # # # # # # # =
#=20
# # #### # # # # # # #### # #### ##### =
#### =20

with what he is asking.

On top of it, according to Hassan, apparently your algorithm is wrong. =
Good job!



 
Reply With Quote
 
botp
Guest
Posts: n/a
 
      12-15-2010
On Wed, Dec 15, 2010 at 2:15 AM, Iain Barnett <(E-Mail Removed)> wrote:
> These two forms don't work:
>
> get '/tweets/:service/' do
> =A0mymethod( params[:service] )
> ...
>
> get '/tweets/:service/' do
> =A0service =3D params[:service]
> =A0mymethod( service )
> ...



1 pls show terminal/screenshots
2 provide small code that produces the error (cause i cannot reproduce
that behaviour here)

thanks and best regards -botp

 
Reply With Quote
 
thunk
Guest
Posts: n/a
 
      12-15-2010


Listen,

If he adopted the nearness tester for a quick true/false for equality
he would get one of two results:

1. The test finds a difference, and this case he could drill down on
the physical difference.

2. The test finds no difference, then he could use it as a work-
around to get on with app while the powers that be do what the powers
that be do sometime in the future.

I will swear that the code, which I may or may not have modified,
worked for me when I was testing it some two years ago. It stuck me
as a very useful piece of code. It is out there somewhere under the
name given in the code, I should guess.

In any case it struck me as possibly useful, and if somebody took the
time to test it then that person also thought so, or?

Clearly if he can flip some system switch or whatever, then that is
the superior solution, I was the first to respond, and was unaware
that the versions changed internal formats, but that does make sense.

In the worst case the algorithm gets down and dirty with the visible
elements of a string - or did back in earlier versions. That might
well have come in handy.

Really, Ryon, if you want to head butt, let's take it off-line, I have
said all I feel I need to say after trying to help somebody.

Sincerely,

George
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      12-15-2010
On 15.12.2010 07:15, thunk wrote:
> If he adopted the nearness tester for a quick true/false for equality
> he would get one of two results:
>
> 1. The test finds a difference, and this case he could drill down on
> the physical difference.
>
> 2. The test finds no difference, then he could use it as a work-
> around to get on with app while the powers that be do what the powers
> that be do sometime in the future.


As Brian has correctly pointed out there are more ways that String
instances can differ than just character content. And Levenstein
Distance is zero for Strings with identical content (which the OP
indicated). So there is no additional information to be gained from
calculating this measure.

> George


George, my 0.02 EUR on this: admitting to have made a mistake and even
apologizing does not hurt. It has happened to me, it has happened to
others around here as well - it happens all the time to everybody. On
the contrary, trying to defend your initial posting which is obviously
off the mark is unlikely to earn you merits.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(UK) - mental health & benefit cuts. Ian Field Computer Support 23 10-18-2010 07:53 PM
Health is a state of complete physical, mental and social well-beingand not merely the absence of disease or infirmity. [1] ... fashion girl Computer Support 0 05-08-2008 06:13 AM
Health is a state of complete physical, mental and social well-beingand not merely the absence of disease or infirmity. [1] ... fashion girl Digital Photography 0 05-08-2008 06:11 AM



Advertisments