langouste 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ ## PROJECT::GENERAL
2
+ coverage
3
+ rdoc
4
+ pkg
5
+
6
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Hariton Mizgir
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = langouste
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Hariton Mizgir. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "langouste"
8
+ gem.summary = %Q{Console tool for translation through various online services (google translate, babelfish, pereklad, etc)}
9
+ gem.email = "hmizgir@gmail.com"
10
+ gem.homepage = "http://github.com/hariton/langouste"
11
+ gem.executables = %w(translate)
12
+ gem.authors = ["Hariton Mizgir"]
13
+ gem.add_dependency "mechanize", ">= 1.0.0"
14
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ gem.files.include %w(config/langouste.yaml)
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/test_*.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ task :default => :test
46
+
47
+ require 'rake/rdoctask'
48
+ Rake::RDocTask.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "langouste #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/translate ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'langouste'
4
+
5
+ options = {
6
+ :from_lang => :russian,
7
+ :to_lang => :english,
8
+ :service => :google,
9
+ }
10
+
11
+ if ARGV.any?
12
+ require 'optparse'
13
+ opts = OptionParser.new {|op|
14
+ op.banner = "Usage: #{File.basename($0)} [options] text"
15
+
16
+ op.on('-f language', '--from language', "") {|v| options[:from_lang] = v.to_sym }
17
+ op.on('-t language', '--to language', "") {|v| options[:to_lang] = v.to_sym }
18
+ op.on('-l', '--list', 'List of available services') { puts Langouste.list }
19
+ op.on('-s', '--service services', "") {|v| options[:services] = v.to_sym }
20
+ op.on('-c path', '--config path', "Path to config yaml-file") {|v| options[:config] = v }
21
+ op.on('-h', '--help', 'Shows this help message') {puts op; exit}
22
+
23
+ }.parse!(ARGV.dup)
24
+ text = opts.join(' ')
25
+ end
26
+
27
+ puts Langouste.new(options).translate(text)
@@ -0,0 +1,186 @@
1
+ abbreviations:
2
+ languages:
3
+ :ru: :russian
4
+ :r: :russian
5
+ :en: :english
6
+ :e: :english
7
+ :ua: :ukrainian
8
+ :u: :ukrainian
9
+ :es: :spanish
10
+ :de: :german
11
+ :fr: :french
12
+ :it: :italian
13
+ services:
14
+ :g: :google
15
+ :m: :meta
16
+ :b: :babelfish
17
+ :p: :pereklad
18
+ :t: :prompt
19
+
20
+ # полный список: af, sq, ar, be, bg, ca, zh-CN, zh-TW, hr, cs, da, nl, en, et, tl, fi, fr, gl, de, el, ht, iw, hi, hu, is, id, ga, it, ja, ko, lv, lt, mk, ms, mt, no, fa, pl, pt, ro, ru, sr, sk, sl, es, sw, sv, th, tr, uk, vi, cy, yi
21
+ google_languages: &google_languages
22
+ :russian: ru
23
+ :english: en
24
+ :spanish: es
25
+ :ukrainian: uk
26
+ :german: de
27
+ :french: fr
28
+ :italian: it
29
+
30
+ pereklad_languages: &pereklad_languages
31
+ :russian: Rus
32
+ :english: Eng
33
+ :spanish: es
34
+ :ukrainian: Ukr
35
+ :german: Ger
36
+ :french: Fre
37
+ :italian: it
38
+
39
+ :google:
40
+ url: 'http://translate.google.com'
41
+ input:
42
+ form:
43
+ selector: {:name: 'text_form'}
44
+ from:
45
+ field:
46
+ selector: {:name: 'sl'}
47
+ languages:
48
+ <<: *google_languages
49
+ to:
50
+ field:
51
+ selector: {:name: 'tl'}
52
+ languages:
53
+ <<: *google_languages
54
+ text:
55
+ selector: {:name: 'text'}
56
+ output:
57
+ form:
58
+ selector: {:action: '/translate_suggestion'}
59
+ text:
60
+ selector: {:name: 'gtrans'}
61
+
62
+ :babelfish:
63
+ url: 'http://babelfish.yahoo.com/'
64
+ input:
65
+ form:
66
+ selector: {:name: 'frmTrText'}
67
+ directions:
68
+ field:
69
+ selector: {:name: 'lp'}
70
+ directions:
71
+ :'english-dutch': en_nl
72
+ :'english-french': en_fr
73
+ :'english-german': en_de
74
+ :'english-greek': en_el
75
+ :'english-italian': en_it
76
+ :'english-japanese': en_ja
77
+ :'english-korean': en_ko
78
+ :'english-portuguese': en_pt
79
+ :'english-russian': en_ru
80
+ :'english-spanish': en_es
81
+ :'dutch-english': nl_en
82
+ :'dutch-french': nl_fr
83
+ :'french-dutch': fr_nl
84
+ :'french-english': fr_en
85
+ :'french-german': fr_de
86
+ :'french-greek': fr_el
87
+ :'french-italian': fr_it
88
+ :'french-portuguese': fr_pt
89
+ :'french-spanish': fr_es
90
+ :'german-english': de_en
91
+ :'german-french': de_fr
92
+ :'greek-english': el_en
93
+ :'greek-french': el_fr
94
+ :'italian-english': it_en
95
+ :'italian-french': it_fr
96
+ :'japanese-english': ja_en
97
+ :'korean-english': ko_en
98
+ :'portuguese-english': pt_en
99
+ :'portuguese-french': pt_fr
100
+ :'russian-english': ru_en
101
+ :'spanish-english': es_en
102
+ :'spanish-french': es_fr
103
+ text:
104
+ selector: {:name: 'trtext'}
105
+ output:
106
+ xpath: '//div[@id="result"]/div/text()'
107
+
108
+ :pereklad:
109
+ url: 'http://pereklad.online.ua'
110
+ input:
111
+ form:
112
+ selector: {:name: 'transl'}
113
+ from:
114
+ field:
115
+ selector: {:name: 'TranFrom'}
116
+ languages:
117
+ <<: *pereklad_languages
118
+ to:
119
+ field:
120
+ selector: {:name: 'TranTo'}
121
+ languages:
122
+ <<: *pereklad_languages
123
+ text:
124
+ selector: {:name: 'SrcTxt'}
125
+ output:
126
+ form:
127
+ selector: {:name: 'transl'}
128
+ text:
129
+ selector: {:name: 'DstTxt'}
130
+
131
+ :meta:
132
+ url: 'http://translate.meta.ua'
133
+ input:
134
+ form:
135
+ selector: {:name: 'form1'}
136
+ directions:
137
+ field:
138
+ selector: {:name: 'language'}
139
+ directions:
140
+ :'russian-english': Rus-Eng
141
+ :'english-russian': Eng-Rus
142
+ :'ukrainian-english': Ukr-Eng
143
+ :'english-ukrainian': Eng-Ukr
144
+ :'russian-ukrainian': Rus-Ukr
145
+ :'ukrainian-russian': Ukr-Rus
146
+ :'russian-german': Rus-Ger
147
+ :'german-russian': Ger-Rus
148
+ :'ukrainian-german': Ukr-Ger
149
+ :'german-ukrainian': Ger-Ukr
150
+ :'english-german': Eng-Ger
151
+ :'german-english': Ger-Eng
152
+ :'russian-latvian': Rus-Lat
153
+ :'latvian-russian': Lat-Rus
154
+ :'ukrainian-latvian': Ukr-Lat
155
+ :'latvian-ukrainian': Lat-Ukr
156
+ :'english-latvian': Eng-Lat
157
+ :'latvian-english': Lat-Eng
158
+ :'german-latvian': Ger-Lat
159
+ :'latvian-german': Lat-Ger
160
+ text:
161
+ selector: {:name: 'SrcTxt'}
162
+ output:
163
+ form:
164
+ selector: {:name: 'form1'}
165
+ text:
166
+ selector: {:name: 'DstTxt'}
167
+
168
+ :prompt:
169
+ url: 'http://online.perevodov.net/Perevodov.net/online-dictionaries/promt.php4'
170
+ input:
171
+ form:
172
+ selector: {:name: 'trans'}
173
+ directions:
174
+ field:
175
+ selector: {:name: 'direction'}
176
+ directions:
177
+ :'russian-english': re
178
+ :'english-russian': er
179
+ text:
180
+ selector: {:name: 'source'}
181
+ output:
182
+ form:
183
+ selector: {:name: 'trans'}
184
+ text:
185
+ selector: {:name: 'result'}
186
+
data/langouste.gemspec ADDED
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{langouste}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Hariton Mizgir"]
12
+ s.date = %q{2010-05-05}
13
+ s.default_executable = %q{translate}
14
+ s.email = %q{hmizgir@gmail.com}
15
+ s.executables = ["translate"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".gitignore",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "bin/translate",
28
+ "config/langouste.yaml",
29
+ "langouste.gemspec",
30
+ "lib/langouste.rb",
31
+ "test/helper.rb",
32
+ "test/test_langouste.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/hariton/langouste}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.6}
38
+ s.summary = %q{Console tool for translation through various online services (google translate, babelfish, pereklad, etc)}
39
+ s.test_files = [
40
+ "test/test_langouste.rb",
41
+ "test/helper.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<mechanize>, [">= 1.0.0"])
50
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<mechanize>, [">= 1.0.0"])
53
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<mechanize>, [">= 1.0.0"])
57
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
58
+ end
59
+ end
60
+
data/lib/langouste.rb ADDED
@@ -0,0 +1,118 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'mechanize'
5
+
6
+ class Langouste
7
+ attr_accessor :from_lang, :to_lang, :service, :config
8
+
9
+ @@config_path = File.join(File.expand_path(File.dirname(__FILE__)), '../config/langouste.yaml')
10
+
11
+ def self.config
12
+ @@config ||= YAML.load(File.open(@@config_path))
13
+ end
14
+
15
+ def self.config=(config)
16
+ @@config = config
17
+ end
18
+
19
+ def initialize(options = {})
20
+ options = {
21
+ :from_lang => :russian,
22
+ :to_lang => :english,
23
+ :service => :google,
24
+ :config => @@config_path
25
+ }.merge options
26
+
27
+ Langouste.config = YAML.load(File.open(options[:config]))
28
+
29
+ @from_lang = deabbreviate_language(options[:from_lang])
30
+ @to_lang = deabbreviate_language(options[:to_lang])
31
+ @service = deabbreviate_service(options[:service])
32
+ @config = Langouste.config[@service]
33
+ end
34
+
35
+ def translate(input_text)
36
+
37
+ output_text = ''
38
+
39
+ begin
40
+ input_form = config['input']['form']
41
+ rescue
42
+ raise "Bad service: '#{service}'"
43
+ end
44
+
45
+ # если у переводчика нет нужных языков - запрос не выполняется
46
+ if input_form['to'] and input_form['from']
47
+ return '' unless input_form['to']['languages'][to_lang] and input_form['from']['languages'][from_lang]
48
+ elsif input_form['directions']
49
+ return '' unless input_form['directions']['directions']["#{from_lang}-#{to_lang}".to_sym]
50
+ else
51
+ raise "Bad YAML-config for service: '#{service}'"
52
+ end
53
+
54
+ agent = Mechanize.new {|a| a.user_agent_alias = 'Linux Konqueror'}
55
+
56
+ agent.get(config['url']) do |page|
57
+
58
+ translated_page = page.form_with(input_form['selector']) do |form|
59
+
60
+ text = form.field_with(input_form['text']['selector'])
61
+ text.value = input_text
62
+
63
+ if input_form['directions'] and input_form['directions']['field']['selector']
64
+
65
+ value = input_form['directions']['directions']["#{from_lang}-#{to_lang}".to_sym]
66
+ direction = form.field_with(input_form['directions']['field']['selector'])
67
+ direction.option_with(:value => value).select
68
+
69
+ else
70
+
71
+ if input_form['from'] and input_form['from']['field'] and input_form['from']['field']['selector']
72
+
73
+ value = input_form['from']['languages'][from_lang]
74
+ from_lang = form.field_with(input_form['from']['field']['selector'])
75
+ from_lang.option_with(:value => value).select
76
+
77
+ end
78
+
79
+ if input_form['to'] and input_form['to']['field'] and input_form['to']['field']['selector']
80
+
81
+ value = input_form['to']['languages'][to_lang]
82
+ to_lang = form.field_with(input_form['to']['field']['selector'])
83
+ to_lang.option_with(:value => value).select
84
+
85
+ end
86
+
87
+ end
88
+
89
+ end.submit
90
+
91
+ output_text = if config['output']['xpath']
92
+ xpath = config['output']['xpath']
93
+ translated_page.search(xpath)
94
+ else
95
+ output_form = config['output']['form']
96
+ translated_page.form_with(output_form['selector']).field_with(output_form['text']['selector']).value
97
+ end
98
+
99
+ end
100
+
101
+ output_text.to_s
102
+ end
103
+
104
+ def self.list(config_path = nil)
105
+ Langouste.config.keys.select{|k| k.is_a?(Symbol)}
106
+ end
107
+
108
+ private
109
+
110
+ def deabbreviate_language(language)
111
+ Langouste.config['abbreviations']['languages'][language] || language
112
+ end
113
+
114
+ def deabbreviate_service(services)
115
+ Langouste.config['abbreviations']['services'][services] || services
116
+ end
117
+
118
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'langouste'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestLangouste < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: langouste
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Hariton Mizgir
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-05 00:00:00 +04:00
18
+ default_executable: translate
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: mechanize
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 0
30
+ - 0
31
+ version: 1.0.0
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: thoughtbot-shoulda
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ description:
47
+ email: hmizgir@gmail.com
48
+ executables:
49
+ - translate
50
+ extensions: []
51
+
52
+ extra_rdoc_files:
53
+ - LICENSE
54
+ - README.rdoc
55
+ files:
56
+ - .document
57
+ - .gitignore
58
+ - LICENSE
59
+ - README.rdoc
60
+ - Rakefile
61
+ - VERSION
62
+ - bin/translate
63
+ - config/langouste.yaml
64
+ - langouste.gemspec
65
+ - lib/langouste.rb
66
+ - test/helper.rb
67
+ - test/test_langouste.rb
68
+ has_rdoc: true
69
+ homepage: http://github.com/hariton/langouste
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --charset=UTF-8
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ requirements: []
92
+
93
+ rubyforge_project:
94
+ rubygems_version: 1.3.6
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Console tool for translation through various online services (google translate, babelfish, pereklad, etc)
98
+ test_files:
99
+ - test/test_langouste.rb
100
+ - test/helper.rb