Class: Rouge::Guesser

Inherits:
Object
  • Object
show all
Defined in:
lib/rouge/guesser.rb

Defined Under Namespace

Classes: Ambiguous

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.guess(guessers, lexers) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rouge/guesser.rb', line 14

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

Instance Method Details

#collect_best(lexers, opts = {}, &scorer) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rouge/guesser.rb', line 33

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

#filter(lexers) ⇒ Object


53
54
55
# File 'lib/rouge/guesser.rb', line 53

def filter(lexers)
  raise 'abstract'
end