Class: Licensee::Matchers::Dice
- Inherits:
-
Object
- Object
- Licensee::Matchers::Dice
- Defined in:
- lib/licensee/matchers/dice_matcher.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
-
#confidence ⇒ Object
Confidence that the matched license is a match.
-
#initialize(file) ⇒ Dice
constructor
A new instance of Dice.
- #licenses_by_similiarity ⇒ Object
-
#match ⇒ Object
Return the first potential license that is more similar than the confidence threshold.
- #matches ⇒ Object
-
#potential_licenses ⇒ Object
Licenses that may be a match for this file.
Constructor Details
#initialize(file) ⇒ Dice
Returns a new instance of Dice.
6 7 8 |
# File 'lib/licensee/matchers/dice_matcher.rb', line 6 def initialize(file) @file = file end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
4 5 6 |
# File 'lib/licensee/matchers/dice_matcher.rb', line 4 def file @file end |
Instance Method Details
#confidence ⇒ Object
Confidence that the matched license is a match
55 56 57 |
# File 'lib/licensee/matchers/dice_matcher.rb', line 55 def confidence @confidence ||= match ? file.similarity(match) : 0 end |
#licenses_by_similiarity ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/licensee/matchers/dice_matcher.rb', line 39 def licenses_by_similiarity @licenses_by_similiarity ||= begin licenses = potential_licenses.map do |license| [license, license.similarity(file)] end licenses.sort_by { |_, similarity| similarity }.reverse end end |
#match ⇒ Object
Return the first potential license that is more similar than the confidence threshold
12 13 14 15 16 17 18 |
# File 'lib/licensee/matchers/dice_matcher.rb', line 12 def match @match ||= if matches.empty? nil else matches.first[0] end end |
#matches ⇒ Object
48 49 50 51 52 |
# File 'lib/licensee/matchers/dice_matcher.rb', line 48 def matches @matches ||= licenses_by_similiarity.select do |_, similarity| similarity >= Licensee.confidence_threshold end end |
#potential_licenses ⇒ Object
Licenses that may be a match for this file. To avoid false positives:
-
Creative commons licenses cannot be matched against license files that begin with the title of a non-open source CC license variant
-
The percentage change in file length may not exceed the inverse of the confidence threshold
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/licensee/matchers/dice_matcher.rb', line 27 def potential_licenses @potential_licenses ||= begin Licensee.licenses(hidden: true).select do |license| if license.creative_commons? && file.potential_false_positive? false else license.wordset && license.length_delta(file) <= license.max_delta end end end end |