Class: Linguist::Heuristics

Inherits:
Object
  • Object
show all
Defined in:
lib/linguist/heuristics.rb

Overview

A collection of simple heuristics that can be used to better analyze languages.

Constant Summary collapse

ObjectiveCRegex =

Common heuristics

/^[ \t]*@(interface|class|protocol|property|end|synchronised|selector|implementation)\b/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(languages, &heuristic) ⇒ Heuristics

Internal



49
50
51
52
# File 'lib/linguist/heuristics.rb', line 49

def initialize(languages, &heuristic)
  @languages = languages
  @heuristic = heuristic
end

Class Method Details

.call(blob, languages) ⇒ Object

Public: Use heuristics to detect language of the blob.

blob - An object that quacks like a blob. possible_languages - Array of Language objects

Examples

Heuristics.call(FileBlob.new("path/to/file"), [
  Language["Ruby"], Language["Python"]
])

Returns an Array of languages, or empty if none matched or were inconclusive.



16
17
18
19
20
21
22
23
24
# File 'lib/linguist/heuristics.rb', line 16

def self.call(blob, languages)
  data = blob.data

  @heuristics.each do |heuristic|
    return Array(heuristic.call(data)) if heuristic.matches?(languages)
  end

  [] # No heuristics matched
end

.disambiguate(*languages, &heuristic) ⇒ Object

Internal: Define a new heuristic.

languages - String names of languages to disambiguate. heuristic - Block which takes data as an argument and returns a Language or nil.

Examples

disambiguate "Perl", "Prolog" do |data|
  if data.include?("use strict")
    Language["Perl"]
  elsif /^[^#]+:-/.match(data)
    Language["Prolog"]
  end
end


41
42
43
# File 'lib/linguist/heuristics.rb', line 41

def self.disambiguate(*languages, &heuristic)
  @heuristics << new(languages, &heuristic)
end

Instance Method Details

#call(data) ⇒ Object

Internal: Perform the heuristic



60
61
62
# File 'lib/linguist/heuristics.rb', line 60

def call(data)
  @heuristic.call(data)
end

#matches?(candidates) ⇒ Boolean

Internal: Check if this heuristic matches the candidate languages.



55
56
57
# File 'lib/linguist/heuristics.rb', line 55

def matches?(candidates)
  candidates.any? && candidates.all? { |l| @languages.include?(l.name) }
end