easy_translate 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 422e8ca1c75f8125002cba2383871ec8988d14c4
4
- data.tar.gz: c9fca5ee01af683ea3d7d3decc7afd6423cc8909
3
+ metadata.gz: a65f5b6c50442857f3f8de4d618cd0d3311a78af
4
+ data.tar.gz: a74abf1852057b9dbb787d2e2a8df4c53da5e0f6
5
5
  SHA512:
6
- metadata.gz: 4e8ba638b958afb464830b30f4b558ccb61d1f7e7d951fcea48c5cf1fc58651878bd9d645de4937ada57ce4ebfcf8eaf0ca1b1b5fe343df16048d9cdb9a6096d
7
- data.tar.gz: dc2254431130de0d5f30a4e5c85f0e29b7289ffb6b17cc2920fadf5ffbffc1b5b1d9bb9e7c5282315738f842bb3b1a6e5464a1a4b372d663b12bd9067544d1dc
6
+ metadata.gz: 064111bf0a3becf0ef54ed54e49452c6d1ce14929b0c5f51a929bef71d68d4983b5b83cb7d1232599f5e3c799031f4b1d13159766219d5196454bd113fc05267
7
+ data.tar.gz: 95b03df27f5e80405e17000f30a68c9370ac0204840d18690a594f75266dc874c0eea21837faa5041c48395ec9fb471faec69df5af7ec236ad2feedfe80a10e6
@@ -1,24 +1,30 @@
1
1
  require 'json'
2
2
  require 'cgi'
3
3
  require 'easy_translate/request'
4
+ require 'easy_translate/threadable'
4
5
 
5
6
  module EasyTranslate
6
7
 
7
8
  module Detection
9
+ include Threadable
8
10
 
9
11
  # Detect language
10
12
  # @param [String, Array] texts - A single string or set of strings to detect for
11
13
  # @param [Hash] options - Extra options to pass along with the request
12
14
  # @return [String, Array] The resultant language or languages
13
15
  def detect(texts, options = {}, http_options = {})
16
+ threaded_process(:request_detection, texts, options, http_options)
17
+ end
18
+
19
+ private
20
+ def request_detection(texts, options, http_options)
14
21
  request = DetectionRequest.new(texts, options, http_options)
15
- # Turn the response into an array of detections
16
22
  raw = request.perform_raw
17
23
  detections = JSON.parse(raw)['data']['detections'].map do |res|
18
- res.empty? ? nil : res.first['language']
24
+ res.empty? ? nil :
25
+ options[:confidence] ?
26
+ { :language => res.first['language'], :confidence => res.first['confidence'] } : res.first['language']
19
27
  end
20
- # And then return, if they only asked for one, only give one back
21
- request.multi? ? detections : detections.first
22
28
  end
23
29
 
24
30
  # A convenience class for wrapping a detection request
@@ -30,6 +30,7 @@ module EasyTranslate
30
30
  'it' => 'italian',
31
31
  'ja' => 'japanese',
32
32
  'ko' => 'korean',
33
+ 'la' => 'latin',
33
34
  'lv' => 'latvian',
34
35
  'lt' => 'lithuanian',
35
36
  'mk' => 'macedonian',
@@ -0,0 +1,33 @@
1
+ require 'thread/pool'
2
+ require 'thread_safe'
3
+
4
+ module EasyTranslate
5
+
6
+ module Threadable
7
+ def threaded_process(method, *args)
8
+ texts = args[0]
9
+ options = args[1]
10
+ http_options = args[2]
11
+ options = options.dup
12
+ batch_size = options.delete(:batch_size) || 100
13
+ concurrency = options.delete(:concurrency) || 4
14
+ batches = Array(texts).each_slice(batch_size).to_a
15
+ if concurrency > 1 && batches.size > 1
16
+ pool = Thread::Pool.new([concurrency, 1 + (texts.length - 1) / batch_size].min)
17
+ batch_results = ThreadSafe::Array.new
18
+ batches.each_with_index do |texts_batch, i|
19
+ pool.process { batch_results[i] = self.send(method, texts_batch, options, http_options) }
20
+ end
21
+ pool.shutdown
22
+ results = batch_results.reduce(:+)
23
+ else
24
+ results = batches.map { |texts_batch| self.send(method, texts_batch, options, http_options) }.reduce(:+)
25
+ end
26
+ # if they only asked for one, only give one back
27
+ texts.is_a?(String) ? results[0] : results
28
+ ensure
29
+ pool.shutdown! if pool && !pool.shutdown?
30
+ end
31
+ end
32
+
33
+ end
@@ -1,12 +1,12 @@
1
1
  require 'json'
