Sha256: fff1129e40b6f6c3e40f56b5e85cb16f5e4282b66028cf282d2997ea71e3dd02

Contents?: true

Size: 1.68 KB

Versions: 8

Compression:

Stored size: 1.68 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:
      #
      # 1. Creative commons licenses cannot be matched against license files
      #    that begin with the title of a non-open source CC license variant
      # 2. 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|
            if license.creative_commons? && file.potential_false_positive?
              false
            else
              license.wordset && license.length_delta(file) <= license.max_delta
            end
          end
        end
      end

      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

      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

8 entries across 8 versions & 1 rubygems

Version Path
licensee-8.9.2 lib/licensee/matchers/dice_matcher.rb
licensee-8.9.0 lib/licensee/matchers/dice_matcher.rb
licensee-8.8.5 lib/licensee/matchers/dice_matcher.rb
licensee-8.8.4 lib/licensee/matchers/dice_matcher.rb
licensee-8.8.3 lib/licensee/matchers/dice_matcher.rb
licensee-8.8.2 lib/licensee/matchers/dice_matcher.rb
licensee-8.8.1 lib/licensee/matchers/dice_matcher.rb
licensee-8.8.0 lib/licensee/matchers/dice_matcher.rb