Sha256: 3466888d12965dda8473d30f6ccc17f0beef392d83d64d70e66c381a0aec8b83

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 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   ||= 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

1 entries across 1 versions & 1 rubygems

Version Path
opener-language-identifier-4.3.0 lib/opener/language_identifier/detector.rb