Class: FeldtRuby::Statistics::NormalizedCompressionDistance

Inherits:
StringDistance show all
Defined in:
lib/feldtruby/statistics/distance/string_distance.rb

Overview

Cilibrasi and Vitanyi’s NCD.

Instance Method Summary collapse

Methods inherited from StringDistance

#compress, #compressed_length

Instance Method Details

#distance(string1, string2) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/feldtruby/statistics/distance/string_distance.rb', line 21

def distance(string1, string2)
  return 0.0 if string1 == string2
  c1 = compressed_length(string1)
  c2 = compressed_length(string2)
  c_1_2 = compressed_length(string1 + string2)
  (c_1_2 - [c1, c2].min).to_f / ([c1, c2].max)
end