Class: Linguist::Heuristics
- Inherits:
-
Object
- Object
- Linguist::Heuristics
- 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
/^\s*(@(interface|class|protocol|property|end|synchronised|selector|implementation)\b|#import\s+.+\.h[">])/
Class Method Summary collapse
-
.call(blob, candidates) ⇒ Object
Public: Use heuristics to detect language of the blob.
-
.disambiguate(*extensions, &heuristic) ⇒ Object
Internal: Define a new heuristic.
Instance Method Summary collapse
-
#call(data) ⇒ Object
Internal: Perform the heuristic.
-
#initialize(extensions, &heuristic) ⇒ Heuristics
constructor
Internal.
-
#matches?(filename) ⇒ Boolean
Internal: Check if this heuristic matches the candidate languages.
Constructor Details
#initialize(extensions, &heuristic) ⇒ Heuristics
Internal
52 53 54 55 |
# File 'lib/linguist/heuristics.rb', line 52 def initialize(extensions, &heuristic) @extensions = extensions @heuristic = heuristic end |
Class Method Details
.call(blob, candidates) ⇒ 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 25 26 27 |
# 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) ⇒ 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 ".pm" do |data|
if data.include?("use strict")
Language["Perl"]
elsif /^[^#]+:-/.match(data)
Language["Prolog"]
end
end
44 45 46 |
# File 'lib/linguist/heuristics.rb', line 44 def self.disambiguate(*extensions, &heuristic) @heuristics << new(extensions, &heuristic) end |
Instance Method Details
#call(data) ⇒ Object
Internal: Perform the heuristic
64 65 66 |
# File 'lib/linguist/heuristics.rb', line 64 def call(data) @heuristic.call(data) end |
#matches?(filename) ⇒ Boolean
Internal: Check if this heuristic matches the candidate languages.
58 59 60 61 |
# File 'lib/linguist/heuristics.rb', line 58 def matches?(filename) filename = filename.downcase @extensions.any? { |ext| filename.end_with?(ext) } end |