google-translate 1.0.1 → 1.1.0

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.
data/CHANGES CHANGED
@@ -65,4 +65,8 @@
65
65
 
66
66
  == Version 1.0.1
67
67
 
68
- * Bug fixes
68
+ * Bug fixes
69
+
70
+ == Version 1.1.0
71
+
72
+ * Reflects changes in recent google translate service.
data/Gemfile CHANGED
@@ -1,13 +1,14 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  group :default do
4
- gem "json_pure"
5
- gem "thor"
4
+ gem "json_pure", "~>1.8"
5
+ gem "resource_accessor", "~>1.2"
6
+ gem "thor", "~>0.19"
6
7
  end
7
8
 
8
9
  group :development do
9
- gem "gemspec_deps_gen"
10
- gem "gemcutter"
10
+ gem "gemspec_deps_gen", "~>1.1"
11
+ gem "gemcutter", "~>0.7"
11
12
  end
12
13
 
13
14
  group :test do
data/Gemfile.lock CHANGED
@@ -2,30 +2,36 @@ GEM
2
2
  remote: https://rubygems.org/
3
3
  specs:
4
4
  awesome_print (1.2.0)
5
- diff-lcs (1.2.4)
5
+ diff-lcs (1.2.5)
6
6
  file_utils (1.0.7)
7
7
  gemcutter (0.7.1)
8
- gemspec_deps_gen (1.1.1)
8
+ gemspec_deps_gen (1.1.2)
9
9
  bundler
10
10
  file_utils
11
- json_pure (1.8.0)
12
- rspec (2.14.1)
13
- rspec-core (~> 2.14.0)
14
- rspec-expectations (~> 2.14.0)
15
- rspec-mocks (~> 2.14.0)
16
- rspec-core (2.14.4)
17
- rspec-expectations (2.14.1)
18
- diff-lcs (>= 1.1.3, < 2.0)
19
- rspec-mocks (2.14.3)
20
- thor (0.18.1)
11
+ json_pure (1.8.1)
12
+ resource_accessor (1.2.3)
13
+ rspec (3.0.0)
14
+ rspec-core (~> 3.0.0)
15
+ rspec-expectations (~> 3.0.0)
16
+ rspec-mocks (~> 3.0.0)
17
+ rspec-core (3.0.4)
18
+ rspec-support (~> 3.0.0)
19
+ rspec-expectations (3.0.4)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.0.0)
22
+ rspec-mocks (3.0.4)
23
+ rspec-support (~> 3.0.0)
24
+ rspec-support (3.0.4)
25
+ thor (0.19.1)
21
26
 
22
27
  PLATFORMS
23
28
  ruby
24
29
 
25
30
  DEPENDENCIES
26
31
  awesome_print
27
- gemcutter
28
- gemspec_deps_gen
29
- json_pure
32
+ gemcutter (~> 0.7)
33
+ gemspec_deps_gen (~> 1.1)
34
+ json_pure (~> 1.8)
35
+ resource_accessor (~> 1.2)
30
36
  rspec
31
- thor
37
+ thor (~> 0.19)
data/README.md CHANGED
@@ -43,6 +43,10 @@ or
43
43
 
44
44
  $ ts ru hello world
45
45
 
46
+ Translates and gives extra information (nouns, verbs, synonyms):
47
+
48
+ $ t -e ru help
49
+
46
50
  ## Contributing
47
51
 
48
52
  1. Fork it
data/Rakefile CHANGED
@@ -10,7 +10,7 @@ version = GoogleTranslate::VERSION
10
10
  project_name = File.basename(Dir.pwd)
11
11
 
12
12
  task :gen do
13
- generator = GemspecDepsGen.new project_name
13
+ generator = GemspecDepsGen.new
14
14
 
15
15
  generator.generate_dependencies "spec", "#{project_name}.gemspec.erb", "#{project_name}.gemspec"
16
16
  end
data/bin/translate CHANGED
@@ -15,6 +15,7 @@ elsif ARGV.size == 1
15
15
  else
16
16
  params = []
17
17
  say = ENV['say'] ? (ENV['say'] == 'true') : false
18
+ extra = ENV['extra'] ? (ENV['extra'] == 'true') : false
18
19
 
