Sha256: 1357e49452a8c0b565d7a22971c6546fe7f68f8b2491d931b8872550576fd620

Contents?: true

Size: 1.31 KB

Versions: 48

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module Rouge
  class Guesser
    class Ambiguous < StandardError
      attr_reader :alternatives
      def initialize(alternatives); @alternatives = alternatives; end

      def message
        "Ambiguous guess: can't decide between #{alternatives.map(&:tag).inspect}"
      end
    end

    def self.guess(guessers, lexers)
      original_size = lexers.size

      guessers.each do |g|
        new_lexers = case g
        when Guesser then g.filter(lexers)
        when proc { |x| x.respond_to? :call } then g.call(lexers)
        else raise "bad guesser: #{g}"
        end

        lexers = new_lexers && new_lexers.any? ? new_lexers : lexers
      end

      # if we haven't filtered the input at *all*,
      # then we have no idea what language it is,
      # so we bail and return [].
      lexers.size < original_size ? lexers : []
    end

    def collect_best(lexers, opts={}, &scorer)
      best = []
      best_score = opts[:threshold]

      lexers.each do |lexer|
        score = scorer.call(lexer)

        next if score.nil?

        if best_score.nil? || score > best_score
          best_score = score
          best = [lexer]
        elsif score == best_score
          best << lexer
        end
      end

      best
    end

    def filter(lexers)
      raise 'abstract'
    end
  end
end

Version data entries

48 entries across 48 versions & 3 rubygems

Version Path
rouge-4.5.1 lib/rouge/guesser.rb
rouge-4.5.0 lib/rouge/guesser.rb
rouge-4.4.0 lib/rouge/guesser.rb
rouge-4.3.0 lib/rouge/guesser.rb
rouge-4.2.1 lib/rouge/guesser.rb
rouge-4.2.0 lib/rouge/guesser.rb
rouge-4.1.3 lib/rouge/guesser.rb
rouge-4.1.2 lib/rouge/guesser.rb
rouge-4.1.1 lib/rouge/guesser.rb
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/rouge-3.30.0/lib/rouge/guesser.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/rouge-3.30.0/lib/rouge/guesser.rb
rouge-4.1.0 lib/rouge/guesser.rb
rouge-4.0.1 lib/rouge/guesser.rb
rouge-4.0.0 lib/rouge/guesser.rb
rouge-3.30.0 lib/rouge/guesser.rb
rouge-3.29.0 lib/rouge/guesser.rb
rouge-3.28.0 lib/rouge/guesser.rb
rouge-3.27.0 lib/rouge/guesser.rb
rouge-3.26.1 lib/rouge/guesser.rb
mumukit-content-type-1.11.1 vendor/bundle/ruby/2.6.0/gems/rouge-3.26.0/lib/rouge/guesser.rb