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
- ACTIVE =
false
Class Method Summary collapse
- .active? ⇒ Boolean
-
.disambiguate_c(data, languages) ⇒ Object
.h extensions are ambigious between C, C++, and Objective-C.
- .disambiguate_pl(data, languages) ⇒ Object
- .disambiguate_ts(data, languages) ⇒ Object
-
.find_by_heuristics(data, languages) ⇒ Object
Public: Given an array of String language names, apply heuristics against the given data and return an array of matching languages, or nil.
Class Method Details
.active? ⇒ Boolean
56 57 58 |
# File 'lib/linguist/heuristics.rb', line 56 def self.active? !!ACTIVE end |
.disambiguate_c(data, languages) ⇒ Object
.h extensions are ambigious between C, C++, and Objective-C. We want to shortcut look for Objective-C and now C++ too!
Returns an array of Languages or []
32 33 34 35 36 37 |
# File 'lib/linguist/heuristics.rb', line 32 def self.disambiguate_c(data, languages) matches = [] matches << Language["Objective-C"] if data.include?("@interface") matches << Language["C++"] if data.include?("#include <cstdint>") matches end |
.disambiguate_pl(data, languages) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/linguist/heuristics.rb', line 39 def self.disambiguate_pl(data, languages) matches = [] matches << Language["Prolog"] if data.include?(":-") matches << Language["Perl"] if data.include?("use strict") matches end |
.disambiguate_ts(data, languages) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/linguist/heuristics.rb', line 46 def self.disambiguate_ts(data, languages) matches = [] if (data.include?("</translation>")) matches << Language["XML"] else matches << Language["TypeScript"] end matches end |
.find_by_heuristics(data, languages) ⇒ Object
Public: Given an array of String language names, apply heuristics against the given data and return an array of matching languages, or nil.
data - Array of tokens or String data to analyze. languages - Array of language name Strings to restrict to.
Returns an array of Languages or []
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/linguist/heuristics.rb', line 14 def self.find_by_heuristics(data, languages) if active? if languages.all? { |l| ["Objective-C", "C++"].include?(l) } disambiguate_c(data, languages) end if languages.all? { |l| ["Perl", "Prolog"].include?(l) } disambiguate_pl(data, languages) end if languages.all? { |l| ["TypeScript", "XML"].include?(l) } disambiguate_ts(data, languages) end end end |