19
20
  index = 0
20
21
 
@@ -23,6 +24,8 @@ else
23
24
 
24
25
  if param =~ /--say/ or param =~ /-s/
25
26
  say = true
27
+ elsif param =~ /--extra/ or param =~ /-e/
28
+ extra = true
26
29
  else
27
30
  params << param
28
31
  end
@@ -39,5 +42,5 @@ else
39
42
  ['en', '', '']
40
43
  end
41
44
 
42
- TranslateCLI.start ["translate", "--say=#{say}", from.to_sym, to.to_sym, text]
45
+ TranslateCLI.start ["translate", "--say=#{say}", "--extra=#{extra}", from.to_sym, to.to_sym, text]
43
46
  end
@@ -4,7 +4,7 @@ require File.expand_path(File.dirname(__FILE__) + '/lib/google_translate/version
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "google-translate"
7
- spec.summary = %q{Simple client for Google Translate API.}
7
+ spec.summary = %q{Summary: Simple client for Google Translate API.}
8
8
  spec.description = %q{Simple client for Google Translate API.}
9
9
  spec.email = "alexander.shvets@gmail.com"
10
10
  spec.authors = ["Alexander Shvets"]
@@ -3,9 +3,10 @@
3
3
  require 'net/http'
4
4
  require 'json'
5
5
  require 'tempfile'
6
+ require 'resource_accessor'
6
7
 
7
8
  class GoogleTranslate
8
- GOOGLE_TRANSLATE_SERVICE_URL = "http://translate.google.com"
9
+ GOOGLE_TRANSLATE_SERVICE_URL = "translate.google.com"
9
10
 
10
11
  def supported_languages
11
12
  response = call_service GOOGLE_TRANSLATE_SERVICE_URL
@@ -21,7 +22,9 @@ class GoogleTranslate
21
22
  raise("Missing 'to' language") unless to_lang
22
23
  raise("Missing text for translation") unless text
23
24
 
24
- result = JSON.parse(call_translate_service(from_lang, to_lang, text))
25
+ r = call_translate_service(from_lang, to_lang, text)
26
+
27
+ result = JSON.parse(r.gsub('[,', '['))
25
28
 
26
29
  raise("Translate Server is down") if (!result || result.empty?)
27
30
 
@@ -31,7 +34,7 @@ class GoogleTranslate
31
34
  def say lang, text
32
35
  speech_content = call_speech_service(lang, text)
33
36
 
34
- file = Tempfile.new('.google_translate_speech-')
37
+ file = Tempfile.new('.google_translate_speech---')
35
38
 
36
39
  file.write(speech_content)
37
40
 
@@ -45,17 +48,17 @@ class GoogleTranslate
45
48
  private
46
49
 
47
50
  def translate_url(from_lang, to_lang)
48
- "#{GOOGLE_TRANSLATE_SERVICE_URL}/translate_a/t?client=t&sl=#{from_lang}&tl=#{to_lang}&hl=pl&sc=2&ie=UTF-8&oe=UTF-8&prev=enter&ssel=0&tsel=0&"
51
+ "https://#{GOOGLE_TRANSLATE_SERVICE_URL}/translate_a/single?client=t&sl=#{from_lang}&tl=#{to_lang}&hl=en&dt=bd&dt=ex&dt=ld&dt=md&dt=qc&dt=rw&dt=rm&dt=ss&dt=t&dt=at&dt=sw&ie=UTF-8&oe=UTF-8&prev=btn&rom=1&ssel=0&tsel=0"
49
52
  end
50
53
 
51
54
  def speech_url(lang)
52
- "#{GOOGLE_TRANSLATE_SERVICE_URL}/translate_tts?tl=#{lang}&ie=UTF-8&oe=UTF-8"
55
+ "http://#{GOOGLE_TRANSLATE_SERVICE_URL}/translate_tts?tl=#{lang}&ie=UTF-8&oe=UTF-8"
53
56
  end
54
57
 
55
58
  def call_translate_service from_lang, to_lang, text
56
59
  url = translate_url(from_lang, to_lang)
57
60
 
58
- response = call_service url, text
61
+ response = call_service url + "&q=#{text}"
59
62
 
60
63
  response.body.split(',').collect { |s| s == '' ? "\"\"" : s }.join(",") # fix json object
61
64
  end
@@ -63,24 +66,18 @@ class GoogleTranslate
63
66
  def call_speech_service lang, text
64
67
  url = speech_url(lang)
65
68
 
66
- response = call_service url, text
69
+ response = call_service url + "&q=#{text}"
67
70
 
68
71
  response.body
69
72
  end
70
73
 
71
- def call_service url, text=nil
72
- uri = URI.parse(URI.escape(url))
73
-
74
- http = Net::HTTP.new(uri.host, uri.port)
75
- request = Net::HTTP::Post.new(uri.request_uri)
76
- request.set_form_data(text: text)
74
+ def call_service url
75
+ accessor = ResourceAccessor.new
77
76
 
78
- http.request(request)
77
+ accessor.get_response url: url
79
78
  end
80
79
 
81
80
  def collect_languages buffer, index, tag_name, tag_id
82
- languages = []
83
-
84
81
  spaces = '\s?'
85
82
  quote = '(\s|\'|")?'
86
83
 
@@ -109,10 +106,7 @@ class GoogleTranslate
109
106
  matches = text.gsub(/selected/i, '').squeeze.scan(re2)
110
107
  end
111
108
 
112
- matches.each do |m| languages << Language.new(m[2], m[1])
113
- end
114
-
115
- languages
109
+ matches.map { |m| Language.new(m[2], m[1]) }
116
110
  end
117
111
  end
118
112
 
@@ -0,0 +1,83 @@
1
+ class ResultParser
2
+
3
+ attr_reader :result
4
+
5
+ def initialize result
6
+ @result = result
7
+ end
8
+
9
+ def translation
10
+ result[0][0][0]
11
+ end
12
+
13
+ def translit
14
+ result[0][1][2]
15
+ end
16
+
17
+ def nouns
18
+ list = []
19
+
20
+ if result[1].size > 1
21
+ if result[1][0]
22
+ list = result[1][0][1]
23
+ end
24
+ end
25
+
26
+ list
27
+ end
28
+
29
+ def verbs
30
+ list = []
31
+
32
+ if result[1].size > 1
33
+ if result[1][1]
34
+ list = result[1][1][1]
35
+ end
36
+ end
37
+
38
+ list
39
+ end
40
+
41
+ def synonym_nouns
42
+ list = []
43
+
44
+ if result[11].size > 1
45
+ if result[1][0]
46
+ result[11][0][1].each do |r, _|
47
+ list << r
48
+ end
49
+ end
50
+ end
51
+
52
+ list
53
+ end
54
+
55
+ def synonym_verbs
56
+ list = []
57
+
58
+ if result[11].size > 1
59
+ if result[1][1]
60
+ result[11][1][1].each do |r, _|
61
+ list << r
62
+ end
63
+ end
64
+ end
65
+
66
+ list
67
+ end
68
+
69
+ def synonym_exclamations
70
+ list = []
71
+
72
+ if result[11].size > 1
73
+ if result[1][2]
74
+ result[11][2][1].each do |r, _|
75
+ list << r
76
+ end
77
+ end
78
+ end
79
+
80
+ list
81
+ end
82
+
83
+ end
@@ -1,6 +1,7 @@
1
1
  require "thor"
2
2
  require 'google_translate'
3
3
  require 'google_translate/version'
4
+ require 'google_translate/result_parser'
4
5
 
5
6
  class TranslateCLI < Thor
6
7
  USAGE = <<-LONGDESC
@@ -13,6 +14,7 @@ class TranslateCLI < Thor
13
14
  translate en:ru Hello world # translates from English to Russian
14
15
  translate ru Hello world # translates to Russian from auto-detected language
15
16
  translate -s ru Hello world # translates and tries to say it
17
+ translate -e ru help # translates and gives extra information (nouns, verbs, synonyms)
16
18
  LONGDESC
17
19
 
18
20
  desc "version", "displays version"
@@ -31,45 +33,54 @@ class TranslateCLI < Thor
31
33
  end
32
34
 
33
35
  long_desc USAGE
34
- desc "thanslate text", "thanslates the text"
35
- option :say, :aliases => "-s"
36
+ desc "translate text", "translates the text"
37
+ option :say, aliases: "-s"
38
+ option :extra, aliases: "-e"
36
39
  def translate from_lang, to_lang, text
37
40
  translator = GoogleTranslate.new
38
41
 
39
42
  result = translator.translate(from_lang, to_lang, text)
40
43
 
41
- translation = result[0][0][0]
42
- translit = result[0][0][2]
44
+ result_parser = ResultParser.new result
43
45
 
44
- puts "Translation: #{translation}"
45
- puts "Translit: #{translit}" unless translit.size == 0
46
+ extra = options[:extra] ? (options[:extra] == 'true') : false
46
47
 
47
- display_synonyms(result)
48
+ if extra
49
+ puts "Nouns: #{result_parser.nouns.join(", ")}"
50
+ puts "Verbs: #{result_parser.verbs.join(", ")}"
48
51
 
49
- say = options[:say] ? (options[:say] == 'true') : false
52
+ puts "Synonym Nouns:"
50
53
 
51
- if say and !!(RUBY_PLATFORM =~ /darwin/i)
52
- translator.say(from_lang, text)
53
- translator.say(to_lang, translation)
54
- end
55
- end
54
+ result_parser.synonym_nouns.each do |r|
55
+ puts "- " + r.join(", ")
56
+ end
56
57
 
57
- private
58
+ puts "Synonym Verbs:"
58
59
 
59
- def display_synonyms result
60
- if result[5].size > 5
61
- synonyms = result[5][0][2]
60
+ result_parser.synonym_verbs.each do |r|
61
+ puts "- " + r.join(", ")
62
+ end
62
63
 
63
- if synonyms.size > 1
64
- puts "Synonyms:"
64
+ puts "Synonym Exclamations:"
65
65
 
66
- (1..synonyms.size-1).each do |index|
67
- puts synonyms[index][0]
68
- end
66
+ result_parser.synonym_exclamations.each do |r|
67
+ puts "- " + r.join(", ")
69
68
  end
70
69
  end
70
+
71
+ puts "Translation: #{result_parser.translation}"
72
+ puts "Translit: #{result_parser.translit}" unless result_parser.translit.size == 0
73
+
74
+ say = options[:say] ? (options[:say] == 'true') : false
75
+
76
+ if say and !!(RUBY_PLATFORM =~ /darwin/i)
77
+ translator.say(from_lang, text)
78
+ translator.say(to_lang, result_parser.translation)
79
+ end
71
80
  end
72
81
 
82
+ private
83
+
73
84
  def print_languages title, list
74
85
  puts title
75
86
  puts list.join(', ')
@@ -1,3 +1,3 @@
1
1
  class GoogleTranslate
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -4,7 +4,7 @@ require File.dirname(__FILE__) + '/spec_helper'
4
4
 
5
5
  require 'google_translate'
6
6
 
7
- describe GoogleTranslate do
7
+ RSpec.describe GoogleTranslate do
8
8
 
9
9
  it "should raise an error if one of parameters is missing" do
10
10
  expect { subject.translate(nil, :ru) }.to raise_error
@@ -17,13 +17,14 @@ describe GoogleTranslate do
17
17
  it "should translate test string from one language to another" do
18
18
  r = subject.translate(:en, :ru, "hello world!")
19
19
  puts r
20
- r.size.should be > 0
20
+
21
+ expect(r.size).to be > 0
21
22
  end
22
23
 
23
24
  it "should translate test string from one language to another with autodetect" do
24
25
  r = subject.translate(:auto, :ru, "hello world!")
25
26
  puts r
26
- r.size.should be > 0
27
+ expect(r.size).to be > 0
27
28
  end
28
29
 
29
30
  #it "should return unreliable flag if language is not recognized" do
@@ -31,9 +32,9 @@ describe GoogleTranslate do
31
32
  #end
32
33
 
33
34
  it "should return list of supported languages" do
34
- languages = subject.supported_languages
35
+ from_languages, to_languages = subject.supported_languages
35
36
 
36
- languages[:from_languages].size.should > 0
37
- languages[:to_languages].size.should > 0
37
+ expect(from_languages.size).to be > 0
38
+ expect(to_languages.size).to be > 0
38
39
  end
39
40
  end