Module: EasyTranslate::Detection

Included in:
EasyTranslate
Defined in:
lib/easy_translate/detection.rb

Defined Under Namespace

Classes: DetectionRequest

Instance Method Summary collapse

Instance Method Details

#detect(texts, options = nil, http_options = {}) ⇒ String, Array

Detect language

Parameters:

  • texts (String, Array)
    • A single string or set of strings to detect for

  • options (Hash) (defaults to: nil)
    • Extra options to pass along with the request

Returns:

  • (String, Array)

    The resultant language or languages



12
13
14
15
16
17
18
19
20
21
# File 'lib/easy_translate/detection.rb', line 12

def detect(texts, options = nil, http_options={})
  request = DetectionRequest.new(texts, options, http_options)
  # Turn the response into an array of detections
  raw = request.perform_raw
  detections = JSON.parse(raw)['data']['detections'].map do |res|
    res.empty? ? nil : res.first['language']
  end
  # And then return, if they only asked for one, only give one back
  request.multi? ? detections : detections.first
end