mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Instrument all calls and pass the blob, strategy and language candidates in the payload.
This commit is contained in:
@@ -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.
|
||||
#
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user