Class: Opener::LanguageIdentifier::Backend::DetectLanguageCom

Inherits:
Object
  • Object
show all
Defined in:
lib/opener/language_identifier/backend/detect_language_com.rb

Constant Summary collapse

CODE_MAP =

Unknown or languages that use a different code

{
  bug: nil,
  ceb: :tl,
  chr: nil,
  crs: nil,
  egy: nil,
  got: nil,
  haw: nil,
  hmn: nil,
  iw:  :he,
  jw:  :jv,
  kha: nil,
  lif: :li,
  mfe: nil,
  nso: nil,
  sco: nil,
  syr: nil,
  tlh: nil,
  war: :tl,
  zh:  :'zh-cn',
  'zh-Hant': :'zh-cn',
  'zh-tw':   :'zh-cn',
}

Instance Method Summary collapse

Constructor Details

#initializeDetectLanguageCom

Returns a new instance of DetectLanguageCom.



33
34
35
36
37
38
39
40
41
# File 'lib/opener/language_identifier/backend/detect_language_com.rb', line 33

def initialize
  DetectLanguage.configure do |config|
    config.secure  = true
    config.api_key = ENV['DETECT_LANGUAGE_TOKEN']
    config.http_read_timeout = 300
    config.http_open_timeout = 300
    raise 'no detectlanguage token specified' if config.api_key.nil?
  end
end

Instance Method Details

#detect(input) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/opener/language_identifier/backend/detect_language_com.rb', line 43

def detect input
  code = DetectLanguage.simple_detect input
  return 'unknown' unless code

  mapped_code = CODE_MAP[code.to_sym]
  return mapped_code.to_s if mapped_code

  code

rescue
  retry
end