Sha256: 6040686a27671c505f69cd96ddedd7da699238f393cc6974310391aeb98ee66a

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 KB

Contents

module Licensee
  module Matchers
    class Dice
      attr_reader :file

      def initialize(file)
        @file = file
      end

      # Return the first potential license that is more similar
      # than the confidence threshold
      def match
        @match ||= if matches.empty?
          nil
        else
          matches.first[0]
        end
      end

      # Licenses that may be a match for this file.
      # To avoid false positives, the percentage change in file length
      # may not exceed the inverse of the confidence threshold
      def potential_licenses
        @potential_licenses ||= begin
          Licensee.licenses(hidden: true).select do |license|
            license.wordset && license.length_delta(file) <= license.max_delta
          end
        end
      end

      def licenses_by_similiarity
        @licenses_by_similiarity ||= begin
          licenses = potential_licenses.map { |l| [l, l.similarity(file)] }
          licenses.sort_by { |_, similarity| similarity }.reverse
        end
      end

      def matches
        @matches ||= licenses_by_similiarity.select do |_, similarity|
          similarity >= Licensee.confidence_threshold
        end
      end

      # Confidence that the matched license is a match
      def confidence
        @confidence ||= match ? file.similarity(match) : 0
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
licensee-8.7.0 lib/licensee/matchers/dice_matcher.rb
licensee-8.6.1 lib/licensee/matchers/dice_matcher.rb
licensee-8.6.0 lib/licensee/matchers/dice_matcher.rb
licensee-8.5.0 lib/licensee/matchers/dice_matcher.rb