Class: Opener::LanguageIdentifier::Detector
- Inherits:
-
Object
- Object
- Opener::LanguageIdentifier::Detector
- Defined in:
- lib/opener/language_identifier/detector.rb
Overview
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.
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
Instance Method Summary collapse
- #backend_detect(backend, input) ⇒ Object
- #detect(input) ⇒ String
-
#initialize(backend = nil, fallback = nil) ⇒ Detector
constructor
A new instance of Detector.
Constructor Details
#initialize(backend = nil, fallback = nil) ⇒ Detector
Returns a new instance of Detector.
16 17 18 19 20 21 22 23 24 25 |
# 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 |
Instance Attribute Details
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
10 11 12 |
# File 'lib/opener/language_identifier/detector.rb', line 10 def backend @backend end |
Instance Method Details
#backend_detect(backend, input) ⇒ Object
38 39 40 41 42 43 |
# 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) ⇒ String
30 31 32 33 34 35 36 |
# 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 |