Sha256: 56032de78c171161cfea9a2709c4783439bf6be5402a7f8a8fa0677008c0e130

Contents?: true

Size: 1.94 KB

Versions: 2

Compression:

Stored size: 1.94 KB

Contents

require "cmp_text/version"

module CmpText
  class Analysis
    class << self
      attr_accessor :log_text

      def open(file)
        File.file?(file) ? File.open(file).read.chomp.strip.gsub(/'/, '') : file.to_s.dup
      end

      def txt_cmp(f0, f1)
        str_f0, str_f1 = open(f0), open(f1)

        @log_text = {
          or_text_1: str_f0.dup,
          or_text_2: str_f1.dup,
          succ_char:[],
          failed_char: []
        }

        arr_f0_each_char = str_f0.dup.split('')
        arr_f1_each_char = str_f1.dup.split('')
        str_f0_size = str_f0.dup.size
        str_f1_size = str_f1.dup.size

        arr_f0_each_char.each_with_index do |char, index|
          cmp = false
          break if [arr_f0_each_char.size, arr_f1_each_char.size].any?{|size| size == 0}
          to_index = index

          while str_f1.include?(char) do
            if [str_f0_size, str_f1_size].min < (to_index + 1)
              cmp = true
              break
            end
            to_index += 1
            char = arr_f0_each_char[index..to_index].join
          end

          to_index -= 1 unless [str_f0_size, str_f1_size].min == (to_index + 1) && cmp
          if (to_index - index) > 0 || (to_index - index == 0 && ([str_f0_size,   str_f1_size].min == 1))
            succ_char = arr_f0_each_char.slice!(index..to_index).join
            arr_f0_each_char.unshift(nil)
            arr_f1_each_char = arr_f1_each_char.join.sub(succ_char,'').split
            @log_text[:succ_char] << succ_char
          end
        end

        @log_text[:failed_char] << arr_f0_each_char.join << arr_f1_each_char.join
        
        @log_text[:failed_char].delete('')

        result = @log_text[:succ_char].join.size.to_f / [ @log_text[:or_text_1].size.to_f, @log_text[:or_text_2].size.to_f].max
      end

      def print(f0, f1)
        result = txt_cmp(f0, f1)

        { result: "匹配度是 #{(result * 100).round(3)}%", data: @log_text}
      end
    end
  end
end




Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cmp_text-0.1.3 lib/cmp_text.rb
cmp_text-0.1.2 lib/cmp_text.rb