translator-text 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +17 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.md +11 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +109 -0
- data/LICENSE.txt +21 -0
- data/README.md +93 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/translator-text.rb +20 -0
- data/lib/translator_text/client.rb +112 -0
- data/lib/translator_text/exceptions.rb +20 -0
- data/lib/translator_text/types.rb +34 -0
- data/lib/translator_text/types/alternative.rb +14 -0
- data/lib/translator_text/types/detection_result.rb +16 -0
- data/lib/translator_text/types/sentence.rb +11 -0
- data/lib/translator_text/types/translation.rb +12 -0
- data/lib/translator_text/types/translation_result.rb +21 -0
- data/lib/translator_text/version.rb +3 -0
- data/translator-text.gemspec +26 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2a23c9076d2e91225a46124d809eb0acf0b3513b
|
4
|
+
data.tar.gz: ef82b1270172a0ef04f5b83b5e15317b5de61383
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4552b5d7a357a5f013a0a3a36aaf9ee9d63b54a42ce3f92451ddc312e6a8a52ed239250f4289c2070b346c11a1fe43e747fe5887c08e43113352e51ad7417d6a
|
7
|
+
data.tar.gz: 44ff8cb26e0d4ff5f99a9f52276847cc7fbd7e31c40cb1e6a604f996227e607d8b385c0f0bd2948d90c0be5455d72e8a24d13cf9d8478ca343d03a0139e160ae
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# Changelog
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
5
|
+
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
6
|
+
|
7
|
+
## 0.1.0 (2018-08-28)
|
8
|
+
### Added
|
9
|
+
- Initial release
|
10
|
+
- Support of <translation> service
|
11
|
+
- Support of <detect> service
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at aymeric.brisse@perfect-memory.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# git_source(:github) {|repo_name| 'https://github.com/#{repo_name}' }
|
4
|
+
|
5
|
+
group :test do
|
6
|
+
gem 'bundler', '~> 1.16'
|
7
|
+
gem 'dotenv'
|
8
|
+
gem 'rake', '~> 10.0'
|
9
|
+
gem 'rspec', '~> 3.0'
|
10
|
+
gem 'rubocop'
|
11
|
+
gem 'simplecov', require: false
|
12
|
+
gem 'webmock'
|
13
|
+
gem 'yard'
|
14
|
+
end
|
15
|
+
|
16
|
+
# Specify your gem's dependencies in ms-translator-text.gemspec
|
17
|
+
gemspec
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
translator-text (0.1.0)
|
5
|
+
dry-struct (~> 0.5.0)
|
6
|
+
httparty (~> 0.15)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
addressable (2.5.2)
|
12
|
+
public_suffix (>= 2.0.2, < 4.0)
|
13
|
+
ast (2.4.0)
|
14
|
+
concurrent-ruby (1.0.5)
|
15
|
+
crack (0.4.3)
|
16
|
+
safe_yaml (~> 1.0.0)
|
17
|
+
diff-lcs (1.3)
|
18
|
+
docile (1.3.1)
|
19
|
+
dotenv (2.5.0)
|
20
|
+
dry-configurable (0.7.0)
|
21
|
+
concurrent-ruby (~> 1.0)
|
22
|
+
dry-container (0.6.0)
|
23
|
+
concurrent-ruby (~> 1.0)
|
24
|
+
dry-configurable (~> 0.1, >= 0.1.3)
|
25
|
+
dry-core (0.4.7)
|
26
|
+
concurrent-ruby (~> 1.0)
|
27
|
+
dry-equalizer (0.2.1)
|
28
|
+
dry-inflector (0.1.2)
|
29
|
+
dry-logic (0.4.2)
|
30
|
+
dry-container (~> 0.2, >= 0.2.6)
|
31
|
+
dry-core (~> 0.2)
|
32
|
+
dry-equalizer (~> 0.2)
|
33
|
+
dry-struct (0.5.1)
|
34
|
+
dry-core (~> 0.4, >= 0.4.3)
|
35
|
+
dry-equalizer (~> 0.2)
|
36
|
+
dry-types (~> 0.13)
|
37
|
+
ice_nine (~> 0.11)
|
38
|
+
dry-types (0.13.2)
|
39
|
+
concurrent-ruby (~> 1.0)
|
40
|
+
dry-container (~> 0.3)
|
41
|
+
dry-core (~> 0.4, >= 0.4.4)
|
42
|
+
dry-equalizer (~> 0.2)
|
43
|
+
dry-inflector (~> 0.1, >= 0.1.2)
|
44
|
+
dry-logic (~> 0.4, >= 0.4.2)
|
45
|
+
hashdiff (0.3.7)
|
46
|
+
httparty (0.16.2)
|
47
|
+
multi_xml (>= 0.5.2)
|
48
|
+
ice_nine (0.11.2)
|
49
|
+
jaro_winkler (1.5.1)
|
50
|
+
json (2.1.0)
|
51
|
+
multi_xml (0.6.0)
|
52
|
+
parallel (1.12.1)
|
53
|
+
parser (2.5.1.2)
|
54
|
+
ast (~> 2.4.0)
|
55
|
+
powerpack (0.1.2)
|
56
|
+
public_suffix (3.0.3)
|
57
|
+
rainbow (3.0.0)
|
58
|
+
rake (10.5.0)
|
59
|
+
rspec (3.8.0)
|
60
|
+
rspec-core (~> 3.8.0)
|
61
|
+
rspec-expectations (~> 3.8.0)
|
62
|
+
rspec-mocks (~> 3.8.0)
|
63
|
+
rspec-core (3.8.0)
|
64
|
+
rspec-support (~> 3.8.0)
|
65
|
+
rspec-expectations (3.8.1)
|
66
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
67
|
+
rspec-support (~> 3.8.0)
|
68
|
+
rspec-mocks (3.8.0)
|
69
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
70
|
+
rspec-support (~> 3.8.0)
|
71
|
+
rspec-support (3.8.0)
|
72
|
+
rubocop (0.58.2)
|
73
|
+
jaro_winkler (~> 1.5.1)
|
74
|
+
parallel (~> 1.10)
|
75
|
+
parser (>= 2.5, != 2.5.1.1)
|
76
|
+
powerpack (~> 0.1)
|
77
|
+
rainbow (>= 2.2.2, < 4.0)
|
78
|
+
ruby-progressbar (~> 1.7)
|
79
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
80
|
+
ruby-progressbar (1.10.0)
|
81
|
+
safe_yaml (1.0.4)
|
82
|
+
simplecov (0.16.1)
|
83
|
+
docile (~> 1.1)
|
84
|
+
json (>= 1.8, < 3)
|
85
|
+
simplecov-html (~> 0.10.0)
|
86
|
+
simplecov-html (0.10.2)
|
87
|
+
unicode-display_width (1.4.0)
|
88
|
+
webmock (3.4.2)
|
89
|
+
addressable (>= 2.3.6)
|
90
|
+
crack (>= 0.3.2)
|
91
|
+
hashdiff
|
92
|
+
yard (0.9.16)
|
93
|
+
|
94
|
+
PLATFORMS
|
95
|
+
ruby
|
96
|
+
|
97
|
+
DEPENDENCIES
|
98
|
+
bundler (~> 1.16)
|
99
|
+
dotenv
|
100
|
+
rake (~> 10.0)
|
101
|
+
rspec (~> 3.0)
|
102
|
+
rubocop
|
103
|
+
simplecov
|
104
|
+
translator-text!
|
105
|
+
webmock
|
106
|
+
yard
|
107
|
+
|
108
|
+
BUNDLED WITH
|
109
|
+
1.16.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Perfect Memory
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# TranslatorText
|
2
|
+
|
3
|
+
[![Gem](https://img.shields.io/gem/v/translator-text.svg)](https://rubygems.org/gems/translator-text/) [![Code Climate](https://codeclimate.com/github/PerfectMemory/translator-text.png)](https://codeclimate.com/github/PerfectMemory/translator-text)
|
4
|
+
|
5
|
+
API Wrapper for the [Microsoft Translator Text API 3.0](https://docs.microsoft.com/en-us/azure/cognitive-services/translator/reference/v3-0-reference) (Cognitive Services).
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'translator-text'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install translator-text
|
22
|
+
|
23
|
+
## Cognitive Services API Key
|
24
|
+
|
25
|
+
* To be able to use the Cognitive Services, you must have an API key and for that you need an Azure account.
|
26
|
+
* If you don't have an Azure account, you can create one for free on [this page](https://azure.microsoft.com/en-us/free/).
|
27
|
+
* Once logged on your [Azure portal](https://portal.azure.com/), subscribe to the Translator Text API in Microsoft Cognitive Service.
|
28
|
+
* You will find two subscription keys available in 'RESOURCE MANAGEMENT > Keys' ('KEY 1' and 'KEY 2').
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
### Translation
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require 'translator-text'
|
36
|
+
|
37
|
+
api_key = ENV.fetch('COGNITIVE_SUBSCRIPTION_KEY')
|
38
|
+
client = TranslatorText::Client.new(api_key)
|
39
|
+
|
40
|
+
results = client.translate(
|
41
|
+
['something to translate', 'another something to translate'],
|
42
|
+
to: [:fr, :es]
|
43
|
+
)
|
44
|
+
|
45
|
+
# Returns
|
46
|
+
# [
|
47
|
+
# #<TranslationResult detectedLanguage={"language"=>"en", "score"=>1.0} translations=[
|
48
|
+
# #<Translation text="Première phrase" to="fr">,
|
49
|
+
# #<Translation text="Primera oración" to="es">]>,
|
50
|
+
# #<TranslationResult detectedLanguage={"language"=>"en", "score"=>1.0} translations=[
|
51
|
+
# #<Translation text="Deuxième phrase" to="fr">,
|
52
|
+
# #<Translation text="Segunda oración" to="es">]>
|
53
|
+
# ]
|
54
|
+
```
|
55
|
+
|
56
|
+
### Detection
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
require 'translator-text'
|
60
|
+
|
61
|
+
api_key = ENV.fetch('COGNITIVE_SUBSCRIPTION_KEY')
|
62
|
+
client = TranslatorText::Client.new(api_key)
|
63
|
+
|
64
|
+
results = client.detect(['some unknown language', 'quelque chose à détecter'])
|
65
|
+
|
66
|
+
# Returns
|
67
|
+
# [
|
68
|
+
# #<DetectionResult language=:en score=1.0 isTranslationSupported=true isTransliterationSupported=false alternatives=[
|
69
|
+
# #<Alternative language=:de score=1.0 isTranslationSupported=true isTransliterationSupported=false>,
|
70
|
+
# #<Alternative language=:"fil-PH" score=1.0 isTranslationSupported=true isTransliterationSupported=false>]>,
|
71
|
+
# #<DetectionResult language=:fr score=1.0 isTranslationSupported=true isTransliterationSupported=false alternatives=[
|
72
|
+
# #<Alternative language=:nl score=0.75 isTranslationSupported=true isTransliterationSupported=false>,
|
73
|
+
# #<Alternative language=:vi score=0.75 isTranslationSupported=true isTransliterationSupported=false>]>
|
74
|
+
# ]
|
75
|
+
```
|
76
|
+
|
77
|
+
## Development
|
78
|
+
|
79
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
80
|
+
|
81
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
|
85
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/PerfectMemory/translator-text. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
86
|
+
|
87
|
+
## License
|
88
|
+
|
89
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
90
|
+
|
91
|
+
## Code of Conduct
|
92
|
+
|
93
|
+
Everyone interacting in the TranslatorText project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/PerfectMemory/translator-text/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'translator-text'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative 'translator_text/version'
|
2
|
+
require_relative 'translator_text/exceptions'
|
3
|
+
require_relative 'translator_text/client'
|
4
|
+
require_relative 'translator_text/types'
|
5
|
+
|
6
|
+
module TranslatorText
|
7
|
+
class << self
|
8
|
+
attr_writer :client
|
9
|
+
|
10
|
+
def client
|
11
|
+
@client ||= build_client
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def build_client
|
17
|
+
TranslatorText::Client.new(ENV.fetch('COGNITIVE_SUBSCRIPTION_KEY'))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'securerandom'
|
3
|
+
require 'httparty'
|
4
|
+
|
5
|
+
module TranslatorText
|
6
|
+
class Client
|
7
|
+
include HTTParty
|
8
|
+
format :json
|
9
|
+
|
10
|
+
base_uri 'https://api.cognitive.microsofttranslator.com'.freeze
|
11
|
+
API_VERSION = '3.0'.freeze
|
12
|
+
|
13
|
+
# Initialize the client
|
14
|
+
# @since 1.0.0
|
15
|
+
#
|
16
|
+
# @param api_key [String] the Cognitive Services API Key
|
17
|
+
def initialize(api_key)
|
18
|
+
@api_key = api_key
|
19
|
+
end
|
20
|
+
|
21
|
+
# Translate a group of sentences.
|
22
|
+
#
|
23
|
+
# The following limitations apply:
|
24
|
+
# * The array _sentences_ can have at most 25 elements.
|
25
|
+
# * The entire text included in the request cannot exceed 5,000 characters including spaces.
|
26
|
+
# @see https://docs.microsoft.com/en-us/azure/cognitive-services/translator/reference/v3-0-translate
|
27
|
+
#
|
28
|
+
# @param sentences [Array<String, TranslatorText::Types::Sentence>] the sentences to process
|
29
|
+
# @param to [Symbol] Specifies the language of the output text (required)
|
30
|
+
# @param options [Hash] the optional options to transmit to the service. Consult the API documentation for the exhaustive available options.
|
31
|
+
# @return [Array<TranslatorText::Types::TranslationResult>] the translation results
|
32
|
+
def translate(sentences, to:, **options)
|
33
|
+
results = post(
|
34
|
+
'/translate',
|
35
|
+
body: build_sentences(sentences).to_json,
|
36
|
+
query: Hash[to: to, **options]
|
37
|
+
)
|
38
|
+
|
39
|
+
results.map { |r| Types::TranslationResult.new(r) }
|
40
|
+
end
|
41
|
+
|
42
|
+
# Identifies the language of a piece of text.
|
43
|
+
#
|
44
|
+
# The following limitations apply:
|
45
|
+
# * The array _sentences_ can have at most 100 elements.
|
46
|
+
# * The text value of an array element cannot exceed 10,000 characters including spaces.
|
47
|
+
# * The entire text included in the request cannot exceed 50,000 characters including spaces.
|
48
|
+
# @see https://docs.microsoft.com/en-us/azure/cognitive-services/translator/reference/v3-0-detect
|
49
|
+
#
|
50
|
+
# @param sentences [Array<String, TranslatorText::Types::Sentence>] the sentences to process
|
51
|
+
# @return [Array<TranslatorText::Types::DetectionResult>] the detection results
|
52
|
+
def detect(sentences)
|
53
|
+
results = post(
|
54
|
+
'/detect',
|
55
|
+
body: build_sentences(sentences).to_json
|
56
|
+
)
|
57
|
+
|
58
|
+
results.map { |r| Types::DetectionResult.new(r) }
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def build_sentences(sentences)
|
64
|
+
sentences.map do |sentence|
|
65
|
+
Types::Sentence(sentence)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def post(path, params)
|
70
|
+
options = {
|
71
|
+
headers: headers,
|
72
|
+
query: {}
|
73
|
+
}.merge(params)
|
74
|
+
|
75
|
+
options[:query][:'api-version'] = API_VERSION
|
76
|
+
|
77
|
+
response = self.class.post(path, options)
|
78
|
+
handle_response(response)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Handle the response
|
82
|
+
#
|
83
|
+
# If success, return the response body
|
84
|
+
# If failure, raise an error
|
85
|
+
def handle_response(response)
|
86
|
+
case response.code
|
87
|
+
when 200..299
|
88
|
+
response
|
89
|
+
else
|
90
|
+
if response.request.format == :json
|
91
|
+
raise ServiceError.new(
|
92
|
+
code: response['error']['code'],
|
93
|
+
message: response['error']['message']
|
94
|
+
)
|
95
|
+
else
|
96
|
+
raise NetError.new(
|
97
|
+
code: response.code,
|
98
|
+
message: response.response.message
|
99
|
+
)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def headers
|
105
|
+
{
|
106
|
+
'Ocp-Apim-Subscription-Key' => @api_key,
|
107
|
+
'X-ClientTraceId' => SecureRandom.uuid,
|
108
|
+
'Content-type' => 'application/json'
|
109
|
+
}
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module TranslatorText
|
2
|
+
class Error < StandardError
|
3
|
+
attr_reader :code, :message, :errors
|
4
|
+
|
5
|
+
def initialize(code:, message:)
|
6
|
+
@code = code
|
7
|
+
@message = message
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_s
|
11
|
+
"#{message} (#{code})"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class ServiceError < Error
|
16
|
+
end
|
17
|
+
|
18
|
+
class NetError < Error
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'dry-struct'
|
2
|
+
|
3
|
+
module TranslatorText
|
4
|
+
# Includes all the types for the exchanges with the API
|
5
|
+
module Types
|
6
|
+
include Dry::Types.module
|
7
|
+
|
8
|
+
Symbol = Types::Symbol.constructor do |str|
|
9
|
+
str ? str.to_sym : str
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def Sentence(item)
|
14
|
+
if item.is_a?(Types::Sentence)
|
15
|
+
item
|
16
|
+
elsif item.is_a?(String)
|
17
|
+
Types::Sentence.new(Text: item)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module JSON
|
23
|
+
def to_json(*args)
|
24
|
+
to_hash.to_json(*args)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
require_relative 'types/sentence'
|
31
|
+
require_relative 'types/translation'
|
32
|
+
require_relative 'types/translation_result'
|
33
|
+
require_relative 'types/alternative'
|
34
|
+
require_relative 'types/detection_result'
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module TranslatorText
|
2
|
+
module Types
|
3
|
+
class Alternative < Dry::Struct
|
4
|
+
include Types::JSON
|
5
|
+
|
6
|
+
transform_keys(&:to_sym)
|
7
|
+
|
8
|
+
attribute :language, Types::Symbol
|
9
|
+
attribute :score, Types::Float
|
10
|
+
attribute :isTranslationSupported, Types::Bool
|
11
|
+
attribute :isTransliterationSupported, Types::Bool
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module TranslatorText
|
2
|
+
module Types
|
3
|
+
class DetectionResult < Dry::Struct
|
4
|
+
include Types::JSON
|
5
|
+
|
6
|
+
transform_keys(&:to_sym)
|
7
|
+
|
8
|
+
attribute :language, Types::Symbol
|
9
|
+
attribute :score, Types::Float
|
10
|
+
attribute :isTranslationSupported, Types::Bool
|
11
|
+
attribute :isTransliterationSupported, Types::Bool
|
12
|
+
|
13
|
+
attribute :alternatives, Types::Array.of(Alternative)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module TranslatorText
|
2
|
+
module Types
|
3
|
+
class TranslationResult < Dry::Struct
|
4
|
+
include Types::JSON
|
5
|
+
|
6
|
+
transform_keys(&:to_sym)
|
7
|
+
|
8
|
+
attribute :translations, Types::Array.of(Translation)
|
9
|
+
attribute :detectedLanguage, Types::Hash.default({})
|
10
|
+
|
11
|
+
# Returns the detected language
|
12
|
+
#
|
13
|
+
# @return [Symbol] the language
|
14
|
+
def detected_language
|
15
|
+
if h = detectedLanguage['language']
|
16
|
+
h.to_sym
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'translator_text/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'translator-text'
|
7
|
+
spec.version = TranslatorText::VERSION
|
8
|
+
spec.authors = ['Aymeric Brisse']
|
9
|
+
spec.email = ['aymeric.brisse@perfect-memory.com']
|
10
|
+
|
11
|
+
spec.summary = 'API Wrapper for the Microsoft Translator Text API'
|
12
|
+
spec.description = 'API Wrapper for the Microsoft Translator Text API (Cognitive Services)'
|
13
|
+
spec.homepage = 'https://github.com/PerfectMemory/translator-text'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_dependency 'dry-struct', '~> 0.5.0'
|
25
|
+
spec.add_dependency 'httparty', '~> 0.15'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: translator-text
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aymeric Brisse
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-08-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-struct
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.5.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.5.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: httparty
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.15'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.15'
|
41
|
+
description: API Wrapper for the Microsoft Translator Text API (Cognitive Services)
|
42
|
+
email:
|
43
|
+
- aymeric.brisse@perfect-memory.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- ".rubocop.yml"
|
51
|
+
- ".travis.yml"
|
52
|
+
- CHANGELOG.md
|
53
|
+
- CODE_OF_CONDUCT.md
|
54
|
+
- Gemfile
|
55
|
+
- Gemfile.lock
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- bin/console
|
60
|
+
- bin/setup
|
61
|
+
- lib/translator-text.rb
|
62
|
+
- lib/translator_text/client.rb
|
63
|
+
- lib/translator_text/exceptions.rb
|
64
|
+
- lib/translator_text/types.rb
|
65
|
+
- lib/translator_text/types/alternative.rb
|
66
|
+
- lib/translator_text/types/detection_result.rb
|
67
|
+
- lib/translator_text/types/sentence.rb
|
68
|
+
- lib/translator_text/types/translation.rb
|
69
|
+
- lib/translator_text/types/translation_result.rb
|
70
|
+
- lib/translator_text/version.rb
|
71
|
+
- translator-text.gemspec
|
72
|
+
homepage: https://github.com/PerfectMemory/translator-text
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.6.14.1
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: API Wrapper for the Microsoft Translator Text API
|
96
|
+
test_files: []
|