ms_paraphrase 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.idea/.name +1 -0
- data/.idea/.rakeTasks +7 -0
- data/.idea/encodings.xml +5 -0
- data/.idea/misc.xml +5 -0
- data/.idea/modules.xml +9 -0
- data/.idea/ms_paraphrase.iml +197 -0
- data/.idea/scopes/scope_settings.xml +5 -0
- data/.idea/vcs.xml +7 -0
- data/.idea/workspace.xml +804 -0
- data/.rvmrc +53 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +63 -0
- data/Rakefile +1 -0
- data/lib/ms_paraphrase.rb +115 -0
- data/lib/ms_paraphrase/version.rb +3 -0
- data/ms_paraphrase.gemspec +24 -0
- data/test/connection_spec.rb +65 -0
- metadata +93 -0
data/.rvmrc
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use 1.9.3" > .rvmrc
|
9
|
+
environment_id="ruby-2.0.0-p353@ms_paraphrase"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.14.5 (stable)" # 1.10.1 seams as a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
|
18
|
+
# First we attempt to load the desired environment directly from the environment
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
20
|
+
# CLI and selector. If you want feedback on which environment was used then
|
21
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
25
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
26
|
+
[[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
|
27
|
+
\. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
|
28
|
+
if [[ $- == *i* ]] # check for interactive shells
|
29
|
+
then echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
|
30
|
+
else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
|
31
|
+
fi
|
32
|
+
else
|
33
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
34
|
+
rvm --create use "$environment_id" || {
|
35
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
36
|
+
return 1
|
37
|
+
}
|
38
|
+
fi
|
39
|
+
|
40
|
+
# If you use bundler, this might be useful to you:
|
41
|
+
# if [[ -s Gemfile ]] && {
|
42
|
+
# ! builtin command -v bundle >/dev/null ||
|
43
|
+
# builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
|
44
|
+
# }
|
45
|
+
# then
|
46
|
+
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
|
47
|
+
# gem install bundler
|
48
|
+
# fi
|
49
|
+
# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
|
50
|
+
# then
|
51
|
+
# bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
|
52
|
+
# fi
|
53
|
+
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Martin Tuncaydin
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# MsParaphrase
|
2
|
+
|
3
|
+
This is a rails wrapper for the Microsoft Paraphrase API. Microsoft Paraphrase API is an English-to-English machine translation system that rephrases English sentences in English.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'ms_paraphrase'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install ms_paraphrase
|
18
|
+
|
19
|
+
## Access
|
20
|
+
|
21
|
+
Subscribe to the Microsoft Translator API <a href="http://go.microsoft.com/?linkid=9782667">here.</a>
|
22
|
+
|
23
|
+
Register your application with <a href="https://datamarket.azure.com/developer/applications/">Azure DataMarket</a>.
|
24
|
+
|
25
|
+
##Configuration
|
26
|
+
|
27
|
+
The best way to configure the module is to add an initializer in config called ms_paraphrase.rb and add the following :
|
28
|
+
|
29
|
+
MsParaphrase.configure do |config|
|
30
|
+
config.token_api = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'
|
31
|
+
config.paraphrase_api = 'http://api.microsofttranslator.com/v3/json/paraphrase'
|
32
|
+
config.client_id = 'XXXXXXXXXXX'
|
33
|
+
config.client_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
|
34
|
+
config.scope = 'http://api.microsofttranslator.com'
|
35
|
+
config.grant_type = 'client_credentials'
|
36
|
+
|
37
|
+
#Configure token manager
|
38
|
+
MsParaphrase.configure_token_manager
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
##Paraphrasing
|
43
|
+
|
44
|
+
s = "This is a sentence that we are going to run through the microsoft Paraphrase API"
|
45
|
+
MsParaphrase.translator.translate(s)
|
46
|
+
|
47
|
+
|
48
|
+
Error message can be found by accessing :
|
49
|
+
|
50
|
+
MsParaphrase.translator.result['em']
|
51
|
+
|
52
|
+
Array of results can be found by accessing :
|
53
|
+
|
54
|
+
MsParaphrase.translator.result['paraphrases']
|
55
|
+
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
1. Fork it ( http://github.com/flightjumpr/ms_paraphrase/fork )
|
60
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
61
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
62
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
63
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require "ms_paraphrase/version"
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module MsParaphrase
|
5
|
+
|
6
|
+
class Configuration
|
7
|
+
attr_accessor :token_api, :paraphrase_api, :client_id, :client_secret, :scope, :grant_type
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
#self.log_level = 'info'
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
class << self
|
16
|
+
attr_accessor :configuration, :token_manager, :translator
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.configure
|
20
|
+
self.configuration ||= Configuration.new
|
21
|
+
self.token_manager ||= TokenManager.new
|
22
|
+
self.translator ||= Translator.new
|
23
|
+
yield(configuration) if block_given?
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.configure_token_manager
|
27
|
+
self.token_manager ||= TokenManager.new
|
28
|
+
yield(token_manager) if block_given?
|
29
|
+
end
|
30
|
+
|
31
|
+
class TokenManager
|
32
|
+
|
33
|
+
private
|
34
|
+
attr_accessor :token, :token_expiration
|
35
|
+
public
|
36
|
+
|
37
|
+
MAX_TRANSLATIONS = 20
|
38
|
+
|
39
|
+
def initialize
|
40
|
+
self.token = nil
|
41
|
+
end
|
42
|
+
|
43
|
+
def get_token
|
44
|
+
if token.nil? || !is_token_valid?(token)
|
45
|
+
|
46
|
+
self.token = JSON.parse(RestClient.post MsParaphrase.configuration.token_api, {:client_id => MsParaphrase.configuration.client_id,
|
47
|
+
:client_secret => MsParaphrase.configuration.client_secret,
|
48
|
+
:scope => MsParaphrase.configuration.scope,
|
49
|
+
:grant_type => MsParaphrase.configuration.grant_type}) if is_credentials_provided?
|
50
|
+
elsif is_token_valid?(token)
|
51
|
+
return token
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
def is_credentials_provided?
|
57
|
+
if MsParaphrase.configuration.token_api.nil?
|
58
|
+
raise TranslateApiException.new('No token API endpoint provided.')
|
59
|
+
elsif MsParaphrase.configuration.client_id.nil?
|
60
|
+
raise TranslateApiException.new('No client ID was provided.')
|
61
|
+
elsif MsParaphrase.configuration.client_secret.nil?
|
62
|
+
raise TranslateApiException.new('No client secret was provided.')
|
63
|
+
elsif MsParaphrase.configuration.scope.nil?
|
64
|
+
raise TranslateApiException.new('No scope was provided.')
|
65
|
+
elsif MsParaphrase.configuration.grant_type.nil?
|
66
|
+
raise TranslateApiException.new('No grant type provided.')
|
67
|
+
else
|
68
|
+
return true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
def is_token_valid?(token)
|
74
|
+
token['expires_in'].to_i < 10 ? false : true
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
class Translator < TokenManager
|
80
|
+
attr_accessor :result
|
81
|
+
|
82
|
+
def translate(sentence)
|
83
|
+
unless sentence.nil?
|
84
|
+
v = MsParaphrase.configuration.paraphrase_api + "?sentence=#{CGI::escape(sentence)}&language=en&Category=general"
|
85
|
+
r = RestClient.get v, { 'Authorization' => "Bearer #{MsParaphrase.token_manager.get_token['access_token']}"}
|
86
|
+
self.result = JSON.parse(r.force_encoding("UTF-8").gsub("\xEF\xBB\xBF", ''))
|
87
|
+
is_translation_success?
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
private
|
92
|
+
def is_translation_success?
|
93
|
+
if self.result.keys.include?('em') && self.result['em'] == 'OK' && self.result['paraphrases'].is_a?(Array)
|
94
|
+
return true
|
95
|
+
else
|
96
|
+
raise TranslationException.new(self.result['em'])
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
public
|
103
|
+
class TranslateApiException < Exception
|
104
|
+
def initialize(data)
|
105
|
+
@data = data
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
class TranslationException < Exception
|
110
|
+
def initialize(data)
|
111
|
+
@data = data
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ms_paraphrase/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ms_paraphrase"
|
8
|
+
spec.version = MsParaphrase::VERSION
|
9
|
+
spec.authors = ["Martin Tuncaydin"]
|
10
|
+
spec.email = ["martin.tuncaydin@flightjumpr.com"]
|
11
|
+
spec.summary = %q{Wrapper for the microsoft Paraphrase API.}
|
12
|
+
spec.description = %q{Provides a connectivity wrapper around the microsoft Paraphrase API. Token management and paraphrasing of sentences.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rest_client'
|
3
|
+
require 'ms_paraphrase'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
describe 'Connectivity to translation API' do
|
7
|
+
before(:all) do
|
8
|
+
MsParaphrase.configure do |config|
|
9
|
+
config.token_api = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'
|
10
|
+
config.paraphrase_api = 'http://api.microsofttranslator.com/v3/json/paraphrase'
|
11
|
+
config.client_id = 'FlightJumpr'
|
12
|
+
config.client_secret = 'N1bGd+G03SWwz0rXdpbCQ5rL3as/3H0IqfRO39sql7A='
|
13
|
+
config.scope = 'http://api.microsofttranslator.com'
|
14
|
+
config.grant_type = 'client_credentials'
|
15
|
+
|
16
|
+
#Configure token manager
|
17
|
+
MsParaphrase.configure_token_manager
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should connect to microsoft token service and get token' do
|
22
|
+
response = RestClient.post MsParaphrase.configuration.token_api, :client_id => MsParaphrase.configuration.client_id, :client_secret => MsParaphrase.configuration.client_secret, :scope => MsParaphrase.configuration.scope, :grant_type => MsParaphrase.configuration.grant_type
|
23
|
+
expect(response.code).to eq(200) #check for http 200 code
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should return a json response' do
|
27
|
+
response = RestClient.post MsParaphrase.configuration.token_api, :client_id => MsParaphrase.configuration.client_id, :client_secret => MsParaphrase.configuration.client_secret, :scope => MsParaphrase.configuration.scope, :grant_type => MsParaphrase.configuration.grant_type
|
28
|
+
body = JSON.parse(response.body)
|
29
|
+
body.should include('access_token')
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'token manager tests' do
|
33
|
+
|
34
|
+
it "module should return saved or created token" do
|
35
|
+
expect(MsParaphrase.token_manager.get_token).to_not be_nil
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
describe 'should paraphrase submitted sentence' do
|
41
|
+
before(:all) do
|
42
|
+
MsParaphrase.configure_token_manager
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should paraphrase provided sentence and produce results" do
|
46
|
+
s = 'This is an example sentence that in an idea world should be translated by the paraphrase API.'
|
47
|
+
expect(MsParaphrase.translator.translate(s)).equal? true
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should produce error message due to too many sentences" do
|
51
|
+
s = 'This is an example sentence that in an idea world should be translated by the paraphrase API. This attempt should fail'
|
52
|
+
expect { MsParaphrase.translator.translate(s) }.to raise_error
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should return response that contains em OK and a set of paraphrase results." do
|
56
|
+
s = 'This is an example sentence that in an idea world should be translated by the paraphrase API.'
|
57
|
+
MsParaphrase.translator.translate(s)
|
58
|
+
expect(MsParaphrase.translator.result.values.include?('em')).equal? true
|
59
|
+
expect(MsParaphrase.translator.result['em']).equal? "OK"
|
60
|
+
expect(MsParaphrase.translator.result['paraphrases']).is_a? Array
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ms_paraphrase
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Tuncaydin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-28 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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Provides a connectivity wrapper around the microsoft Paraphrase API.
|
42
|
+
Token management and paraphrasing of sentences.
|
43
|
+
email:
|
44
|
+
- martin.tuncaydin@flightjumpr.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- .gitignore
|
50
|
+
- .idea/.name
|
51
|
+
- .idea/.rakeTasks
|
52
|
+
- .idea/encodings.xml
|
53
|
+
- .idea/misc.xml
|
54
|
+
- .idea/modules.xml
|
55
|
+
- .idea/ms_paraphrase.iml
|
56
|
+
- .idea/scopes/scope_settings.xml
|
57
|
+
- .idea/vcs.xml
|
58
|
+
- .idea/workspace.xml
|
59
|
+
- .rvmrc
|
60
|
+
- Gemfile
|
61
|
+
- LICENSE.txt
|
62
|
+
- README.md
|
63
|
+
- Rakefile
|
64
|
+
- lib/ms_paraphrase.rb
|
65
|
+
- lib/ms_paraphrase/version.rb
|
66
|
+
- ms_paraphrase.gemspec
|
67
|
+
- test/connection_spec.rb
|
68
|
+
homepage: ''
|
69
|
+
licenses:
|
70
|
+
- MIT
|
71
|
+
metadata: {}
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 2.2.2
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: Wrapper for the microsoft Paraphrase API.
|
92
|
+
test_files:
|
93
|
+
- test/connection_spec.rb
|