gg_translator 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 67cf9de66881c9c917f7e45889ff702081e7014c
4
+ data.tar.gz: 95c36f7a768c55fcfb69b681b9f0f29d50f52a3d
5
+ SHA512:
6
+ metadata.gz: 987604ed6bd93d8f5120906de98037212b973a6ba7bc816216ece08e0123cbea324d03165cf433edfe746517d44120eac20c07c981255dff12c0212b95eed7f9
7
+ data.tar.gz: 5e57100fa3af99eb581601410d0722c6aa26c84682848ccc4a018769e6e49affa0f8b1ca31427d782d000bb0ae3f44f52035d90c76679c6d3f033a00a8738cdf
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .idea
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'json'
4
+ gem 'readline'
5
+
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Serge Kislak
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,29 @@
1
+ # GgTranslator
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'gg_translator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install gg_translator
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/gg ADDED
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'gg_translator'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ require 'gg_translator'
8
+ end
9
+
10
+ t = (ARGV.count > 0 && ARGV.join(' '))
11
+ translator = GgTranslator::Translator.new
12
+ (translator.t(URI.escape(t)) && exit) if t
13
+
14
+ require 'readline'
15
+ require 'yaml'
16
+ require 'tmpdir'
17
+ fname = "#{Dir::tmpdir}/google_translator.conf"
18
+
19
+
20
+ parsed = begin
21
+ YAML.load(File.open(fname))
22
+ rescue
23
+ end
24
+
25
+ if parsed
26
+ translator.sl = parsed[:sl]
27
+ translator.tl = parsed[:tl]
28
+ end
29
+
30
+ loop do
31
+ s = Readline::readline('> ')
32
+ break if s == 'exit'
33
+ next if (s == '') && Readline::HISTORY.to_a.last && translator.exp(Readline::HISTORY.to_a.last)
34
+
35
+ Readline::HISTORY.push(s)
36
+
37
+ if s.match(/^rv$/) || s.match(/^ch$/)
38
+ translator.sl, translator.tl = translator.tl, translator.sl
39
+ next
40
+ end
41
+
42
+ next if s.match(/^from /) && s.gsub!(/^from /, '') && translator.sl = s
43
+ next if s.match(/^into /) && s.gsub!(/^into /, '') && translator.tl = s
44
+ next if (s.match(/^exp /) || s == 'exp') && s.gsub!(/^exp/, '') && translator.exp(s)
45
+ s.split(' ').each{|w| translator.t(w)}
46
+ end
47
+
48
+ data = {
49
+ sl: translator.sl,
50
+ tl: translator.tl
51
+ }
52
+
53
+ File.open(fname, "w") {|f| f.write(data.to_yaml) }
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gg_translator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gg_translator"
8
+ spec.version = GgTranslator::VERSION
9
+ spec.authors = ["Serge Kislak"]
10
+ spec.email = ["kislak7@gmail.com"]
11
+ spec.description = %q{commandline repl translator}
12
+ spec.summary = %q{run: gg}
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.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,3 @@
1
+ module GgTranslator
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,96 @@
1
+ require "gg_translator/version"
2
+
3
+ module GgTranslator
4
+ module Joiner
5
+ def joins(s1, s2)
6
+ sub1 = ' ' * s1.map{|s| s.size}.max + ' '
7
+ sub2 = ' ' * s2.map{|s| s.size}.max + ' '
8
+ i = 0
9
+ res = []
10
+ while (i <= s1.size-1 || i <= s2.size-1) do
11
+ res[i] = (s1[i] && s1[i].ljust(sub1.size) or sub1) + (s2[i] && s2[i].ljust(sub2.size) or sub2)
12
+ i+=1
13
+ end
14
+ res
15
+ end
16
+ end
17
+
18
+ class Translator
19
+ include Joiner
20
+ require 'rubygems'
21
+ require 'net/http'
22
+ require 'uri'
23
+ require 'json'
24
+ attr_accessor :sl, :tl, :last
25
+
26
+ def initialize(sl = 'auto', tl = 'ru')
27
+ @sl = sl
28
+ @tl = tl
29
+ end
30
+
31
+ def t(text)
32
+ base_url = '/translate_a/t?client=j&pc=0&oc=1&hl=en&ie=UTF-8&oe=UTF-8'
33
+ req = "#{base_url}&text=#{text}&sl=#{@sl}&tl=#{@tl}"
34
+ result = ask_google(req)
35
+ @sl = result['src'] #remember language
36
+ print_result(result)
37
+ end
38
+
39
+ def exp(text = nil)
40
+ text.strip!
41
+ text ||= Readline::HISTORY.to_a.last
42
+ text = URI.encode(text)
43
+ req = "/translate_a/ex?sl=#{@sl}&tl=#{@tl}&q=#{text}"
44
+ res = ask_google(req)[0][0]
45
+ res && res.map{|e| [e.first, e[3]]}.each do |s, translation|
46
+ s = s.split(/<b>|<\/b>/)
47
+ puts "#{s[0]}#{s[1].upcase}#{s[2]}"
48
+ s = translation.split(/<b>|<\/b>/)
49
+ puts "#{s[0]}**#{s[1].upcase}**#{s[2]}"
50
+ puts '-'*80
51
+ end
52
+ true
53
+ end
54
+
55
+ private
56
+ def ask_google(req)
57
+ proxy = URI.parse(ENV['http_proxy']) if ENV['http_proxy']
58
+ worker = proxy ? Net::HTTP::Proxy(proxy.host, proxy.port) : Net::HTTP
59
+ JSON.parse(worker.get_response('translate.google.com', req).body)
60
+ end
61
+
62
+ def print_result(result)
63
+ sentences = []
64
+ result['sentences'].each do |s|
65
+ sentences << "#{s['trans']}"
66
+ end
67
+ puts ''
68
+ puts '-'*80
69
+ puts "#{result['sentences'][0]['orig']}: #{sentences.join(', ')} (#{result['src']})"
70
+
71
+ if result['dict']
72
+ s = []
73
+ result['dict'].each_with_index do |d, i|
74
+ s[i] = ["#{d['pos']}: "]
75
+ d['terms'].each_with_index do |t|
76
+ (s[i] ||= []) << "- #{t}"
77
+ end
78
+ end
79
+
80
+ i = 0
81
+ res = ['']
82
+ while i < s.size do
83
+ if res[0].size > 50
84
+ puts '-' * 80
85
+ puts res
86
+ res = ['']
87
+ end
88
+ res = joins(res, s[i])
89
+ i+=1
90
+ end
91
+ puts '-' * 80
92
+ puts res
93
+ end
94
+ end
95
+ end
96
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gg_translator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Serge Kislak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-25 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
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: commandline repl translator
42
+ email:
43
+ - kislak7@gmail.com
44
+ executables:
45
+ - gg
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/gg
55
+ - gg_translator.gemspec
56
+ - lib/gg_translator.rb
57
+ - lib/gg_translator/version.rb
58
+ homepage: ''
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.0.0.rc.2
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: 'run: gg'
82
+ test_files: []
83
+ has_rdoc: