Instrument all calls and pass the blob, strategy and language candidates in the payload.

This commit is contained in:
Arfon Smith
2015-02-26 15:27:33 -06:00
parent fd7633518f
commit 9a86b9ad75
2 changed files with 19 additions and 12 deletions

View File

@@ -16,11 +16,9 @@ module Linguist
# #
# Returns an Array of Language objects, most probable first. # Returns an Array of Language objects, most probable first.
def self.call(blob, possible_languages) def self.call(blob, possible_languages)
Linguist.instrument("linguist.bayesian_classification") do language_names = possible_languages.map(&:name)
language_names = possible_languages.map(&:name) classify(Samples.cache, blob.data, language_names).map do |name, _|
classify(Samples.cache, blob.data, language_names).map do |name, _| Language[name] # Return the actual Language objects
Language[name] # Return the actual Language objects
end
end end
end end

View File

@@ -107,18 +107,27 @@ module Linguist
Linguist.instrument("linguist.detection") do Linguist.instrument("linguist.detection") do
# Call each strategy until one candidate is returned. # Call each strategy until one candidate is returned.
STRATEGIES.reduce([]) do |languages, strategy| languages = []
candidates = strategy.call(blob, 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 if candidates.size == 1
return candidates.first languages = candidates
break
elsif candidates.size > 1 elsif candidates.size > 1
# More than one candidate was found, pass them to the next strategy. # More than one candidate was found, pass them to the next strategy.
candidates languages = candidates
else else
# No candiates were found, pass on languages from the previous strategy. # No candidates, try the next strategy
languages
end end
end.first end
Linguist.instrument("linguist.detected", :blob => blob, :strategy => strategy, :language => languages.first) do
languages.first
end
end end
end end