Class: TranslatorText::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/translator_text/client.rb

Constant Summary collapse

API_VERSION =
'3.0'

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_region = nil) ⇒ Client

Initialize the client

Parameters:

  • api_key (String)

    the Cognitive Services API Key

  • api_region (String) (defaults to: nil)

    the Cognitive Services API Region

Since:

  • 1.0.0



26
27
28
29
# File 'lib/translator_text/client.rb', line 26

def initialize(api_key, api_region = nil)
  @api_key = api_key
  @api_region = api_region
end

Instance Method Details

#detect(sentences) ⇒ Array<TranslatorText::Types::DetectionResult>

Identifies the language of a piece of text.

The following limitations apply:

  • The array sentences can have at most 100 elements.

  • The text value of an array element cannot exceed 10,000 characters including spaces.

  • The entire text included in the request cannot exceed 50,000 characters including spaces.

Parameters:

Returns:

See Also:



62
63
64
65
66
67
68
69
# File 'lib/translator_text/client.rb', line 62

def detect(sentences)
  results = post(
    '/detect',
    body: build_sentences(sentences).to_json
  )

  results.map { |r| Types::DetectionResult.new(r) }
end

#translate(sentences, to:, **options) ⇒ Array<TranslatorText::Types::TranslationResult>

Translate a group of sentences.

The following limitations apply:

  • The array sentences can have at most 25 elements.

  • The entire text included in the request cannot exceed 5,000 characters including spaces.

Parameters:

  • sentences (Array<String, TranslatorText::Types::Sentence>)

    the sentences to process

  • to (Symbol)

    Specifies the language of the output text (required)

  • options (Hash)

    the optional options to transmit to the service. Consult the API documentation for the exhaustive available options.

Returns:

See Also:



42
43
44
45
46
47
48
49
50
# File 'lib/translator_text/client.rb', line 42

def translate(sentences, to:, **options)
  results = post(
    '/translate',
    body: build_sentences(sentences).to_json,
    query: { to:, **options }
  )

  results.map { |r| Types::TranslationResult.new(r) }
end