sishen-rtranslate 1.0 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +3 -0
- data/Manifest.txt +2 -0
- data/Rakefile +3 -3
- data/bin/rtranslate +11 -2
- data/lib/rtranslate.rb +11 -7
- data/lib/rtranslate/detection.rb +70 -0
- data/lib/rtranslate/rtranslate.rb +9 -19
- data/rtranslate.gemspec +4 -3
- data/tests/ts_detection.rb +31 -0
- metadata +7 -2
data/History.txt
CHANGED
data/Manifest.txt
CHANGED
data/Rakefile
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'hoe'
|
3
3
|
|
4
|
-
Hoe.new('rtranslate', '1.
|
4
|
+
Hoe.new('rtranslate', '1.1') do |p|
|
5
5
|
p.rubyforge_name = 'rtranslate'
|
6
6
|
p.author = 'Ye Dingding'
|
7
7
|
p.email = 'yedingding@gmail.com'
|
8
|
-
p.url = 'http://
|
8
|
+
p.url = 'http://github.com/sishen/rtranslate'
|
9
9
|
p.summary = 'Google translate ruby client api'
|
10
10
|
p.description = 'A simple, unofficial, ruby client API for using Google Translate.'
|
11
|
-
p.extra_deps = [['
|
11
|
+
p.extra_deps = [['json', '>= 1.1.3']]
|
12
12
|
end
|
data/bin/rtranslate
CHANGED
@@ -10,6 +10,7 @@ opts = OptionParser.new do |opts|
|
|
10
10
|
opts.banner = <<-EOF
|
11
11
|
Usage:
|
12
12
|
rtranslate -f lang -t lang text
|
13
|
+
rtranslate -d text
|
13
14
|
EOF
|
14
15
|
|
15
16
|
opts.on("-fLANG", "--from LANG", "From Language LANG") do |x|
|
@@ -20,6 +21,10 @@ opts = OptionParser.new do |opts|
|
|
20
21
|
options[:to] = x
|
21
22
|
end
|
22
23
|
|
24
|
+
opts.on("-d", "--detect", "Detect the text") do
|
25
|
+
options[:detect] = true
|
26
|
+
end
|
27
|
+
|
23
28
|
opts.on("-h", "--help", "Show this message") do
|
24
29
|
puts opts
|
25
30
|
exit
|
@@ -29,9 +34,13 @@ end
|
|
29
34
|
opts.parse!(ARGV)
|
30
35
|
text = ARGV.shift
|
31
36
|
|
32
|
-
|
37
|
+
unless (options[:detect] || (options[:from] && options[:to]) ) && text
|
33
38
|
puts opts
|
34
39
|
exit
|
35
40
|
end
|
36
41
|
|
37
|
-
|
42
|
+
if options[:detect]
|
43
|
+
STDOUT.puts(Translate.d(text))
|
44
|
+
else
|
45
|
+
STDOUT.puts(Translate.t(text, options[:from], options[:to]))
|
46
|
+
end
|
data/lib/rtranslate.rb
CHANGED
@@ -1,23 +1,27 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'rtranslate/language')
|
2
2
|
require File.join(File.dirname(__FILE__), 'rtranslate/rtranslate')
|
3
|
+
require File.join(File.dirname(__FILE__), 'rtranslate/detection')
|
3
4
|
require 'uri'
|
4
5
|
require 'open-uri'
|
5
6
|
|
6
7
|
begin
|
7
|
-
require '
|
8
|
+
require 'json'
|
8
9
|
rescue LoadError
|
9
10
|
require 'rubygems'
|
10
|
-
require '
|
11
|
+
require 'json'
|
11
12
|
end
|
12
13
|
|
13
14
|
$KCODE = 'u'
|
14
15
|
|
15
16
|
include Translate
|
16
17
|
def Translate.t(text, from, to)
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
"Error: " + $!
|
21
|
-
end
|
18
|
+
RTranslate.translate(text, from, to)
|
19
|
+
rescue
|
20
|
+
"Error: " + $!
|
22
21
|
end
|
23
22
|
|
23
|
+
def Translate.d(text)
|
24
|
+
Detection.detect(text)
|
25
|
+
rescue
|
26
|
+
"Error: " + $!
|
27
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# The program is a simple, unofficial, ruby client API
|
2
|
+
# for using Google Translate.
|
3
|
+
#
|
4
|
+
# Author:: Dingding Ye (mailto:yedingding@gmail.com)
|
5
|
+
# Copyright:: Copyright (c) 2007 Dingding Ye
|
6
|
+
# License:: Distributes under MIT License
|
7
|
+
|
8
|
+
module Translate
|
9
|
+
class DetectionResponse
|
10
|
+
attr_reader :abbrev
|
11
|
+
attr_reader :language
|
12
|
+
attr_reader :reliability
|
13
|
+
attr_reader :confidence
|
14
|
+
|
15
|
+
def initialize(json)
|
16
|
+
@abbrev = json["language"]
|
17
|
+
@language = Google::Language::Languages[@abbrev]
|
18
|
+
@reliability = json["isReliable"]
|
19
|
+
@confidence = json["confidence"]
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_s
|
23
|
+
abbrev
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Detection
|
28
|
+
# Google AJAX Language REST Service URL
|
29
|
+
GOOGLE_DETECTION_URL = "http://ajax.googleapis.com/ajax/services/language/detect"
|
30
|
+
|
31
|
+
# Default version of Google AJAX Language API
|
32
|
+
DEFAULT_VERSION = "1.0"
|
33
|
+
|
34
|
+
attr_reader :version, :key
|
35
|
+
|
36
|
+
class << self
|
37
|
+
def detect(text)
|
38
|
+
Detection.new.detect(text)
|
39
|
+
end
|
40
|
+
alias_method :d, :detect
|
41
|
+
end
|
42
|
+
|
43
|
+
def initialize(version = DEFAULT_VERSION, key = nil)
|
44
|
+
@version = version
|
45
|
+
@key = key
|
46
|
+
end
|
47
|
+
|
48
|
+
def detect(text, details = false)
|
49
|
+
url = "#{GOOGLE_DETECTION_URL}?q=#{text}&v=#{@version}"
|
50
|
+
if @key
|
51
|
+
url << "&key=#{@key}"
|
52
|
+
end
|
53
|
+
detection_response = do_detect(url)
|
54
|
+
details ? detection_response : detection_response.abbrev
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
def do_detect(url)
|
59
|
+
jsondoc = open(URI.escape(url)).read
|
60
|
+
response = JSON.parse(jsondoc)
|
61
|
+
if response["responseStatus"] == 200
|
62
|
+
DetectionResponse.new(response["responseData"])
|
63
|
+
else
|
64
|
+
raise StandardError, response["responseDetails"]
|
65
|
+
end
|
66
|
+
rescue Exception => e
|
67
|
+
raise StandardError, e.message
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -5,13 +5,6 @@
|
|
5
5
|
# Copyright:: Copyright (c) 2007 Dingding Ye
|
6
6
|
# License:: Distributes under MIT License
|
7
7
|
|
8
|
-
begin
|
9
|
-
require 'json'
|
10
|
-
rescue LoadError
|
11
|
-
require 'rubygems'
|
12
|
-
require 'json'
|
13
|
-
end
|
14
|
-
|
15
8
|
module Translate
|
16
9
|
class UnsupportedLanguagePair < StandardError
|
17
10
|
end
|
@@ -30,6 +23,7 @@ module Translate
|
|
30
23
|
def translate(text, from, to)
|
31
24
|
RTranslate.new.translate(text, { :from => from, :to => to })
|
32
25
|
end
|
26
|
+
alias_method :t, :translate
|
33
27
|
|
34
28
|
def translate_strings(text_array, from, to)
|
35
29
|
RTranslate.new.translate_strings(text_array, {:from => from, :to => to})
|
@@ -42,8 +36,6 @@ module Translate
|
|
42
36
|
def batch_translate(translate_options)
|
43
37
|
RTranslate.new.batch_translate(translate_options)
|
44
38
|
end
|
45
|
-
|
46
|
-
alias_method :t, :translate
|
47
39
|
end
|
48
40
|
|
49
41
|
def initialize(version = DEFAULT_VERSION, key = nil, default_from = nil, default_to = nil)
|
@@ -121,17 +113,15 @@ module Translate
|
|
121
113
|
|
122
114
|
private
|
123
115
|
def do_translate(url) #:nodoc:
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
raise StandardError, response["responseDetails"]
|
131
|
-
end
|
132
|
-
rescue Exception => e
|
133
|
-
raise StandardError, e.message
|
116
|
+
jsondoc = open(URI.escape(url)).read
|
117
|
+
response = JSON.parse(jsondoc)
|
118
|
+
if response["responseStatus"] == 200
|
119
|
+
response["responseData"]["translatedText"]
|
120
|
+
else
|
121
|
+
raise StandardError, response["responseDetails"]
|
134
122
|
end
|
123
|
+
rescue Exception => e
|
124
|
+
raise StandardError, e.message
|
135
125
|
end
|
136
126
|
end
|
137
127
|
end
|
data/rtranslate.gemspec
CHANGED
@@ -1,16 +1,17 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = %q{rtranslate}
|
3
|
-
s.version = "1.
|
3
|
+
s.version = "1.1"
|
4
4
|
|
5
5
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
6
6
|
s.authors = ["Ye Dingding"]
|
7
|
-
s.date = %q{
|
7
|
+
s.date = %q{2009-07-11}
|
8
8
|
s.description = %q{rtranslate is a simple, unofficial, ruby client API for using Google Translate.}
|
9
9
|
s.email = %q{yedingding@gmail.com}
|
10
10
|
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
|
11
11
|
s.files = ["History.txt", "MIT-LICENSE", "Manifest.txt", "README.txt", "Rakefile", "rtranslate.gemspec", "bin/rtranslate",
|
12
12
|
"lib/rtranslate.rb", "lib/rtranslate/language.rb", "lib/rtranslate/rtranslate.rb", "tests/ts_translate.rb",
|
13
|
-
"bin/rtranslate", "lib/rtranslate.rb", "lib/rtranslate/language.rb",
|
13
|
+
"bin/rtranslate", "lib/rtranslate.rb", "lib/rtranslate/language.rb",
|
14
|
+
"lib/rtranslate/rtranslate.rb", "lib/rtranslate/detection.rb", "tests/ts_detection.rb"]
|
14
15
|
s.has_rdoc = true
|
15
16
|
s.homepage = %q{http://sishen.lifegoo.com}
|
16
17
|
s.rdoc_options = ["--main", "README.txt"]
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
$:.unshift File.expand_path(File.dirname(__FILE__) + "/../lib")
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require 'rtranslate'
|
6
|
+
|
7
|
+
class Translate::DetectionTest < Test::Unit::TestCase
|
8
|
+
include Google::Language
|
9
|
+
def test_detection
|
10
|
+
d = Detection.new
|
11
|
+
assert_equal "es", d.detect("para poner este importante proyecto en práctica")
|
12
|
+
assert_equal "en", d.detect("this is a test of the Emergency text categorizing system.")
|
13
|
+
assert_equal "fr", d.detect("serait désigné peu après PDG d'Antenne 2 et de FR 3. Pas même lui ! Le")
|
14
|
+
assert_equal "it", d.detect("studio dell'uomo interiore? La scienza del cuore umano, che")
|
15
|
+
assert_equal "ro", d.detect("taiate pe din doua, in care vezi stralucind brun sau violet cristalele interioare")
|
16
|
+
assert_equal "pl", d.detect("na porozumieniu, na ³±czeniu si³ i ¶rodków. Dlatego szukam ludzi, którzy")
|
17
|
+
assert_equal "de", d.detect("sagt Hühsam das war bei Über eine Annonce in einem Frankfurter der Töpfer ein. Anhand von gefundenen gut kennt, hatte ihm die wahren Tats
|
18
|
+
achen Sechzehn Adorno-Schüler erinnern und daß ein Weiterdenken der Theorie für ihre Festlegung sind drei Jahre Erschütterung Einblick in die Abhängigkeit der Bauarbeiten
|
19
|
+
sei")
|
20
|
+
assert_equal "fi", d.detect("koulun arkistoihin pölyttymään, vaan nuoret saavat itse vaikuttaa ajatustensa eteenpäinviemiseen esimerkiksi")
|
21
|
+
assert_equal "sv", d.detect("enligt all sannolikhet för att få ro oavsiktligt intagit en för")
|
22
|
+
assert_equal "hu", d.detect("esôzéseket egy kissé túlméretezte, ebbôl kifolyólag a Földet egy hatalmas árvíz mosta el")
|
23
|
+
assert_equal "fi", d.detect("koulun arkistoihin pölyttymään, vaan nuoret saavat itse vaikuttaa ajatustensa eteenpäinviemiseen esimerkiksi")
|
24
|
+
assert_equal "nl", d.detect("tegen de kabinetsplannen. Een speciaal in het leven geroepen Landelijk")
|
25
|
+
assert_equal "da", d.detect("viksomhed, 58 pct. har et arbejde eller er under uddannelse, 76 pct. forsørges ikke længere af Kolding")
|
26
|
+
assert_equal "cs", d.detect("datují rokem 1862. Naprosto zakázán byl v pocitech smutku, beznadìje èi jiné")
|
27
|
+
assert_equal "no", d.detect("hånd på den enda hvitere restaurant-duken med en bevegelse så forfinet")
|
28
|
+
assert_equal "pt", d.detect("popular. Segundo o seu biógrafo, a Maria Adelaide auxiliava muita gente")
|
29
|
+
assert_equal "en", d.detect("TaffyDB finders looking nice so far!")
|
30
|
+
end
|
31
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sishen-rtranslate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "1.
|
4
|
+
version: "1.1"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ye Dingding
|
@@ -9,11 +9,12 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-07-11 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: hoe
|
17
|
+
type: :runtime
|
17
18
|
version_requirement:
|
18
19
|
version_requirements: !ruby/object:Gem::Requirement
|
19
20
|
requirements:
|
@@ -23,6 +24,7 @@ dependencies:
|
|
23
24
|
version:
|
24
25
|
- !ruby/object:Gem::Dependency
|
25
26
|
name: rspec
|
27
|
+
type: :runtime
|
26
28
|
version_requirement:
|
27
29
|
version_requirements: !ruby/object:Gem::Requirement
|
28
30
|
requirements:
|
@@ -32,6 +34,7 @@ dependencies:
|
|
32
34
|
version:
|
33
35
|
- !ruby/object:Gem::Dependency
|
34
36
|
name: json
|
37
|
+
type: :runtime
|
35
38
|
version_requirement:
|
36
39
|
version_requirements: !ruby/object:Gem::Requirement
|
37
40
|
requirements:
|
@@ -61,6 +64,8 @@ files:
|
|
61
64
|
- lib/rtranslate/language.rb
|
62
65
|
- lib/rtranslate/rtranslate.rb
|
63
66
|
- tests/ts_translate.rb
|
67
|
+
- lib/rtranslate/detection.rb
|
68
|
+
- tests/ts_detection.rb
|
64
69
|
has_rdoc: true
|
65
70
|
homepage: http://sishen.lifegoo.com
|
66
71
|
post_install_message:
|