class Opener::LanguageIdentifier::Detector

Ruby wrapper around the Cybozu DetectorFactory and Detector classes. This class automatically handles switching of profiles based on input sizes, assigning priorities to languages, etc.

Attributes

backend[R]

Public Class Methods

new(backend = nil, fallback = nil) click to toggle source

@param [Hash] options

# File lib/opener/language_identifier/detector.rb, line 16
def initialize backend = nil, fallback = nil
  klass     = Backend.const_get backend.to_sym if backend
  klass   ||= Backend::LanguageDetection
  @backend  = klass.new

  klass     = Backend.const_get fallback.to_sym if fallback
  @fallback = klass.new if klass

  @timeout = ENV['TIMEOUT']&.to_i
end

Public Instance Methods

backend_detect(backend, input) click to toggle source
# File lib/opener/language_identifier/detector.rb, line 38
def backend_detect backend, input
  return backend.detect input unless @timeout
  Timeout.timeout @timeout do
    backend.detect input
  end
end
detect(input) click to toggle source

@return [String]

# File lib/opener/language_identifier/detector.rb, line 30
def detect(input)
  backend_detect @backend, input
rescue
  raise unless @fallback
  puts 'Using fallback backend' if ENV['DEBUG']
  backend_detect @fallback, input
end