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-3.26.0 lib/rouge/guesser.rb
rouge-3.25.0 lib/rouge/guesser.rb
rouge-3.24.0 lib/rouge/guesser.rb
rouge-3.23.0 lib/rouge/guesser.rb
rouge-3.22.0 lib/rouge/guesser.rb
rouge-3.21.0 lib/rouge/guesser.rb
rouge-3.20.0 lib/rouge/guesser.rb
rouge-3.19.0 lib/rouge/guesser.rb
rouge-3.18.0 lib/rouge/guesser.rb
rouge-3.17.0 lib/rouge/guesser.rb
rouge-3.16.0 lib/rouge/guesser.rb
rouge-3.15.0 lib/rouge/guesser.rb
rouge-3.14.0 lib/rouge/guesser.rb
rouge-3.13.0 lib/rouge/guesser.rb
rouge-3.12.0 lib/rouge/guesser.rb
rouge-3.11.1 lib/rouge/guesser.rb
rouge-3.11.0 lib/rouge/guesser.rb
rouge-3.10.0 lib/rouge/guesser.rb
rouge-3.9.0 lib/rouge/guesser.rb
rouge-3.8.0 lib/rouge/guesser.rb