Skip to content

Instantly share code, notes, and snippets.

@ryanong
Created June 21, 2011 19:27

Revisions

  1. ryanong revised this gist Jul 8, 2011. 1 changed file with 19 additions and 17 deletions.
    36 changes: 19 additions & 17 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,23 @@
    str1.downcase!
    pairs1 = (0..str1.length-2).collect {|i| str1[i,2]}.reject {
    |pair| pair.include? " "}
    str2.downcase!
    pairs2 = (0..str2.length-2).collect {|i| str2[i,2]}.reject {
    |pair| pair.include? " "}
    union = pairs1.size + pairs2.size
    intersection = 0
    pairs1.each do |p1|
    0.upto(pairs2.size-1) do |i|
    if p1 == pairs2[i]
    intersection += 1
    pairs2.slice!(i)
    break
    def compare_strings(str1,str2)
    str1.downcase!
    pairs1 = (0..str1.length-2).collect {|i| str1[i,2]}.reject { |pair| pair.include? " "}

    str2.downcase!
    pairs2 = (0..str2.length-2).collect {|i| str2[i,2]}.reject { |pair| pair.include? " "}

    union = pairs1.size + pairs2.size
    intersection = 0

    pairs1.each do |p1|
    0.upto(pairs2.size-1) do |i|
    if p1 == pairs2[i]
    intersection += 1
    pairs2.slice!(i)
    break
    end
    end
    end
    end
    (2.0 * intersection) / union

    (2.0 * intersection) / union
    end
    # http://www.catalysoft.com/articles/StrikeAMatch.html
    # http://stackoverflow.com/questions/653157/a-better-similarity-ranking-algorithm-for-variable-length-strings
  2. ryanong created this gist Jun 21, 2011.
    21 changes: 21 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    str1.downcase!
    pairs1 = (0..str1.length-2).collect {|i| str1[i,2]}.reject {
    |pair| pair.include? " "}
    str2.downcase!
    pairs2 = (0..str2.length-2).collect {|i| str2[i,2]}.reject {
    |pair| pair.include? " "}
    union = pairs1.size + pairs2.size
    intersection = 0
    pairs1.each do |p1|
    0.upto(pairs2.size-1) do |i|
    if p1 == pairs2[i]
    intersection += 1
    pairs2.slice!(i)
    break
    end
    end
    end
    (2.0 * intersection) / union

    # http://www.catalysoft.com/articles/StrikeAMatch.html
    # http://stackoverflow.com/questions/653157/a-better-similarity-ranking-algorithm-for-variable-length-strings