Class: Licensee::LevenshteinMatcher

Inherits:
Matcher
  • Object
show all
Defined in:
lib/licensee/matchers/levenshtein_matcher.rb

Instance Attribute Summary

Attributes inherited from Matcher

#file

Instance Method Summary collapse

Methods inherited from Matcher

#initialize, match

Constructor Details

This class inherits a constructor from Licensee::Matcher

Instance Method Details

#confidenceObject

Confidence that the matched license is a match



31
32
33
# File 'lib/licensee/matchers/levenshtein_matcher.rb', line 31

def confidence
  @confidence ||= match ? similarity(match) : 0
end

#length_delta(license) ⇒ Object

Calculate the difference between the file length and a given license’s length



20
21
22
# File 'lib/licensee/matchers/levenshtein_matcher.rb', line 20

def length_delta(license)
  (file_length - license.body_normalized.length).abs
end

#matchObject

Return the first potential license that is more similar than the confidence threshold



5
6
7
8
9
# File 'lib/licensee/matchers/levenshtein_matcher.rb', line 5

def match
  @match ||= potential_licenses.find do |license|
    similarity(license) >= Licensee::CONFIDENCE_THRESHOLD
  end
end

#max_deltaObject

Maximum possible difference between file length and license length for a license to be a potential license to be matched



26
27
28
# File 'lib/licensee/matchers/levenshtein_matcher.rb', line 26

def max_delta
  @max_delta ||= (file_length * (Licensee::CONFIDENCE_THRESHOLD.to_f / 100.to_f ))
end

#potential_licensesObject

Sort all licenses, in decending order, by difference in length to the file Difference in lengths cannot exceed the file’s length * the confidence threshold / 100



13
14
15
16
17
# File 'lib/licensee/matchers/levenshtein_matcher.rb', line 13

def potential_licenses
  @potential_licenses ||= begin
    Licensee.licenses.select { |license| length_delta(license) <= max_delta }.sort_by { |l| length_delta(l) }
  end
end