machine_translation 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,14 @@
1
+
2
+ Copyright (c) Eric Woodward.
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
7
+ is furnished to do so, subject to the following conditions:
8
+
9
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
12
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
13
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
14
+ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,8 @@
1
+
2
+ = Ruby gem for Machine Translations from Google
3
+
4
+ More details TBA.
5
+
6
+ == Copyright
7
+
8
+ Copyright (c) 2010 Eric Woodward.. See LICENSE for details. http://ejw.com.
data/Rakefile ADDED
@@ -0,0 +1,73 @@
1
+ ##
2
+ # Copyright (c) Eric Woodward.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ # modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
7
+ # is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
+ #
11
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
12
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
13
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
14
+ # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+ ##
16
+
17
+ require 'rubygems'
18
+ require 'rake'
19
+
20
+ begin
21
+ require 'jeweler'
22
+ Jeweler::Tasks.new do |gem|
23
+ gem.name = "machine_translation"
24
+ gem.summary = %Q{gem for getting a machine generated translation}
25
+ gem.description = %Q{gem for getting a machine generated translation from Google and hopefully other sources too (eventually)}
26
+
27
+ gem.email = "me@ejw.com"
28
+ gem.homepage = "http://github.com/ejw/machine_translation"
29
+ gem.authors = ["Eric Woodward"]
30
+ gem.rubyforge_project = "machine_translation"
31
+ gem.files = FileList['[A-Z]*', 'lib/**/*.rb', 'lib/tasks/*.rake', 'test/**/*.rb']
32
+
33
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
34
+ gem.add_dependency 'rest-client', '~> 1.6.0'
35
+ gem.add_dependency 'json', '>= 1.4.4'
36
+ end
37
+ Jeweler::GemcutterTasks.new
38
+ rescue LoadError
39
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
40
+ end
41
+
42
+ require 'rake/testtask'
43
+ Rake::TestTask.new(:test) do |test|
44
+ test.libs << 'lib' << 'test'
45
+ test.pattern = 'test/*test.rb'
46
+ test.verbose = true
47
+ end
48
+
49
+ begin
50
+ require 'rcov/rcovtask'
51
+ Rcov::RcovTask.new do |test|
52
+ test.libs << 'test'
53
+ test.pattern = 'test/*test.rb'
54
+ test.verbose = true
55
+ end
56
+ rescue LoadError
57
+ task :rcov do
58
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
59
+ end
60
+ end
61
+
62
+ task :test => :check_dependencies
63
+ task :default => :test
64
+
65
+ require 'rake/rdoctask'
66
+ Rake::RDocTask.new do |rdoc|
67
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
68
+
69
+ rdoc.rdoc_dir = 'rdoc'
70
+ rdoc.title = "machine_translation #{version}"
71
+ rdoc.rdoc_files.include('README*')
72
+ rdoc.rdoc_files.include('lib/**/*.rb')
73
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,46 @@
1
+ ##
2
+ # Copyright (c) Eric Woodward.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ # modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
7
+ # is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
+ #
11
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
12
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
13
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
14
+ # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+ ##
16
+
17
+ require 'rubygems'
18
+ require 'json'
19
+ require 'active_support'
20
+ require 'rest_client'
21
+
22
+ module MachineTranslation
23
+ class << self
24
+ ##
25
+ # *CONFIGURATION*
26
+ # Call the configure method within a application configuration initializer to set your Google Translate API key:
27
+ # MachineTranslation.configure do |config|
28
+ # config.google_key = 'APIKEY'
29
+ # config.google_referer = 'http://yoursite.com'
30
+ # end
31
+ ##
32
+
33
+ attr_writer :configuration
34
+ def configure
35
+ configuration ||= Configuration.new
36
+ yield(@configuration)
37
+ end
38
+ def configuration
39
+ @configuration ||= Configuration.new
40
+ end
41
+ end
42
+ end
43
+
44
+ require 'machine_translation/configuration'
45
+ require 'machine_translation/google'
46
+ require 'machine_translation/translator'
@@ -0,0 +1,29 @@
1
+ ##
2
+ # Copyright (c) Eric Woodward.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ # modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
7
+ # is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
+ #
11
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
12
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
13
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
14
+ # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+ ##
16
+
17
+ module MachineTranslation
18
+ class Configuration
19
+ attr_accessor :google_key, :google_referer
20
+
21
+ DEFAULT_GOOGLE_KEY = 'TBA'
22
+ DEFAULT_GOOGLE_REFERER = 'TBA'
23
+
24
+ def initialize
25
+ @google_key = DEFAULT_GOOGLE_KEY
26
+ @google_referer = DEFAULT_GOOGLE_REFERER
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,92 @@
1
+ ##
2
+ # Copyright (c) Eric Woodward.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ # modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
7
+ # is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
+ #
11
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
12
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
13
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
14
+ # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+ ##
16
+
17
+ module MachineTranslation
18
+ class Google
19
+ GOOGLE_API_HOST = 'http://ajax.googleapis.com'
20
+ GOOGLE_API_PATH = '/ajax/services/language/translate'
21
+ GOOGLE_API_VERSION = '1.0'
22
+ GOOGLE_LANGUAGES_PAIRS = {
23
+ 'af' => 'afrikaans',
24
+ 'sq' => 'albanian',
25
+ 'ar' => 'arabic',
26
+ 'be' => 'belarusian',
27
+ 'bg' => 'bulgarian',
28
+ 'zh-CN' => 'chinese simplified',
29
+ 'zh-TW' => 'chinese-traditional',
30
+ 'ca' => 'catalan',
31
+ 'hr' => 'croatian',
32
+ 'cs' => 'czech',
33
+ 'da' => 'danish',
34
+ 'nl' => 'dutch',
35
+ 'en' => 'english',
36
+ 'et' => 'estonian',
37
+ 'tl' => 'filipino',
38
+ 'fi' => 'finnish',
39
+ 'fr' => 'french',
40
+ 'gl' => 'galician',
41
+ 'de' => 'german',
42
+ 'el' => 'greek',
43
+ 'ht' => 'haitian creole',
44
+ 'he' => 'hebrew',
45
+ 'hi' => 'hindi',
46
+ 'hu' => 'hungarian',
47
+ 'is' => 'icelandic',
48
+ 'id' => 'indonesian',
49
+ 'ga' => 'irish',
50
+ 'it' => 'italian',
51
+ 'ja' => 'japanese',
52
+ 'ko' => 'korean',
53
+ 'lv' => 'latvian',
54
+ 'lt' => 'lithuanian',
55
+ 'mk' => 'macedonian',
56
+ 'ms' => 'malay',
57
+ 'mt' => 'maltese',
58
+ 'no' => 'norwegian',
59
+ 'fa' => 'persian',
60
+ 'pl' => 'polish',
61
+ 'pt' => 'portuguese',
62
+ 'ro' => 'romanian',
63
+ 'ru' => 'russian',
64
+ 'es' => 'spanish',
65
+ 'sr' => 'serbian',
66
+ 'sk' => 'slovak',
67
+ 'sl' => 'slovenian',
68
+ 'sw' => 'swahili',
69
+ 'sv' => 'swedish',
70
+ 'th' => 'thai',
71
+ 'tr' => 'turkish',
72
+ 'uk' => 'ukrainian',
73
+ 'vi' => 'vietnamese',
74
+ 'cy' => 'welsh',
75
+ 'yi' => 'yiddish'
76
+ }
77
+
78
+ class << self
79
+ def translate(from, to, content)
80
+ lp = "#{from}|#{to}"
81
+ begin
82
+ resource = RestClient::Resource.new(GOOGLE_API_HOST, { :headers => { 'Referer' => MachineTranslation.configuration.google_referer } })
83
+ response = resource["#{GOOGLE_API_PATH}?v=#{GOOGLE_API_VERSION}&langpair=#{CGI::escape(lp)}&q=#{CGI::escape(content)}"].get
84
+ case response.code
85
+ when 200
86
+ JSON.parse(response.body)['responseData']['translatedText']
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,29 @@
1
+ ##
2
+ # Copyright (c) Eric Woodward.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ # modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
7
+ # is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
+ #
11
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
12
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
13
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
14
+ # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+ ##
16
+
17
+ module MachineTranslation
18
+ class Translator
19
+ class << self
20
+ def translate(from, to, content)
21
+ ## Only support for Google at the moment, but this structure, requesting a translation from a Translator class,
22
+ ## allows for adding different sources for machine translations in the future within configuration, defaulted
23
+ ## to Google.
24
+
25
+ Google.translate(from, to, content)
26
+ end
27
+ end
28
+ end
29
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,26 @@
1
+ ##
2
+ # Copyright (c) Eric Woodward.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ # modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
7
+ # is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
+ #
11
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
12
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
13
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
14
+ # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+ ##
16
+
17
+ require 'rubygems'
18
+ require 'test/unit'
19
+ require 'shoulda'
20
+ require 'lib/machine_translation'
21
+
22
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
23
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
24
+
25
+ class Test::Unit::TestCase
26
+ end
@@ -0,0 +1,34 @@
1
+ ##
2
+ # Copyright (c) Eric Woodward.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ # modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
7
+ # is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
+ #
11
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
12
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
13
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
14
+ # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+ ##
16
+
17
+ require 'test/helper'
18
+
19
+ class TranslationTest < Test::Unit::TestCase
20
+ should "translate some english to english" do
21
+ result = MachineTranslation::Translator.translate('en', 'en', 'hello world')
22
+ ## puts "TRANSLATING some English to French ['#{'hello world'}' -> '#{result}']"
23
+ end
24
+
25
+ should "translate some english to french" do
26
+ result = MachineTranslation::Translator.translate('en', 'fr', 'hello world')
27
+ ## puts "TRANSLATING some English to French ['#{'hello world'}' -> '#{result}']"
28
+ end
29
+
30
+ should "translate some english to simplified chinese" do
31
+ result = MachineTranslation::Translator.translate('en', 'zh-CN', 'hello world')
32
+ ## puts "TRANSLATING some English to French ['#{'hello world'}' -> '#{result}']"
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: machine_translation
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Eric Woodward
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-05 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: thoughtbot-shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rest-client
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 15
44
+ segments:
45
+ - 1
46
+ - 6
47
+ - 0
48
+ version: 1.6.0
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: json
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 15
60
+ segments:
61
+ - 1
62
+ - 4
63
+ - 4
64
+ version: 1.4.4
65
+ type: :runtime
66
+ version_requirements: *id003
67
+ description: gem for getting a machine generated translation from Google and hopefully other sources too (eventually)
68
+ email: me@ejw.com
69
+ executables: []
70
+
71
+ extensions: []
72
+
73
+ extra_rdoc_files:
74
+ - LICENSE
75
+ - README.rdoc
76
+ files:
77
+ - LICENSE
78
+ - README.rdoc
79
+ - Rakefile
80
+ - VERSION
81
+ - lib/machine_translation.rb
82
+ - lib/machine_translation/configuration.rb
83
+ - lib/machine_translation/google.rb
84
+ - lib/machine_translation/translator.rb
85
+ - test/helper.rb
86
+ - test/translation_test.rb
87
+ has_rdoc: true
88
+ homepage: http://github.com/ejw/machine_translation
89
+ licenses: []
90
+
91
+ post_install_message:
92
+ rdoc_options:
93
+ - --charset=UTF-8
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ hash: 3
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ requirements: []
115
+
116
+ rubyforge_project: machine_translation
117
+ rubygems_version: 1.3.7
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: gem for getting a machine generated translation
121
+ test_files:
122
+ - test/helper.rb
123
+ - test/translation_test.rb