mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Try strategies until one language is returned
This commit is contained in:
@@ -4,7 +4,7 @@ module Linguist
|
||||
ACTIVE = true
|
||||
|
||||
def self.call(blob, languages)
|
||||
find_by_heuristics(blob.data, langauges.map(&:name))
|
||||
find_by_heuristics(blob.data, languages.map(&:name))
|
||||
end
|
||||
|
||||
# Public: Given an array of String language names,
|
||||
|
||||
@@ -10,6 +10,9 @@ require 'linguist/heuristics'
|
||||
require 'linguist/samples'
|
||||
require 'linguist/file_blob'
|
||||
require 'linguist/blob_helper'
|
||||
require 'linguist/strategy/filename'
|
||||
require 'linguist/strategy/shebang'
|
||||
require 'linguist/strategy/classifier'
|
||||
|
||||
module Linguist
|
||||
# Language names that are recognizable by GitHub. Defined languages
|
||||
@@ -91,10 +94,6 @@ module Linguist
|
||||
language
|
||||
end
|
||||
|
||||
require 'linguist/strategy/filename'
|
||||
require 'linguist/strategy/shebang'
|
||||
require 'linguist/strategy/classifier'
|
||||
|
||||
STRATEGIES = [
|
||||
Linguist::Strategy::Filename,
|
||||
Linguist::Strategy::Shebang,
|
||||
@@ -112,17 +111,16 @@ module Linguist
|
||||
# Bail early if the blob is binary or empty.
|
||||
return nil if blob.likely_binary? || blob.binary? || blob.empty?
|
||||
|
||||
# Call each strategy until 0 or 1 candidates are returned
|
||||
# Call each strategy until one candidate is returned
|
||||
STRATEGIES.reduce([]) do |languages, strategy|
|
||||
if candidates = strategy.call(blob, languages)
|
||||
if candidates.size > 1
|
||||
# More than one candidate was found, return them for the next strategy
|
||||
candidates = strategy.call(blob, languages)
|
||||
if candidates.size == 1
|
||||
return candidates.first
|
||||
elsif candidates.size > 1
|
||||
# More than one candidate was found, pass them to the next strategy
|
||||
candidates
|
||||
else
|
||||
# 1 or 0 candidates, stop trying strategies
|
||||
break candidates
|
||||
end
|
||||
else
|
||||
# Strategy couldn't find any candidates, so pass on the original list
|
||||
languages
|
||||
end
|
||||
end.first
|
||||
|
||||
@@ -3,7 +3,7 @@ module Linguist
|
||||
# Detect language using the bayesian classifier
|
||||
class Classifier
|
||||
def self.call(blob, languages)
|
||||
Linguist::Classifier.classify(Samples.cache, blob.data, laguages.map(&:name)).map do |name|
|
||||
Linguist::Classifier.classify(Samples.cache, blob.data, languages.map(&:name)).map do |name, _|
|
||||
# Return the actual Language object based of the string language name (i.e., first element of `#classify`)
|
||||
Language[name]
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user