embulk-filter-azure_translator_api 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 +5 -0
- data/.ruby-version +1 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +3 -0
- data/embulk-filter-azure_translator_api.gemspec +21 -0
- data/lib/embulk/filter/azure_translator_api.rb +73 -0
- data/lib/embulk/filter/azure_translator_api/azure_translator_client.rb +47 -0
- metadata +123 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: afa13d2b165a2a901773a492d11e0ef786245125
|
4
|
+
data.tar.gz: 4b8d2da4781f4bfcb20f66527d69f666522225ef
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0b0da7a63c4d1c61cec6bbec439c1de470c8ad84a662ac384897a91ee0b1ed9a2ea5dfeff24caba3a0fedb3d8a0e8fa33d9810bcb565749c21a8505881ee2105
|
7
|
+
data.tar.gz: 598e775bab15d78c62a3f5e8c27521fce63f9b2baf74bbb7b6b42a3780abf6fccb47d2c90af2ac265a1a517346d1c5bd74a9d4c38cd54873525cce8e88509f8d
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
jruby-9.1.5.0
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
MIT License
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Azure Translator Api filter plugin for Embulk
|
2
|
+
|
3
|
+
Write short description here and embulk-filter-azure_translator_api.gemspec file.
|
4
|
+
|
5
|
+
## Documentation
|
6
|
+
* [seamlessly](https://www.microsoft.com/cognitive-services/en-us/translator-api/documentation/Text/overview)
|
7
|
+
* [Microsoft Cognitive Services](http://docs.microsofttranslator.com/text-translate.html)
|
8
|
+
|
9
|
+
## Overview
|
10
|
+
|
11
|
+
* **Plugin type**: filter
|
12
|
+
|
13
|
+
## Configuration
|
14
|
+
- **api_type**: api_type (string),
|
15
|
+
- **out_key_name_suffix**: out_key_name_suffix (string),
|
16
|
+
- **key_names**: key_names (array),
|
17
|
+
- **params**: params (hash, default: {}),
|
18
|
+
- **delay**: delay (integer, default: 0),
|
19
|
+
- **subscription_key**: subscription_key (string),
|
20
|
+
- **content_type**: content_type (string, default: nil),
|
21
|
+
- **category**: category (string, default: nil),
|
22
|
+
- **to**: to (string),
|
23
|
+
- **from**: from (string, default: nil)
|
24
|
+
|
25
|
+
## Example
|
26
|
+
|
27
|
+
```yaml
|
28
|
+
filters:
|
29
|
+
- type: azure_translator_api
|
30
|
+
key_names:
|
31
|
+
- string_column
|
32
|
+
- string_column2
|
33
|
+
out_key_name_suffix: translated
|
34
|
+
subscription_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
35
|
+
api_type: Translate
|
36
|
+
to: ja
|
37
|
+
```
|
38
|
+
|
39
|
+
## Build
|
40
|
+
|
41
|
+
```
|
42
|
+
$ rake
|
43
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
Gem::Specification.new do |spec|
|
3
|
+
spec.name = "embulk-filter-azure_translator_api"
|
4
|
+
spec.version = "0.1.0"
|
5
|
+
spec.authors = ["toyama0919"]
|
6
|
+
spec.summary = "Azure Translator Api filter plugin for Embulk"
|
7
|
+
spec.description = "Azure Translator Api"
|
8
|
+
spec.email = ["toyama0919@gmail.com"]
|
9
|
+
spec.licenses = ["MIT"]
|
10
|
+
spec.homepage = "https://github.com/toyama0919/embulk-filter-azure_translator_api"
|
11
|
+
|
12
|
+
spec.files = `git ls-files`.split("\n") + Dir["classpath/*.jar"]
|
13
|
+
spec.test_files = spec.files.grep(%r{^(test|spec)/})
|
14
|
+
spec.require_paths = ["lib"]
|
15
|
+
|
16
|
+
spec.add_dependency 'rest-client'
|
17
|
+
spec.add_dependency 'crack'
|
18
|
+
spec.add_development_dependency 'embulk', ['>= 0.8.15']
|
19
|
+
spec.add_development_dependency 'bundler', ['>= 1.10.6']
|
20
|
+
spec.add_development_dependency 'rake', ['>= 10.0']
|
21
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require_relative 'azure_translator_api/azure_translator_client'
|
2
|
+
|
3
|
+
module Embulk
|
4
|
+
module Filter
|
5
|
+
|
6
|
+
class AzureTranslatorApi < FilterPlugin
|
7
|
+
Plugin.register_filter("azure_translator_api", self)
|
8
|
+
|
9
|
+
def self.transaction(config, in_schema, &control)
|
10
|
+
task = {
|
11
|
+
"api_type" => config.param("api_type", :string),
|
12
|
+
"out_key_name_suffix" => config.param("out_key_name_suffix", :string),
|
13
|
+
"key_names" => config.param("key_names", :array),
|
14
|
+
"params" => config.param("params", :hash, default: {}),
|
15
|
+
"delay" => config.param("delay", :integer, default: 0),
|
16
|
+
"subscription_key" => config.param("subscription_key", :string),
|
17
|
+
"content_type" => config.param("content_type", :string, default: nil),
|
18
|
+
"category" => config.param("category", :string, default: nil),
|
19
|
+
"to" => config.param("to", :string),
|
20
|
+
"from" => config.param("from", :string, default: nil)
|
21
|
+
}
|
22
|
+
|
23
|
+
add_columns = task['key_names'].map do |key_name|
|
24
|
+
Column.new(nil, key_name + task["out_key_name_suffix"], :string)
|
25
|
+
end
|
26
|
+
|
27
|
+
out_columns = in_schema + add_columns
|
28
|
+
task['authorization_token'] = AzureTranslatorClient.get_authorization_token(task["subscription_key"])
|
29
|
+
|
30
|
+
yield(task, out_columns)
|
31
|
+
end
|
32
|
+
|
33
|
+
def init
|
34
|
+
@delay = task['delay']
|
35
|
+
@key_names = task['key_names']
|
36
|
+
@out_key_name_suffix = task['out_key_name_suffix']
|
37
|
+
|
38
|
+
params = {
|
39
|
+
'to' => task['to'],
|
40
|
+
}
|
41
|
+
params['from'] = task['from'] if task['from']
|
42
|
+
params['content_type'] = task['content_type'] if task['content_type']
|
43
|
+
params['category'] = task['category'] if task['category']
|
44
|
+
@client = AzureTranslatorClient.new(
|
45
|
+
params: params,
|
46
|
+
authorization_token: task['authorization_token'],
|
47
|
+
api_type: task['api_type']
|
48
|
+
)
|
49
|
+
end
|
50
|
+
|
51
|
+
def close
|
52
|
+
end
|
53
|
+
|
54
|
+
def add(page)
|
55
|
+
records = page.map do |record|
|
56
|
+
Hash[in_schema.names.zip(record)]
|
57
|
+
end
|
58
|
+
|
59
|
+
records.each do |record|
|
60
|
+
@key_names.each do |key_name|
|
61
|
+
record[key_name + @out_key_name_suffix] = @client.translate_text(record[key_name])
|
62
|
+
end
|
63
|
+
page_builder.add(record.values)
|
64
|
+
sleep @delay
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def finish
|
69
|
+
page_builder.finish
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'crack/xml'
|
2
|
+
require 'rest-client'
|
3
|
+
|
4
|
+
module Embulk
|
5
|
+
module Filter
|
6
|
+
class AzureTranslatorApi < FilterPlugin
|
7
|
+
class AzureTranslatorClient
|
8
|
+
ENDPOINT_PREFIX = "https://api.microsofttranslator.com/v2/http.svc"
|
9
|
+
AUTH_ENDPOINT_PREFIX = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"
|
10
|
+
|
11
|
+
def self.get_authorization_token(subscription_key)
|
12
|
+
RestClient.post AUTH_ENDPOINT_PREFIX, "", {
|
13
|
+
params: { 'Subscription-Key' => subscription_key }
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(params: , authorization_token: , api_type:)
|
18
|
+
uri_string = "#{ENDPOINT_PREFIX}/#{api_type}"
|
19
|
+
@resource = RestClient::Resource.new uri_string
|
20
|
+
@params = params
|
21
|
+
@authorization_token = authorization_token
|
22
|
+
end
|
23
|
+
|
24
|
+
def translate_text(text)
|
25
|
+
translate(text)['string']
|
26
|
+
end
|
27
|
+
|
28
|
+
def translate(text)
|
29
|
+
request(text)
|
30
|
+
end
|
31
|
+
|
32
|
+
def request(text)
|
33
|
+
request_param = { text: text }.merge(@params)
|
34
|
+
Embulk.logger.debug("request_param => #{request_param}")
|
35
|
+
::Crack::XML.parse(
|
36
|
+
@resource.get(
|
37
|
+
{
|
38
|
+
Authorization: "Bearer #{@authorization_token}",
|
39
|
+
params: request_param
|
40
|
+
}
|
41
|
+
)
|
42
|
+
)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: embulk-filter-azure_translator_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- toyama0919
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
name: rest-client
|
20
|
+
prerelease: false
|
21
|
+
type: :runtime
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
name: crack
|
34
|
+
prerelease: false
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 0.8.15
|
47
|
+
name: embulk
|
48
|
+
prerelease: false
|
49
|
+
type: :development
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.8.15
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 1.10.6
|
61
|
+
name: bundler
|
62
|
+
prerelease: false
|
63
|
+
type: :development
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.10.6
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '10.0'
|
75
|
+
name: rake
|
76
|
+
prerelease: false
|
77
|
+
type: :development
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
description: Azure Translator Api
|
84
|
+
email:
|
85
|
+
- toyama0919@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".ruby-version"
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- embulk-filter-azure_translator_api.gemspec
|
97
|
+
- lib/embulk/filter/azure_translator_api.rb
|
98
|
+
- lib/embulk/filter/azure_translator_api/azure_translator_client.rb
|
99
|
+
homepage: https://github.com/toyama0919/embulk-filter-azure_translator_api
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
metadata: {}
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 2.6.6
|
120
|
+
signing_key:
|
121
|
+
specification_version: 4
|
122
|
+
summary: Azure Translator Api filter plugin for Embulk
|
123
|
+
test_files: []
|