module EasyTranslate::Detection

Public Instance Methods

detect(texts, options = {}, http_options = {}) click to toggle source

Detect language @param [String, Array] texts - A single string or set of strings to detect for @param [Hash] options - Extra options to pass along with the request @return [String, Array] The resultant language or languages

# File lib/easy_translate/detection.rb, line 15
def detect(texts, options = {}, http_options = {})
  threaded_process(:request_detection, texts, options, http_options)
end

Private Instance Methods

request_detection(texts, options, http_options) click to toggle source
# File lib/easy_translate/detection.rb, line 20
def request_detection(texts, options, http_options)
  request = DetectionRequest.new(texts, options, http_options)
  raw = request.perform_raw
  detections = JSON.parse(raw)['data']['detections'].map do |res|
    res.empty? ? nil : 
      options[:confidence] ? 
        { :language => res.first['language'], :confidence => res.first['confidence'] } : res.first['language']
  end
end