3 Replies - 3019 Views - Last Post: 09 March 2012 - 12:50 PM Rate Topic: -----

#1 lnc12  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 268
  • Joined: 21-May 08

How to check how much two string are alike?

Posted 08 March 2012 - 11:14 AM

Hey I am having som troubles coming up with an algorithm for this:

I have to check how much for instance:

Caffe and latte is alike, so a and e are alike so 2/5 letters are a like.

Hope you get the idea
Do anyone have an idea.

Of course the string can be treated as a CHAR[] but ain't there a better way?
Is This A Good Question/Topic? 0
  • +

Replies To: How to check how much two string are alike?

#2 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: How to check how much two string are alike?

Posted 08 March 2012 - 11:54 AM

Did you see Karel-Lodewijk's genetic algo submission? He uses zip and map and he converts the letters to ordinals (or something like that) and detects their difference all in like one line.

$alphabet.zip(candidate).map{|x| (x[1].ord-x[0].ord)**2}.inject(:+)



This won't help you take into account the phonetic quality of the strings though, nor their language overlaps or senses =D

Zip will zip together two arrays.

a = "Caffe".chars.to_a
b = "latte".chars.to_a

z = a.zip(B)/>

z= [[C, l]
    [a, a]
    [f, t]
    [f, t]
    [e, e]]



From there, map is used to compare the characters (again, you'll need to connect or download a database of word senses if you need to make further scrutinize the strings).

This post has been edited by NotarySojac: 08 March 2012 - 12:01 PM

Was This Post Helpful? 2
  • +
  • -

#3 lnc12  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 268
  • Joined: 21-May 08

Re: How to check how much two string are alike?

Posted 08 March 2012 - 12:30 PM

Okay will look at it thanks man
Was This Post Helpful? 0
  • +
  • -

#4 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: How to check how much two string are alike?

Posted 09 March 2012 - 12:50 PM

Well there are different metrics you can use to decide how similar strings are. Which one works best depends on your requirements. If you are looking for something that quantifies how similar two strings are intuitively, you could use something like http://en.wikipedia....shtein_distance . In short it counts how many operations (delete/remove/substitute letter) it takes to transform one string into another.

This page has some code in some different programming languages including ruby http://rosettacode.o...n_distance#Ruby .

I personally wouldn't recommend my code as a good metric for string distance. For one it takes the alphabetic order into account. so the distance between "aaaa" and "bbbb" will be much smaller than the distance between "aaaa" and "zzzz". And secondly it always compares letters at the same position, so "bqz" will be very different from "abqz", while they are obviously quite similar. NotarySojac has quoted my code slightly out of context unless of course you are trying your hands at the genetic algorithm contest, in that case go ahead:)

This post has been edited by Karel-Lodewijk: 09 March 2012 - 02:41 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1