diff --git a/lib/linguist/classifier.rb b/lib/linguist/classifier.rb index 208467e4..89a0df2f 100644 --- a/lib/linguist/classifier.rb +++ b/lib/linguist/classifier.rb @@ -16,11 +16,9 @@ module Linguist # # Returns an Array of Language objects, most probable first. def self.call(blob, possible_languages) - Linguist.instrument("linguist.bayesian_classification") do - language_names = possible_languages.map(&:name) - classify(Samples.cache, blob.data, language_names).map do |name, _| - Language[name] # Return the actual Language objects - end + language_names = possible_languages.map(&:name) + classify(Samples.cache, blob.data, language_names).map do |name, _| + Language[name] # Return the actual Language objects end end diff --git a/lib/linguist/language.rb b/lib/linguist/language.rb index 68b4c4fc..bf0dfc33 100644 --- a/lib/linguist/language.rb +++ b/lib/linguist/language.rb @@ -107,18 +107,27 @@ module Linguist Linguist.instrument("linguist.detection") do # Call each strategy until one candidate is returned. - STRATEGIES.reduce([]) do |languages, strategy| - candidates = strategy.call(blob, languages) + languages = [] + strategy = nil + + STRATEGIES.each do |strategy| + candidates = Linguist.instrument("linguist.strategy", :blob => blob, :strategy => strategy, :candidates => languages) do + strategy.call(blob, languages) + end if candidates.size == 1 - return candidates.first + languages = candidates + break elsif candidates.size > 1 # More than one candidate was found, pass them to the next strategy. - candidates + languages = candidates else - # No candiates were found, pass on languages from the previous strategy. - languages + # No candidates, try the next strategy end - end.first + end + + Linguist.instrument("linguist.detected", :blob => blob, :strategy => strategy, :language => languages.first) do + languages.first + end end end