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.
Class Method Summary collapse
-
.call(blob, languages) ⇒ Object
Public: Use heuristics to detect language of the blob.
-
.disambiguate(*languages, &heuristic) ⇒ Object
Internal: Define a new heuristic.
Instance Method Summary collapse
-
#call(data) ⇒ Object
Internal: Perform the heuristic.
-
#initialize(languages, &heuristic) ⇒ Heuristics
constructor
Internal.
-
#matches?(candidates) ⇒ Boolean
Internal: Check if this heuristic matches the candidate languages.
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 data.include?(":-")
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.all? { |l| @languages.include?(l.name) } end |