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,13 +16,11 @@ 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
end
end
# Public: Train classifier that data is a certain language.
#

View File

@@ -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
Linguist.instrument("linguist.detected", :blob => blob, :strategy => strategy, :language => languages.first) do
languages.first
end
end.first
end
end