2
2
  require 'cgi'
3
- require 'thread/pool'
4
- require 'thread_safe'
5
3
  require 'easy_translate/request'
4
+ require 'easy_translate/threadable'
6
5
 
7
6
  module EasyTranslate
8
7
 
9
8
  module Translation
9
+ include Threadable
10
10
 
11
11
  # Translate text
12
12
  # @param [String, Array] texts - A single string or set of strings to translate
@@ -17,25 +17,7 @@ module EasyTranslate
17
17
  # @option options [Boolean] :html - Whether or not the supplied string is HTML (optional)
18
18
  # @return [String, Array] Translated text or texts
19
19
  def translate(texts, options = {}, http_options = {})
20
- options = options.dup
21
- batch_size = options.delete(:batch_size) || 100
22
- concurrency = options.delete(:concurrency) || 4
23
- batches = Array(texts).each_slice(batch_size).to_a
24
- if concurrency > 1 && batches.size > 1
25
- pool = Thread::Pool.new([concurrency, 1 + (texts.length - 1) / batch_size].min)
26
- batch_results = ThreadSafe::Array.new
27
- batches.each_with_index do |texts_batch, i|
28
- pool.process { batch_results[i] = request_translations(texts_batch, options, http_options) }
29
- end
30
- pool.shutdown
31
- results = batch_results.reduce(:+)
32
- else
33
- results = batches.map { |texts_batch| request_translations(texts_batch, options, http_options) }.reduce(:+)
34
- end
35
- # if they only asked for one, only give one back
36
- texts.is_a?(String) ? results[0] : results
37
- ensure
38
- pool.shutdown! if pool && !pool.shutdown?
20
+ threaded_process(:request_translations, texts, options, http_options)
39
21
  end
40
22
 
41
23
  private
@@ -51,7 +33,8 @@ module EasyTranslate
51
33
  # Turn the response into an array of translations
52
34
  raw = request.perform_raw
53
35
  JSON.parse(raw)['data']['translations'].map do |res|
54
- res['translatedText']
36
+ raw_translation = res['translatedText']
37
+ CGI.unescapeHTML(raw_translation)
55
38
  end
56
39
  end
57
40
 
@@ -1,5 +1,5 @@
1
1
  module EasyTranslate
2
2
 
3
- VERSION = '0.4.0'
3
+ VERSION = '0.5.0'
4
4
 
5
5
  end
@@ -1,11 +1,14 @@
1
1
  # Start SimpleCov
2
2
  begin
3
+ require 'ostruct'
3
4
  require 'simplecov'
4
5
  SimpleCov.start
5
6
  rescue LoadError
6
7
  puts 'for coverage please install SimpleCov'
7
8
  end
8
9
 
10
+ require 'ostruct'
11
+
9
12
  # Require the actual project
10
13
  $: << File.expand_path('../lib', __FILE__)
11
14
  require 'easy_translate'
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Crepezzi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-17 00:00:00.000000000 Z
11
+ date: 2014-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: json
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: thread
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: thread_safe
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  description: easy_translate is a wrapper for the google translate API that makes sense
@@ -73,14 +73,15 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
+ - lib/easy_translate.rb
76
77
  - lib/easy_translate/detection.rb
77
78
  - lib/easy_translate/easy_translate_exception.rb
78
79
  - lib/easy_translate/languages.rb
79
80
  - lib/easy_translate/request.rb
81
+ - lib/easy_translate/threadable.rb
80
82
  - lib/easy_translate/translation.rb
81
83
  - lib/easy_translate/translation_target.rb
82
84
  - lib/easy_translate/version.rb
83
- - lib/easy_translate.rb
84
85
  - spec/spec_helper.rb
85
86
  homepage: https://github.com/seejohnrun/easy_translate
86
87
  licenses: []
@@ -91,17 +92,17 @@ require_paths:
91
92
  - lib
92
93
  required_ruby_version: !ruby/object:Gem::Requirement
93
94
  requirements:
94
- - - '>='
95
+ - - ">="
95
96
  - !ruby/object:Gem::Version
96
97
  version: '0'
97
98
  required_rubygems_version: !ruby/object:Gem::Requirement
98
99
  requirements:
99
- - - '>='
100
+ - - ">="
100
101
  - !ruby/object:Gem::Version
101
102
  version: '0'
102
103
  requirements: []
103
104
  rubyforge_project:
104
- rubygems_version: 2.0.6
105
+ rubygems_version: 2.2.0
105
106
  signing_key:
106
107
  specification_version: 4
107
108
  summary: Google Translate API Wrapper for Ruby