reverso 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/License ADDED
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2011, Iwakura Taro
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification,
5
+ are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ * Neither the name of the copyright owner nor the names of its contributors
15
+ may be used to endorse or promote products derived from this software
16
+ without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README ADDED
@@ -0,0 +1,35 @@
1
+ == reverso
2
+
3
+ Interface for reverso.net translation service.
4
+
5
+ Includes command-line tool named _reverso_.
6
+
7
+
8
+ == Usage
9
+
10
+ require 'rubygems'
11
+ require 'reverso'
12
+ Reverso::Translator.translate('The love', :from => 'En', :to => 'German')
13
+
14
+
15
+ == Requirements
16
+
17
+ * json (tested with json-1.5.1 gem)
18
+
19
+
20
+ == Install
21
+
22
+ gem install reverso
23
+
24
+
25
+ == Author
26
+
27
+ Iwakura Taro, taro@mail333.com
28
+
29
+
30
+ == License
31
+
32
+ Application is released under the terms of the BSD License.
33
+ See the License file for details.
34
+
35
+
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.test_files = FileList['test/test_*.rb']
6
+ test.verbose = false
7
+ test.warning = true
8
+ end
9
+
10
+ task :default => :test
11
+
data/bin/reverso ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'reverso'
4
+ require 'reverso/cli'
5
+
6
+ Reverso::CLI.run
7
+
@@ -0,0 +1,197 @@
1
+ module Reverso # :nodoc:
2
+
3
+ TRANSLATION_CODES = {
4
+ 'Arabic' => {
5
+ 'Dutch' => 'ara-dut-5',
6
+ 'Portuguese' => 'ara-por-5',
7
+ 'English' => 'ara-eng-5',
8
+ 'Russian' => 'ara-rus-5',
9
+ 'Chinese' => 'ara-chi-5',
10
+ 'Spanish' => 'ara-spa-5',
11
+ 'Hebrew' => 'ara-heb-5',
12
+ 'French' => 'ara-fra-5',
13
+ 'German' => 'ara-ger-5',
14
+ 'Romanian' => 'ara-rum-5',
15
+ 'Italian' => 'ara-ita-5',
16
+ 'Japanese' => 'ara-jpn-5'
17
+ },
18
+ 'Chinese' => {
19
+ 'Dutch' => 'chi-dut-5',
20
+ 'Portuguese' => 'chi-por-5',
21
+ 'English' => 'chi-eng-5',
22
+ 'Russian' => 'chi-rus-5',
23
+ 'Spanish' => 'chi-spa-5',
24
+ 'Hebrew' => 'chi-heb-5',
25
+ 'French' => 'chi-fra-5',
26
+ 'German' => 'chi-ger-5',
27
+ 'Romanian' => 'chi-rum-5',
28
+ 'Italian' => 'chi-ita-5',
29
+ 'Arabic' => 'chi-ara-5',
30
+ 'Japanese' => 'chi-jpn-5'
31
+ },
32
+ 'Dutch' => {
33
+ 'Portuguese' => 'dut-por-5',
34
+ 'English' => 'dut-eng-5',
35
+ 'Russian' => 'dut-rus-5',
36
+ 'Chinese' => 'dut-chi-5',
37
+ 'Spanish' => 'dut-spa-5',
38
+ 'Hebrew' => 'dut-heb-5',
39
+ 'French' => 'dut-fra-5',
40
+ 'German' => 'dut-ger-5',
41
+ 'Romanian' => 'dut-rum-5',
42
+ 'Italian' => 'dut-ita-5',
43
+ 'Arabic' => 'dut-ara-5',
44
+ 'Japanese' => 'dut-jpn-5'
45
+ },
46
+ 'English' => {
47
+ 'Dutch' => 'eng-dut-5',
48
+ 'Portuguese' => '1033-2070-2',
49
+ 'Russian' => '1033-1049-3',
50
+ 'Chinese' => 'eng-chi-5',
51
+ 'Spanish' => '2097153',
52
+ 'Hebrew' => '1033-1037-2',
53
+ 'French' => '524289',
54
+ 'German' => '262145',
55
+ 'Romanian' => 'eng-rum-5',
56
+ 'Italian' => '1033-1040-2',
57
+ 'Arabic' => 'eng-ara-5',
58
+ 'Japanese' => '1033-1041-2'
59
+ },
60
+ 'French' => {
61
+ 'Dutch' => 'fra-dut-5',
62
+ 'Portuguese' => '1036-2070-2',
63
+ 'English' => '65544',
64
+ 'Russian' => '1036-1049-2',
65
+ 'Chinese' => 'fra-chi-5',
66
+ 'Spanish' => '2097160',
67
+ 'Hebrew' => '1036-1037-2',
68
+ 'German' => '262152',
69
+ 'Romanian' => 'fra-rum-5',
70
+ 'Italian' => '1036-1040-2',
71
+ 'Arabic' => 'fra-ara-5',
72
+ 'Japanese' => '1036-1041-2'
73
+ },
74
+ 'Hebrew' => {
75
+ 'Dutch' => 'heb-dut-5',
76
+ 'Portuguese' => 'heb-por-5',
77
+ 'English' => '1037-1033-2',
78
+ 'Russian' => '1037-1049-2',
79
+ 'Chinese' => 'heb-chi-5',
80
+ 'Spanish' => '1037-1034-2',
81
+ 'French' => '1037-1036-2',
82
+ 'German' => 'heb-ger-5',
83
+ 'Romanian' => 'heb-rum-5',
84
+ 'Italian' => 'heb-ita-5',
85
+ 'Arabic' => 'heb-ara-5',
86
+ 'Japanese' => 'heb-jpn-5'
87
+ },
88
+ 'German' => {
89
+ 'Dutch' => 'ger-dut-5',
90
+ 'Portuguese' => '1031-2070-2',
91
+ 'English' => '65540',
92
+ 'Russian' => '1031-1049-2',
93
+ 'Chinese' => 'ger-chi-5',
94
+ 'Spanish' => '1031-1034-2',
95
+ 'Hebrew' => 'ger-heb-5',
96
+ 'French' => '524292',
97
+ 'Romanian' => 'ger-rum-5',
98
+ 'Italian' => 'ger-ita-5',
99
+ 'Arabic' => 'ger-ara-5',
100
+ 'Japanese' => 'ger-jpn-5'
101
+ },
102
+ 'Italian' => {
103
+ 'Dutch' => 'ita-dut-5',
104
+ 'Portuguese' => 'ita-por-5',
105
+ 'English' => '1040-1033-2',
106
+ 'Russian' => '1040-1049-2',
107
+ 'Chinese' => 'ita-chi-5',
108
+ 'Spanish' => '1040-1034-2',
109
+ 'Hebrew' => 'ita-heb-5',
110
+ 'French' => '1040-1036-2',
111
+ 'German' => 'ita-ger-5',
112
+ 'Romanian' => 'ita-rum-5',
113
+ 'Arabic' => 'ita-ara-5',
114
+ 'Japanese' => 'ita-jpn-5'
115
+ },
116
+ 'Japanese' => {
117
+ 'Dutch' => 'jpn-dut-5',
118
+ 'Portuguese' => 'jpn-por-5',
119
+ 'English' => '1041-1033-2',
120
+ 'Russian' => '1041-1049-2',
121
+ 'Chinese' => 'jpn-chi-5',
122
+ 'Spanish' => 'jpn-spa-5',
123
+ 'Hebrew' => 'jpn-heb-5',
124
+ 'French' => '1041-1036-2',
125
+ 'German' => 'jpn-ger-5',
126
+ 'Romanian' => 'jpn-rum-5',
127
+ 'Italian' => 'jpn-ita-5',
128
+ 'Arabic' => 'jpn-ara-5'
129
+ },
130
+ 'Portuguese' => {
131
+ 'Dutch' => 'por-dut-5',
132
+ 'English' => '2070-1033-2',
133
+ 'Russian' => 'por-rus-5',
134
+ 'Chinese' => 'por-chi-5',
135
+ 'Spanish' => '2070-1034-2',
136
+ 'Hebrew' => 'por-heb-5',
137
+ 'French' => '2070-1036-2',
138
+ 'German' => '2070-1031-2',
139
+ 'Romanian' => 'por-rum-5',
140
+ 'Italian' => 'por-ita-5',
141
+ 'Arabic' => 'por-ara-5',
142
+ 'Japanese' => 'por-jpn-5'
143
+ },
144
+ 'Russian' => {
145
+ 'Dutch' => 'rus-dut-5',
146
+ 'Portuguese' => 'rus-por-5',
147
+ 'English' => '1049-1033-3',
148
+ 'Chinese' => 'rus-chi-5',
149
+ 'Spanish' => '1049-1034-2',
150
+ 'Hebrew' => '1049-1037-2',
151
+ 'French' => '1049-1036-2',
152
+ 'German' => '1049-1031-2',
153
+ 'Romanian' => 'rus-rum-5',
154
+ 'Italian' => '1049-1040-2',
155
+ 'Arabic' => 'rus-ara-5',
156
+ 'Japanese' => '1049-1041-2'
157
+ },
158
+ 'Spanish' => {
159
+ 'Dutch' => 'spa-dut-5',
160
+ 'Portuguese' => '1034-2070-2',
161
+ 'English' => '65568',
162
+ 'Russian' => '1034-1049-2',
163
+ 'Chinese' => 'spa-chi-5',
164
+ 'Hebrew' => '1034-1037-2',
165
+ 'French' => '524320',
166
+ 'German' => '1034-1031-2',
167
+ 'Romanian' => 'spa-rum-5',
168
+ 'Italian' => '1034-1040-2',
169
+ 'Arabic' => 'spa-ara-5',
170
+ 'Japanese' => 'spa-jpn-5'
171
+ },
172
+ 'Romanian' => {
173
+ 'Dutch' => 'rum-dut-5',
174
+ 'Portuguese' => 'rum-por-5',
175
+ 'English' => 'rum-eng-5',
176
+ 'Russian' => 'rum-rus-5',
177
+ 'Chinese' => 'rum-chi-5',
178
+ 'Spanish' => 'rum-spa-5',
179
+ 'Hebrew' => 'rum-heb-5',
180
+ 'French' => 'rum-fra-5',
181
+ 'German' => 'rum-ger-5',
182
+ 'Italian' => 'rum-ita-5',
183
+ 'Arabic' => 'rum-ara-5',
184
+ 'Japanese' => 'rum-jpn-5'
185
+ }
186
+ }
187
+
188
+ SERVICE_HOST = 'www.reverso.net'
189
+ SERVICE_PATH = '/WebReferences/WSAJAXInterface.asmx/TranslateWS'
190
+ HEADERS = { 'Content-Type' => 'application/json; charset=utf-8',
191
+ 'User-Agent' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418 (KHTML, like Gecko) Safari/417.9.3',
192
+ 'X-Requested-With' => 'XMLHttpRequest',
193
+ 'Accept' => 'application/json, text/javascript, */*' }
194
+ # HACK! Used to pass restriction applied for requests from unauthorized users (translation truncation).
195
+ HACK_HEADERS = { 'Cookie' => 'reverso.net.user_id=afake=; reverso.net.username=afake=; reverso.net.loggedon=1;' }
196
+
197
+ end
@@ -0,0 +1,17 @@
1
+ module Reverso
2
+ module CLI # :nodoc:
3
+ def self.run
4
+ unless ARGV.size == 2
5
+ puts "Usage: #{File.basename $0} source_lang dest_lang"
6
+ exit
7
+ end
8
+ $/ = "\0"
9
+ puts 'Please type some text to translate followed by ^D:'
10
+ puts
11
+ phrase = STDIN.gets
12
+ puts
13
+ puts
14
+ puts Reverso::Translator::translate(phrase, :from => ARGV[0], :to => ARGV[1], :hack_auth => phrase.length > 500)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,61 @@
1
+ module Reverso
2
+ class Translator
3
+
4
+ =begin rdoc
5
+ Returns translated +phrase+
6
+
7
+ +params+ must contain following values:
8
+
9
+ :from source language name
10
+ :to destination language name
11
+
12
+ It is possible to use short language names.
13
+
14
+ #--
15
+
16
+ +params+ may contain following options:
17
+ :hack_auth set to non false value if you need translate big piece of text
18
+ =end
19
+
20
+ def self.translate(phrase, params = {})
21
+ new.translate(phrase, params)
22
+ end
23
+
24
+ # :enddoc:
25
+ def translate(phrase, params)
26
+ query_service(phrase, params)
27
+ result
28
+ end
29
+
30
+ def query_service(phrase, params)
31
+ @response = @http.post(SERVICE_PATH,
32
+ query_string(phrase, translation_code(params)),
33
+ (params[:hack_auth] ? HEADERS.merge(HACK_HEADERS) : HEADERS))
34
+ end
35
+
36
+ def response
37
+ JSON.parse @response.body
38
+ end
39
+
40
+ def result
41
+ response['d']['result']
42
+ end
43
+
44
+ def translation_code(params)
45
+ if from_pack = TRANSLATION_CODES.detect { |from, pack| from =~ /^#{params[:from]}/i } and
46
+ direction = from_pack[1].detect { |to, code| to =~ /^#{params[:to]}/i }
47
+ direction[1]
48
+ else
49
+ raise 'Unable to perform translation.'
50
+ end
51
+ end
52
+
53
+ def query_string(phrase, direction)
54
+ JSON(:searchText => phrase, :direction => direction)
55
+ end
56
+
57
+ def initialize # :nodoc:
58
+ @http = Net::HTTP.new(SERVICE_HOST)
59
+ end
60
+ end
61
+ end
data/lib/reverso.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ require 'reverso/base'
5
+ require 'reverso/translator'
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'fakeweb'
5
+ require 'reverso'
6
+
7
+ class TestReverso < Test::Unit::TestCase
8
+ context 'with valid language names' do
9
+ should 'perform translation' do
10
+ FakeWeb.register_uri(:post, /reverso\.net/, :body => '{"d":{"result":"Liebe"}}')
11
+ result = Reverso::Translator::translate('love', :from => 'English', :to => 'German')
12
+ assert_equal result, 'Liebe'
13
+ end
14
+
15
+ should 'understand short language names' do
16
+ FakeWeb.register_uri(:post, /reverso\.net/, :body => '{"d":{"result":"L\'amour"}}')
17
+ result = Reverso::Translator::translate('The love', :from => 'eng', :to => :fr)
18
+ assert_equal result, "L'amour"
19
+ end
20
+ end
21
+
22
+ context 'with unknown language names' do
23
+ should 'raise exception' do
24
+ assert_raise RuntimeError do
25
+ Reverso::Translator::translate('Two tickets, please.', :from => 'Eng', :to => 'Deutsch')
26
+ end
27
+ end
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: reverso
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Iwakura Taro
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-04 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: json
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: shoulda
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: fakeweb
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :development
61
+ version_requirements: *id003
62
+ description: " Interface for reverso.net translation service.\n Command-line tool included.\n"
63
+ email: taro@mail333.com
64
+ executables:
65
+ - reverso
66
+ extensions: []
67
+
68
+ extra_rdoc_files:
69
+ - README
70
+ - License
71
+ files:
72
+ - lib/reverso.rb
73
+ - lib/reverso/cli.rb
74
+ - lib/reverso/base.rb
75
+ - lib/reverso/translator.rb
76
+ - bin/reverso
77
+ - test/test_reverso.rb
78
+ - README
79
+ - Rakefile
80
+ - License
81
+ homepage: https://github.com/iwakura/reverso
82
+ licenses: []
83
+
84
+ post_install_message:
85
+ rdoc_options:
86
+ - --title
87
+ - reverso
88
+ - --main
89
+ - README
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ requirements: []
111
+
112
+ rubyforge_project:
113
+ rubygems_version: 1.7.2
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: Interface for reverso.net translation service.
117
+ test_files: []
118
+
119
+ has_rdoc: true