yatran 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +27 -9
- data/Rakefile +8 -0
- data/bin/yatran +41 -0
- data/lib/yatran.rb +1 -0
- data/lib/yatran/cli.rb +130 -0
- data/lib/yatran/translatable.rb +11 -11
- data/lib/yatran/version.rb +1 -1
- data/spec/spec_helper.rb +22 -0
- data/spec/string_spec.rb +25 -0
- data/yatran.gemspec +4 -2
- metadata +21 -8
data/README.md
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# Yatran
|
2
|
-
|
3
1
|
Ruby unofficial client for Yandex translation engine
|
4
2
|
|
5
3
|
|
@@ -54,14 +52,34 @@ Or install it yourself as:
|
|
54
52
|
|
55
53
|
More info
|
56
54
|
|
57
|
-
English version: http://translate.yandex.net/tr-url/en+ru.ru/api.yandex.ru/translate/ (
|
55
|
+
English version: http://translate.yandex.net/tr-url/en+ru.ru/api.yandex.ru/translate/ (translated automatically)
|
58
56
|
Russian version: http://api.yandex.ru/translate/
|
59
57
|
|
60
58
|
|
61
|
-
##
|
59
|
+
## CLI
|
60
|
+
/bin/yaran is a cli utility to use yandex translation API
|
61
|
+
|
62
|
+
./yatran -h
|
63
|
+
|
64
|
+
Yandex Command line translations.
|
65
|
+
Available language directions : ru-en,ru-pl,ru-uk,ru-de,ru-fr,ru-es,ru-it,ru-tr,en-ru,en-uk,en-tr,pl-ru,uk-ru,uk-en,de-ru,fr-ru,es-ru,it-ru,tr-ru,tr-en
|
66
|
+
Usage: yatran COMMAND [OPTIONS]
|
67
|
+
|
68
|
+
|
69
|
+
Commands
|
70
|
+
|
71
|
+
d: detect text language
|
72
|
+
t: translate text
|
73
|
+
|
74
|
+
Options
|
75
|
+
|
76
|
+
-p, --phrase Phrase [OPT] Phrase to translate or detect
|
77
|
+
-s, --source FILE [OPT] File with text to translate
|
78
|
+
-o, --output FILE [OPT] File with output
|
79
|
+
-f, --from FROM_LANG [OPT] Language of phrase/text
|
80
|
+
-t, --to TO_LANG [OPT] Output language
|
81
|
+
-l, --language LANGDIR [OPT] Available language direction
|
82
|
+
-d, --debug Debug flag
|
83
|
+
|
84
|
+
|
62
85
|
|
63
|
-
1. Fork it
|
64
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
-
3. Commit your changes (`git commit -am 'Added some feature'`)
|
66
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
-
5. Create new Pull Request
|
data/Rakefile
CHANGED
data/bin/yatran
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$:.unshift File.dirname(__FILE__) + "/../lib"
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'yatran'
|
7
|
+
require "yatran/cli"
|
8
|
+
require 'optparse'
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
tranlsator = Yatran::CLI.new
|
16
|
+
options = tranlsator.instance_variable_get(:@options)
|
17
|
+
#exit
|
18
|
+
|
19
|
+
case ARGV[0]
|
20
|
+
when "d"
|
21
|
+
puts "call detect with options #{options.inspect}" if tranlsator[:debug]
|
22
|
+
begin
|
23
|
+
tranlsator.detect!
|
24
|
+
rescue => e
|
25
|
+
puts e.message
|
26
|
+
end
|
27
|
+
when "t"
|
28
|
+
puts "call translate on options #{options.inspect}" if tranlsator[:debug]
|
29
|
+
begin
|
30
|
+
tranlsator.translate!
|
31
|
+
rescue => e
|
32
|
+
p e.message
|
33
|
+
end
|
34
|
+
else
|
35
|
+
puts "Undefined command #{ARGV[0]}"
|
36
|
+
#
|
37
|
+
exit
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
|
data/lib/yatran.rb
CHANGED
data/lib/yatran/cli.rb
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2012 Igor Fedoronchuk
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#++
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
module Yatran
|
28
|
+
|
29
|
+
class Error < StandardError
|
30
|
+
end
|
31
|
+
|
32
|
+
class CLI
|
33
|
+
|
34
|
+
def initialize()
|
35
|
+
@options = {}
|
36
|
+
languages = Yatran::LANGUAGES_TRANSLATIONS.join(',')
|
37
|
+
OptionParser.new do |opts|
|
38
|
+
opts.banner = <<EOS
|
39
|
+
Yandex Command line translations.
|
40
|
+
Available language directions : #{languages}
|
41
|
+
Usage: yatran COMMAND [OPTIONS]
|
42
|
+
|
43
|
+
EOS
|
44
|
+
opts.separator ""
|
45
|
+
opts.separator "Commands"
|
46
|
+
opts.separator " d: detect text language"
|
47
|
+
opts.separator " t: translate text"
|
48
|
+
opts.separator ""
|
49
|
+
opts.separator "Options"
|
50
|
+
opts.on( '-p', '--phrase Phrase [OPT]', "Phrase to translate or detect" ) do |source|
|
51
|
+
@options[:text] = source
|
52
|
+
end
|
53
|
+
opts.on( '-s', '--source FILE [OPT]', "File with text to translate" ) do |source|
|
54
|
+
@options[:file] = source
|
55
|
+
end
|
56
|
+
opts.on( '-o', '--output FILE [OPT]', "File with output" ) do |output|
|
57
|
+
@options[:output] = output
|
58
|
+
end
|
59
|
+
opts.on("-f", "--from FROM_LANG [OPT]", "Language of phrase/text ") do |from|
|
60
|
+
@options[:from] = from
|
61
|
+
end
|
62
|
+
opts.on("-t", "--to TO_LANG [OPT]", "Output language ") do |to|
|
63
|
+
@options[:to] = to
|
64
|
+
end
|
65
|
+
opts.on("-l", "--language LANGDIR [OPT]", "Available language direction") do |d|
|
66
|
+
@options[:direction] = d
|
67
|
+
end
|
68
|
+
opts.on( '-d', '--debug', "Debug flag" ) do
|
69
|
+
@options[:debug] = true
|
70
|
+
end
|
71
|
+
end.parse!
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
def translate!
|
77
|
+
validate_text!
|
78
|
+
validate_direction!
|
79
|
+
result = @text.send(@direction)
|
80
|
+
if @options[:output]
|
81
|
+
raise Error.new("File #{@opts[:output]} already exists") if File.exists? @options[:output]
|
82
|
+
File.open(@options[:output], 'w') { |f| f.write(result) }
|
83
|
+
puts "check file #{@options[:output]}"
|
84
|
+
|
85
|
+
else
|
86
|
+
puts "Translated:"
|
87
|
+
puts result
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
def detect!
|
93
|
+
validate_text!
|
94
|
+
puts "Detected: #{@text.language}"
|
95
|
+
end
|
96
|
+
|
97
|
+
def [](index)
|
98
|
+
@options[index]
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
def validate_text!
|
104
|
+
@text = @options[:text]
|
105
|
+
|
106
|
+
|
107
|
+
unless @text
|
108
|
+
if @options[:source]
|
109
|
+
raise Error.new("File #{@options[:source]} doesn't exist") unless File.exists? @options[:source]
|
110
|
+
@text = IO.read @options[:source]
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
raise Error.new(" Text is undefined ") if @text.nil?
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
def validate_direction!
|
119
|
+
@direction = @options[:direction]
|
120
|
+
unless @direction
|
121
|
+
raise Error.new(" Undefined translation direction ") unless ( @options[:from] and @options[:to] )
|
122
|
+
@direction = "#{@options[:from]}-#{@options[:to]}"
|
123
|
+
end
|
124
|
+
@direction = @direction.to_sym
|
125
|
+
raise Error.new(" #{@direction} translation is not supported ") unless @text.respond_to?(@direction)
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
data/lib/yatran/translatable.rb
CHANGED
@@ -27,7 +27,7 @@ module Yatran
|
|
27
27
|
|
28
28
|
|
29
29
|
def translate(translation_direction)
|
30
|
-
|
30
|
+
|
31
31
|
API.request('translate', {:lang=> translation_direction, :text=>self})['text'][0]
|
32
32
|
|
33
33
|
end
|
@@ -58,6 +58,7 @@ module Yatran
|
|
58
58
|
# @private
|
59
59
|
|
60
60
|
def respond_to?(method, include_private = false)
|
61
|
+
|
61
62
|
return true unless translation_direction(method.to_s).nil?
|
62
63
|
super(method, include_private)
|
63
64
|
end
|
@@ -66,18 +67,17 @@ module Yatran
|
|
66
67
|
|
67
68
|
def translation_direction(method)
|
68
69
|
|
69
|
-
case method
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
method["_"] = "-"
|
70
|
+
method = case method
|
71
|
+
when /^to_(.*)_from_(.*)$/
|
72
|
+
then "#{$2}-#{$1}"
|
73
|
+
when /^from_(.*)_to_(.*)$/
|
74
|
+
then "#{$1}-#{$2}"
|
75
|
+
when /^(.*)_(.*)$/
|
76
|
+
then "#{$1}-#{$2}"
|
77
77
|
end
|
78
78
|
|
79
|
-
method
|
80
|
-
|
79
|
+
(LANGUAGES_TRANSLATIONS.include? method ) ? method : nil
|
80
|
+
|
81
81
|
|
82
82
|
end
|
83
83
|
|
data/lib/yatran/version.rb
CHANGED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
|
13
|
+
# Run specs in random order to surface order dependencies. If you find an
|
14
|
+
# order dependency and want to debug it, you can fix the order by providing
|
15
|
+
# the seed, which is printed after each run.
|
16
|
+
# --seed 1234
|
17
|
+
config.order = 'random'
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
require File.dirname(__FILE__) + '/../lib/yatran'
|
22
|
+
#require File.dirname(__FILE__) + '/../lib/yatran/cli'
|
data/spec/string_spec.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
string = "string"
|
4
|
+
|
5
|
+
describe string do
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
it { should respond_to :language }
|
10
|
+
it { should respond_to :lang }
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
Yatran::LANGUAGES_TRANSLATIONS.each do |lang|
|
15
|
+
pairs = lang.split "-"
|
16
|
+
|
17
|
+
it { should respond_to "from_#{pairs[0]}_to_#{pairs[1]}" }
|
18
|
+
it { should respond_to "to_#{pairs[1]}_from_#{pairs[0]}" }
|
19
|
+
it { should respond_to "#{pairs[0]}_#{pairs[1]}" }
|
20
|
+
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
end
|
data/yatran.gemspec
CHANGED
@@ -3,14 +3,16 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
3
3
|
require File.expand_path('../lib/yatran/version', __FILE__)
|
4
4
|
|
5
5
|
Gem::Specification.new do |gem|
|
6
|
-
gem.authors = ["Igor"]
|
6
|
+
gem.authors = ["Igor Fedoronchuk"]
|
7
7
|
gem.email = ["fedoronchuk@gmail.com"]
|
8
8
|
gem.description = %q{unofficial http://api.yandex.ru/translate API Client for ruby }
|
9
9
|
gem.summary = %q{ automatic translations and language detecting }
|
10
10
|
gem.homepage = "https://github.com/Fivell/yatran"
|
11
11
|
|
12
12
|
|
13
|
-
gem.add_dependency
|
13
|
+
gem.add_dependency 'json'
|
14
|
+
gem.add_development_dependency "rspec"
|
15
|
+
|
14
16
|
|
15
17
|
|
16
18
|
gem.files = `git ls-files`.split($\)
|
metadata
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yatran
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
|
-
- Igor
|
8
|
+
- Igor Fedoronchuk
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirement: &70254395693740 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,16 +21,23 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: *70254395693740
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70254395693200 !ruby/object:Gem::Requirement
|
25
28
|
none: false
|
26
29
|
requirements:
|
27
30
|
- - ! '>='
|
28
31
|
- !ruby/object:Gem::Version
|
29
32
|
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70254395693200
|
30
36
|
description: ! 'unofficial http://api.yandex.ru/translate API Client for ruby '
|
31
37
|
email:
|
32
38
|
- fedoronchuk@gmail.com
|
33
|
-
executables:
|
39
|
+
executables:
|
40
|
+
- yatran
|
34
41
|
extensions: []
|
35
42
|
extra_rdoc_files: []
|
36
43
|
files:
|
@@ -39,14 +46,18 @@ files:
|
|
39
46
|
- LICENSE
|
40
47
|
- README.md
|
41
48
|
- Rakefile
|
49
|
+
- bin/yatran
|
42
50
|
- lib/yatran.rb
|
43
51
|
- lib/yatran/api.rb
|
52
|
+
- lib/yatran/cli.rb
|
44
53
|
- lib/yatran/detectable.rb
|
45
54
|
- lib/yatran/error.rb
|
46
55
|
- lib/yatran/languages.rb
|
47
56
|
- lib/yatran/translatable.rb
|
48
57
|
- lib/yatran/url.rb
|
49
58
|
- lib/yatran/version.rb
|
59
|
+
- spec/spec_helper.rb
|
60
|
+
- spec/string_spec.rb
|
50
61
|
- yatran.gemspec
|
51
62
|
homepage: https://github.com/Fivell/yatran
|
52
63
|
licenses: []
|
@@ -68,8 +79,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
79
|
version: '0'
|
69
80
|
requirements: []
|
70
81
|
rubyforge_project:
|
71
|
-
rubygems_version: 1.8.
|
82
|
+
rubygems_version: 1.8.17
|
72
83
|
signing_key:
|
73
84
|
specification_version: 3
|
74
85
|
summary: automatic translations and language detecting
|
75
|
-
test_files:
|
86
|
+
test_files:
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
- spec/string_spec.rb
|