class Google::Cloud::Translate::V2::Detection

# Detection

Represents a detect language query result. Returned by {Google::Cloud::Translate::V2::Api#detect}.

@see cloud.google.com/translation/docs/detecting-language Detecting Language

@example

require "google/cloud/translate/v2"

translate = Google::Cloud::Translate::V2.new

detections = translate.detect "chien", "chat"

detections.size #=> 2
detections[0].text #=> "chien"
detections[0].language #=> "fr"
detections[0].confidence #=> 0.7109375
detections[1].text #=> "chat"
detections[1].language #=> "en"
detections[1].confidence #=> 0.59922177

Attributes

results[R]

The list of detection results for the given text. The most likely language is listed first, and its attributes can be accessed through {#language} and {#confidence}.

@return [Array<Detection::Result>]

text[R]

The text upon which the language detection was performed.

@return [String]

Public Class Methods

from_gapi(gapi, text) click to toggle source

@private New Detection from a ListDetectionsResponse object as defined by the Google API Client object.

# File lib/google/cloud/translate/v2/detection.rb, line 85
def self.from_gapi gapi, text
  res = text.zip(Array(gapi["detections"])).map do |txt, detections|
    results = detections.map { |g| Result.from_gapi g }
    new txt, results
  end
  return res.first if res.size == 1
  res
end
new(text, results) click to toggle source

@private Create a new object.

# File lib/google/cloud/translate/v2/detection.rb, line 58
def initialize text, results
  @text = text
  @results = results
end

Public Instance Methods

confidence() click to toggle source

The confidence that the language detection result is correct. The closer this value is to 1, the higher the confidence in language detection.

@return [Float] a value between 0 and 1

# File lib/google/cloud/translate/v2/detection.rb, line 68
def confidence
  return nil if results.empty?
  results.first.confidence
end
language() click to toggle source

The most likely language that was detected. This is an [ISO 639-1](en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code.

@return [String] the language code

# File lib/google/cloud/translate/v2/detection.rb, line 78
def language
  return nil if results.empty?
  results.first.language
end