class TextAlignment::LCSComparison

Attributes

similarity[R]

The similarity ratio of the given two strings after stripping unmatched prefixes and suffixes

str1_match_final[R]

The initial and final matching positions of str1 and str2

str1_match_initial[R]

The initial and final matching positions of str1 and str2

str2_match_final[R]

The initial and final matching positions of str1 and str2

str2_match_initial[R]

The initial and final matching positions of str1 and str2

Public Class Methods

new(str1, str2, lcs = nil, sdiff = nil) click to toggle source
# File lib/text_alignment/lcs_comparison.rb, line 13
def initialize(str1, str2, lcs = nil, sdiff = nil)
        raise ArgumentError, "nil string" if str1 == nil || str2 == nil
        @str1, @str2 = str1, str2
        _lcs_comparison(str1, str2, lcs, sdiff)
end

Private Instance Methods

_lcs_comparison(str1, str2, lcs = nil, sdiff = nil) click to toggle source
# File lib/text_alignment/lcs_comparison.rb, line 21
def _lcs_comparison(str1, str2, lcs = nil, sdiff = nil)
        if lcs.nil?
                lcsmin = TextAlignment::LCSMin.new(str1, str2)
                lcs = lcsmin.lcs
                sdiff = lcsmin.sdiff
        end

        if lcs > 0
                match_initial = sdiff.index{|d| d.action == '='}
                match_final   = sdiff.rindex{|d| d.action == '='}

                @str1_match_initial = sdiff[match_initial].old_position
                @str2_match_initial = sdiff[match_initial].new_position
                @str1_match_final   = sdiff[match_final].old_position
                @str2_match_final   = sdiff[match_final].new_position
                mlcs = sdiff.count{|d| d.action == '=' && d.old_element =~ /\S/ && d.new_element =~ /\S/}
                @similarity  = 2 * mlcs / (str1[@str1_match_initial .. @str1_match_final].scan(/\S/).count + str2[@str2_match_initial .. @str2_match_final].scan(/\S/).count).to_f
                # @similarity  = 2 * lcs / (str1[@str1_match_initial .. @str1_match_final].length + str2[@str2_match_initial .. @str2_match_final].length).to_f
        else
                @str1_match_initial = 0
                @str2_match_initial = 0
                @str1_match_final   = 0
                @str2_match_final   = 0
                @similarity         = 0
        end
end