Class: Gcloud::Translate::Detection

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/translate/detection.rb

Overview

# Detection

Represents a detect language query result. Returned by Api#detect.

Examples:

require "gcloud"

gcloud = Gcloud.new
translate = gcloud.translate

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

See Also:

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, results) ⇒ Detection



60
61
62
63
# File 'lib/gcloud/translate/detection.rb', line 60

def initialize text, results
  @text = text
  @results = results
end

Instance Attribute Details

#resultsArray<Detection::Result> (readonly)

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.



56
57
58
# File 'lib/gcloud/translate/detection.rb', line 56

def results
  @results
end

#textString (readonly)

The text upon which the language detection was performed.



48
49
50
# File 'lib/gcloud/translate/detection.rb', line 48

def text
  @text
end

Class Method Details

.from_gapi(gapi, text) ⇒ Object

defined by the Google API Client object.



89
90
91
92
93
94
95
96
# File 'lib/gcloud/translate/detection.rb', line 89

def self.from_gapi gapi, text
  res = text.zip(Array(gapi.detections)).map do |txt, detections_gapi|
    results = detections_gapi.map { |g| Result.from_gapi g }
    new txt, results
  end
  return res.first if res.size == 1
  res
end

Instance Method Details

#confidenceFloat

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



70
71
72
73
# File 'lib/gcloud/translate/detection.rb', line 70

def confidence
  return nil if results.empty?
  results.first.confidence
end

#languageString

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.



81
82
83
84
# File 'lib/gcloud/translate/detection.rb', line 81

def language
  return nil if results.empty?
  results.first.language
end