Sha256: b40dce413c833b98c8c973d704ec269847d726cbe6572c3c24d49488c091a124

Contents?: true

Size: 1.14 KB

Versions: 5

Compression:

Stored size: 1.14 KB

Contents

module Opener
  class LanguageIdentifier
    ##
    # 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.
    #
    class Detector

      attr_reader :backend

      ##
      # @param [Hash] options
      #
      #
      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

      ##
      # @return [String]
      #
      def detect(input)
        backend_detect @backend, input
      rescue
        raise unless @fallback
        puts 'Using fallback backend' if ENV['DEBUG']
        backend_detect @fallback, input
      end

      def backend_detect backend, input
        return backend.detect input unless @timeout
        Timeout.timeout @timeout do
          backend.detect input
        end
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
opener-language-identifier-4.4.3 lib/opener/language_identifier/detector.rb
opener-language-identifier-4.4.1 lib/opener/language_identifier/detector.rb
opener-language-identifier-4.4.0 lib/opener/language_identifier/detector.rb
opener-language-identifier-4.3.2 lib/opener/language_identifier/detector.rb
opener-language-identifier-4.3.1 lib/opener/language_identifier/detector.rb