watson-language-translator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5b39138220f6b7f4d9413acc03a87bc54cdb0f5a
4
+ data.tar.gz: 334855cff57183b0f1b215119aed7990b3001b3f
5
+ SHA512:
6
+ metadata.gz: d39a67c33231a13892fde4c5b59946bbd5e46108510c10d33cf334a18bdbe988d6a8eac656f48bbd60bbfe990373f347d6e14fa2a066f6ed89f43480ff2a059c
7
+ data.tar.gz: 3654e853ae39259cd5e1b9ae0e6128f4797173b635fab3f8452be05c737719bf8a31db66921896941cf1b120cc1d8b5d7e5d32b2877ec9d92977f3185a61a8e9
@@ -0,0 +1,123 @@
1
+ # watson-language-translator
2
+
3
+ Language Translator translates text from one language to another. The service offers multiple domain-specific models that you can customize based on your unique terminology and language. Use Language Translator to take news from across the globe and present it in your language, communicate with your customers in their own language, and more.
4
+
5
+ For more informations you can read [here](https://www.ibm.com/watson/developercloud/doc/language-translator/index.html)
6
+
7
+ ## Prerequisites
8
+
9
+ To get an API username and password, you'll need to [sign up for IBM Bluemix](https://console.ng.bluemix.net/registration/). After you create an account:
10
+
11
+ Log in to Bluemix and go to the AlchemyAPI service page.
12
+ Click the "Create" button.
13
+ Click the "Service Credentials" button from the Language Tranlator page in your Bluemix dashboard to view your API username and password.
14
+
15
+ You authenticate to the Language Translator API by providing the username and password that are provided in the service credentials for the service instance that you want to use. The API uses Basic Authentication.
16
+
17
+ After creating an instance of the Language Translator service, select Service Credentials from the left navigation for its dashboard to see the username and password that are associated with that instance.
18
+
19
+ Note: Service credentials ("username" and "password") are different from your Bluemix account username and password.
20
+
21
+ After create bluemix account and create username and passowrd credencials, you can use another gem for env variables as ([dotenv](https://github.com/bkeepers/dotenv), [figaro](https://github.com/laserlemon/figaro) or which you want to create env variables).
22
+
23
+ Then in (.env) file you should add:
24
+
25
+ ```ruby
26
+ language_translator_username=$your_username_from_bluemix
27
+ language_translator_password=$your_password_from_bluemix
28
+ ```
29
+
30
+ watson_langauge_translator will take your username and password and it will do request with this credencials, it's it!
31
+
32
+ ## Getting Started in ruby
33
+ You can install this library
34
+
35
+ `gem install 'watson-language-translator'`
36
+
37
+ and use
38
+
39
+ ```ruby
40
+ require 'watson-language-translator'
41
+ ```
42
+
43
+ ## Getting Started in Rails
44
+ AlchemyLanguage works with Rails > 4.1 onwards. You can add it to your Gemfile with:
45
+
46
+ ```ruby
47
+ gem 'rest-client'
48
+ gem 'watson-language-translator'
49
+ ```
50
+
51
+ Then run `bundle install`
52
+
53
+ ## Use
54
+
55
+ ### Models
56
+
57
+ All Services contain these models:
58
+
59
+ ```
60
+ .
61
+ ├── Translate
62
+ ├── IdentifiableLanguages
63
+ ├── IdentifyLanguage
64
+ ├── ListModels
65
+ ├── ModelDetails
66
+ ├── Model (create)
67
+ ├── Model (delete)
68
+
69
+ 7 models
70
+ ```
71
+
72
+ #### Translate
73
+
74
+ Translates input text from the source language to the target language.
75
+
76
+ ```ruby
77
+ @language_translate = WatsonLanguage::Translator.new("hello",
78
+ source: "en",
79
+ target: "es",
80
+ http_method: "post"
81
+ )
82
+
83
+ @language_translate.result # => {"translations"=>[{"translation"=>"Hola"}], "word_count"=>1, "character_count"=>5}
84
+ ```
85
+
86
+ #### Identifiable languages
87
+ Return the list of languages it can detect.
88
+
89
+ ```ruby
90
+ @identifiable_languages = WatsonLanguage::IdentifiableLanguages.fetch
91
+
92
+ @identifiable_languages.result # => { "languages": [ { "language": "af", "name": "Afrikaans"}, { "language": "ar", "name": "Arabic" }, ...] }
93
+ ```
94
+
95
+ #### Identify language
96
+ Identify the language in which a text is written.
97
+
98
+ ```ruby
99
+ @identify_language = WatsonLanguage::IdentifyLanguage.new("this is a text")
100
+
101
+ @identify_language.result # => { "languages": [ { "confidence": 0.9143, "language": "en-US" }, { "confidence": 0.0396, "language": "hu-HU" }, //... ] }
102
+ ```
103
+
104
+ #### List Models
105
+ Lists available models for the Language translator service with option to filter by source or by target language.
106
+
107
+ ```ruby
108
+ @list_models = WatsonLanguage::ListModels.fetch
109
+
110
+ @list_models.result # => { "models": [ { "model_id ": "3e7dfdbe-f757-4150-afee-458e71eb93fb", "source": "en", "target": "es", "base_model_id": "en-es",.. }, //... ] }
111
+ ```
112
+ #### Model Details
113
+ Returns information, including training status, about a specified translation model.
114
+
115
+ ```ruby
116
+ @model_details = WatsonLanguage::ModelDetails.fetch(model_id: "en-es")
117
+
118
+ @model_details.result # => { "models": [ { "model_id ": "3e7dfdbe-f757-4150-afee-458e71eb93fb", "source": "en", "target": "es", "base_model_id": "en-es",.. }, //... ] }
119
+ ```
120
+
121
+ ## License
122
+
123
+ This project is licensed under the MIT License
@@ -0,0 +1,29 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+ require "addressable/uri"
4
+
5
+ module WatsonLanguage
6
+ class << self
7
+ def base_url
8
+ "https://gateway.watsonplatform.net/language-translator/api/v2"
9
+ end
10
+
11
+ def username
12
+ ENV['language_translator_username']
13
+ end
14
+
15
+ def password
16
+ ENV['langauge_translator_password']
17
+ end
18
+ end
19
+ end
20
+
21
+ require_relative "watson-language-translator/generator/language_request.rb"
22
+ require_relative "watson-language-translator/active_method/extra.rb"
23
+ require_relative "watson-language-translator/active_method/base.rb"
24
+ require_relative "watson-language-translator/translator.rb"
25
+ require_relative "watson-language-translator/identifiable_languages.rb"
26
+ require_relative "watson-language-translator/identify.rb"
27
+ require_relative "watson-language-translator/list_models.rb"
28
+ require_relative "watson-language-translator/model_details.rb"
29
+ require_relative "watson-language-translator/model.rb"
@@ -0,0 +1,37 @@
1
+ module WatsonLanguage
2
+ module ActiveMethod
3
+ class Base < ActiveMethod::Extra
4
+ include WatsonLanguage::Generators::LanguageRequest
5
+ attr_accessor :text, :username, :password, :json_result, :options, :http_method
6
+
7
+ def initialize(text = nil, options={})
8
+ @text = text
9
+ @username = WatsonLanguage.username
10
+ @password = WatsonLanguage.password
11
+ @options = options
12
+ @http_method = fetch_http_method
13
+ @json_result = request
14
+ end
15
+
16
+ def request
17
+ json_parser(endpoint)
18
+ end
19
+
20
+ def params_addressable
21
+ uri = Addressable::URI.new
22
+ uri.query_values = @options
23
+ uri.query
24
+ end
25
+
26
+ def fetch_http_method
27
+ if @options[:http_method]
28
+ result = options[:http_method]
29
+ @options.delete(:http_method)
30
+ else
31
+ result = "get"
32
+ end
33
+ return result.to_sym
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,15 @@
1
+ module WatsonLanguage
2
+ module ActiveMethod
3
+ class Extra
4
+ def self.add_response_field(name)
5
+ define_method(name) do
6
+ @json_result[name.to_s]
7
+ end
8
+ end
9
+
10
+ def result
11
+ @json_result
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ module WatsonLanguage
2
+ module Generators
3
+ module LanguageRequest
4
+ def base_url_request
5
+ "#{WatsonLanguage.base_url}"
6
+ end
7
+
8
+ def rest_client_api(request_api_url)
9
+ RestClient::Request.execute(
10
+ method: @http_method,
11
+ url: request_api_url,
12
+ user: @username,
13
+ password: @password,
14
+ :headers => {:accept => :json}
15
+ )
16
+ end
17
+
18
+ def json_parser(url)
19
+ JSON.parse(rest_client_api(base_url_request + "/" + url))
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ module WatsonLanguage
2
+ class IdentifiableLanguages < ActiveMethod::Base
3
+ add_response_field :languages
4
+
5
+ def self.fetch
6
+ self.new
7
+ end
8
+
9
+ def endpoint
10
+ "identifiable_languages"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module WatsonLanguage
2
+ class Identify < ActiveMethod::Base
3
+ add_response_field :languages
4
+
5
+ def endpoint
6
+ url = "identify"
7
+ url << "?text=#{@text}"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ module WatsonLanguage
2
+ class ListModels < ActiveMethod::Base
3
+ add_response_field :models
4
+
5
+ def self.fetch
6
+ self.new
7
+ end
8
+
9
+ def endpoint
10
+ "models"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ module WatsonLanguage
2
+ class Model < ActiveMethod::Base
3
+ add_response_field :model_id
4
+ add_response_field :deleted
5
+
6
+ def self.create(options={})
7
+ self.new(nil, options)
8
+ end
9
+
10
+ def self.delete(options={})
11
+ self.new(nil, options)
12
+ end
13
+
14
+ def endpoint
15
+ return url = "models/#{@options[:model_id]}" if @options[:model_id]
16
+ url = "models"
17
+ url << "&#{params_addressable}" unless params_addressable.empty?
18
+ url
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ module WatsonLanguage
2
+ class ModelDetails < ActiveMethod::Base
3
+ add_response_field :models
4
+
5
+ def self.fetch(options={})
6
+ self.new(nil, options)
7
+ end
8
+
9
+ def endpoint
10
+ "models/#{@options[:model_id]}"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ module WatsonLanguage
2
+ class Translator < ActiveMethod::Base
3
+ add_response_field :translations
4
+ add_response_field :word_count
5
+ add_response_field :character_count
6
+
7
+ def endpoint
8
+ url = "translate"
9
+ url << "?text=#{@text}"
10
+ url << "&#{params_addressable}" unless params_addressable.empty?
11
+ url
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module WatsonLanguage
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: watson-language-translator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Diamant
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Watson Language Translate content into multiple languages
56
+ email:
57
+ - diamantkolshi@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - lib/watson-language-translator.rb
64
+ - lib/watson-language-translator/active_method/base.rb
65
+ - lib/watson-language-translator/active_method/extra.rb
66
+ - lib/watson-language-translator/generator/language_request.rb
67
+ - lib/watson-language-translator/identifiable_languages.rb
68
+ - lib/watson-language-translator/identify.rb
69
+ - lib/watson-language-translator/list_models.rb
70
+ - lib/watson-language-translator/model.rb
71
+ - lib/watson-language-translator/model_details.rb
72
+ - lib/watson-language-translator/translator.rb
73
+ - lib/watson-language-translator/version.rb
74
+ homepage: https://github.com/diamantkolshi/watson-language-translator
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.6.10
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Watson Language!
98
+ test_files: []