class Linguist::Heuristics

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

Constants

ObjectiveCRegex

Common heuristics

Public Class Methods

call(blob, candidates) click to toggle source

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.

# File lib/linguist/heuristics.rb, line 16
def self.call(blob, candidates)
  data = blob.data

  @heuristics.each do |heuristic|
    if heuristic.matches?(blob.name)
      languages = Array(heuristic.call(data))
      return languages if languages.any? || languages.all? { |l| candidates.include?(l) }
    end
  end

  [] # No heuristics matched
end
disambiguate(*extensions, &heuristic) click to toggle source

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 ".pm" do |data|
  if data.include?("use strict")
    Language["Perl"]
  elsif /^[^#]+:-/.match(data)
    Language["Prolog"]
  end
end
# File lib/linguist/heuristics.rb, line 44
def self.disambiguate(*extensions, &heuristic)
  @heuristics << new(extensions, &heuristic)
end
new(extensions, &heuristic) click to toggle source

Internal

# File lib/linguist/heuristics.rb, line 52
def initialize(extensions, &heuristic)
  @extensions = extensions
  @heuristic = heuristic
end

Public Instance Methods

call(data) click to toggle source

Internal: Perform the heuristic

# File lib/linguist/heuristics.rb, line 64
def call(data)
  @heuristic.call(data)
end
matches?(filename) click to toggle source

Internal: Check if this heuristic matches the candidate languages.

# File lib/linguist/heuristics.rb, line 58
def matches?(filename)
  filename = filename.downcase
  @extensions.any? { |ext| filename.end_with?(ext) }
end