crutch-google_translate 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION.yml +4 -0
- data/lib/google_translate.rb +26 -0
- data/lib/google_translate/api_call.rb +31 -0
- data/lib/google_translate/exceptions.rb +22 -0
- data/lib/google_translate/language_detect.rb +31 -0
- data/lib/google_translate/languages.rb +45 -0
- data/lib/google_translate/parsed_response.rb +12 -0
- data/lib/google_translate/translator.rb +49 -0
- data/spec/detect_spec.rb +19 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/translate_spec.rb +43 -0
- metadata +66 -0
data/VERSION.yml
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#require 'open-uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'cgi'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
# google translate API gem
|
8
|
+
# This is based on the documentation found here
|
9
|
+
# http://code.google.com/apis/ajaxlanguage/documentation/#Versioning
|
10
|
+
# look for the paragraph 'Flash and other Non-Javascript Environments'
|
11
|
+
#
|
12
|
+
# look at the Translator and LanguageDetect class (or README file) for instructions
|
13
|
+
module GoogleTranslate
|
14
|
+
HOSTNAME = "ajax.googleapis.com"
|
15
|
+
PATH ="/ajax/services/language/"
|
16
|
+
VERSION = "1.0"
|
17
|
+
#USERAGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1'
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'google_translate/exceptions'
|
22
|
+
require 'google_translate/languages'
|
23
|
+
require 'google_translate/parsed_response'
|
24
|
+
require 'google_translate/api_call'
|
25
|
+
require 'google_translate/translator'
|
26
|
+
require 'google_translate/language_detect'
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module GoogleTranslate
|
2
|
+
module ApiCall
|
3
|
+
def google_api_call(service,params,response_class,get=false)
|
4
|
+
data = []
|
5
|
+
params.each_pair {|key, value| data << "#{key}=#{value}" }
|
6
|
+
data_string = data.join('&')
|
7
|
+
headers = {
|
8
|
+
'Referer' => 'http://www.here-goes-a-referreri',
|
9
|
+
'Content-Type' => 'application/x-www-form-urlencoded',
|
10
|
+
}
|
11
|
+
|
12
|
+
if(get)
|
13
|
+
require 'uri'
|
14
|
+
response = Net::HTTP.get(URI.parse("http://#{HOSTNAME}#{PATH}#{service}?#{data_string}"))
|
15
|
+
else
|
16
|
+
response = Net::HTTP.new(HOSTNAME).post(PATH + service,data_string,headers)
|
17
|
+
end
|
18
|
+
|
19
|
+
raise GoogleUnavailable if response.nil? || response == ""
|
20
|
+
raise GoogleException if !get && response.code.to_i != 200
|
21
|
+
|
22
|
+
if(get)
|
23
|
+
parsed_response = response_class.new(response)
|
24
|
+
else
|
25
|
+
parsed_response = response_class.new(response.body)
|
26
|
+
end
|
27
|
+
|
28
|
+
parsed_response # return response class
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module GoogleTranslate
|
2
|
+
|
3
|
+
# Invalid language (or more specifically not one in existing list)
|
4
|
+
class InvalidLanguage < Exception
|
5
|
+
end
|
6
|
+
|
7
|
+
# no response returned to request
|
8
|
+
class GoogleUnavailable < Exception
|
9
|
+
end
|
10
|
+
|
11
|
+
# the status of the response indicates an error
|
12
|
+
class GoogleException < Exception
|
13
|
+
end
|
14
|
+
|
15
|
+
# the detection of the language is deemed unreliable by google.
|
16
|
+
class UnreliableDetection < Exception
|
17
|
+
end
|
18
|
+
|
19
|
+
# if no string is furnished for translation or detection
|
20
|
+
class NoGivenString < Exception
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module GoogleTranslate
|
2
|
+
# manage language detection based on a given string of text
|
3
|
+
# <b>how to use</b>:
|
4
|
+
# language = LanguageDetect.detect("il fait beau aujourd'hui") # returns "fr"
|
5
|
+
class LanguageDetect
|
6
|
+
extend ApiCall
|
7
|
+
|
8
|
+
SERVICE = "detect"
|
9
|
+
PARAMS = {"v" => "#{VERSION}"}
|
10
|
+
|
11
|
+
# detect the language of a given text.
|
12
|
+
def self.detect(text)
|
13
|
+
PARAMS["q"] = CGI.escape(text)
|
14
|
+
#the "true" parameter indicates that HTTP GET should be used (POST is only for translation)
|
15
|
+
response = google_api_call(SERVICE,PARAMS,DetectResponse,true)
|
16
|
+
raise UnreliableDetection if !response.is_reliable
|
17
|
+
response.language # return value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# to handle the detection response
|
22
|
+
class DetectResponse < ParsedResponse
|
23
|
+
attr_reader :language, :is_reliable, :confidence
|
24
|
+
def initialize(string)
|
25
|
+
super(string)
|
26
|
+
@language = @response_data['language']
|
27
|
+
@is_reliable = @response_data['isReliable']
|
28
|
+
@confidence = @response_data['confidence']
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module GoogleTranslate
|
2
|
+
|
3
|
+
ARABIC = "ar"
|
4
|
+
BULGARIAN = "bg"
|
5
|
+
CATALAN = "ca"
|
6
|
+
CHINESE = "zh"
|
7
|
+
CHINESE_SIMPLIFIED = "zh-CN"
|
8
|
+
CHINESE_TRADITIONAL = "zh-TW"
|
9
|
+
CROATIAN = "cr"
|
10
|
+
CZECH = "cs"
|
11
|
+
DANISH = "da"
|
12
|
+
DUTCH = "nl"
|
13
|
+
ENGLISH = "en"
|
14
|
+
FILIPINO = "tl"
|
15
|
+
FINNISH = "fi"
|
16
|
+
FRENCH = "fr"
|
17
|
+
GERMAN = "de"
|
18
|
+
GREEK = "el"
|
19
|
+
HEBREW = "iw"
|
20
|
+
HINDI = "hi"
|
21
|
+
INDONESIAN = "id"
|
22
|
+
ITALIAN = "it"
|
23
|
+
JAPANESE = "ja"
|
24
|
+
KOREAN = "ko"
|
25
|
+
LATVIAN = "lv"
|
26
|
+
LITHUANIAN = "lt"
|
27
|
+
NORWEGIAN = "no"
|
28
|
+
POLISH = "pl"
|
29
|
+
PORTUGESE = "pt"
|
30
|
+
ROMANIAN = "ro"
|
31
|
+
RUSSIAN = "ru"
|
32
|
+
SERBIAN = "sr"
|
33
|
+
SLOVAK = "sk"
|
34
|
+
SLOVENIAN = "sl"
|
35
|
+
SPANISH = "es"
|
36
|
+
SWEDISH = "sv"
|
37
|
+
UKRANIAN = "uk"
|
38
|
+
VIETNAMESE = "vi"
|
39
|
+
|
40
|
+
LANGUAGES = [ARABIC, BULGARIAN, CATALAN, CHINESE, CHINESE_SIMPLIFIED,CHINESE_TRADITIONAL,
|
41
|
+
CROATIAN, CZECH, DANISH, DUTCH, ENGLISH, FILIPINO, FRENCH, GERMAN, GREEK, HEBREW,
|
42
|
+
ITALIAN, JAPANESE, KOREAN, LATVIAN, LITHUANIAN, NORWEGIAN, POLISH, PORTUGESE,
|
43
|
+
ROMANIAN, RUSSIAN, SERBIAN, SLOVAK, SLOVENIAN, SPANISH, SWEDISH, UKRANIAN, VIETNAMESE]
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module GoogleTranslate
|
2
|
+
# superclass for parsing the response: contains the common elements
|
3
|
+
class ParsedResponse
|
4
|
+
#attr_reader :status, :details
|
5
|
+
def initialize(string)
|
6
|
+
json = JSON.parse(string)
|
7
|
+
#@status = json['responseStatus']
|
8
|
+
#@details = json['responseDetails']
|
9
|
+
@response_data = json['responseData']
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module GoogleTranslate
|
2
|
+
# manage translation of a given string from a given language to another language
|
3
|
+
# <b>How to use</b>:
|
4
|
+
# translator = Translator.new("en","fr") # so several translations can be performed with this new object
|
5
|
+
# result = translator.translate("nice day today") # returns "belle journÃe aujourd'hui"
|
6
|
+
class Translator
|
7
|
+
include ApiCall
|
8
|
+
|
9
|
+
SERVICE = "translate"
|
10
|
+
PARAMS = {"v" => "#{VERSION}"}
|
11
|
+
|
12
|
+
# initialize the translator with the language to translate from (from) and the language to translate to (to)
|
13
|
+
def initialize(from,to)
|
14
|
+
raise InvalidLanguage if !is_language?(from)
|
15
|
+
raise InvalidLanguage if !is_language?(to)
|
16
|
+
@from = from
|
17
|
+
@to = to
|
18
|
+
end
|
19
|
+
|
20
|
+
# translate a string in the given languages
|
21
|
+
# options so far:
|
22
|
+
# - html: if html encoding is desirable (for immediate display on a web page for instance)
|
23
|
+
# then this option needs to have a true value (:html => true)
|
24
|
+
def translate(text,options = {})
|
25
|
+
PARAMS["langpair"] = "#{@from}%7C#{@to}"
|
26
|
+
PARAMS["q"] = CGI.escape(text)
|
27
|
+
response = google_api_call(SERVICE, PARAMS, TranslationResponse)
|
28
|
+
translation = options[:html] ? response.translation : CGI.unescapeHTML(response.translation)
|
29
|
+
translation # return value
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
def is_language?(lang)
|
34
|
+
return true if lang == ""
|
35
|
+
LANGUAGES.include?(lang)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# to handle the translation response
|
40
|
+
class TranslationResponse < ParsedResponse
|
41
|
+
attr_reader :translation
|
42
|
+
def initialize(string)
|
43
|
+
super(string)
|
44
|
+
# here we unescape html escaped characters BUT if it's required to keep html format,
|
45
|
+
# an extra option should be added
|
46
|
+
@translation = @response_data['translatedText'] if @response_data
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/spec/detect_spec.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe GoogleTranslate, 'detect' do
|
4
|
+
it "should detect the language of a string" do
|
5
|
+
LanguageDetect.detect("il fait beau aujourd'hui").should be == "fr"
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should raise an error if no string" do
|
9
|
+
lambda {
|
10
|
+
LanguageDetect.detect(nil)
|
11
|
+
}.should raise_error(NoGivenString)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should raise an error if language not recognized" do
|
15
|
+
lambda {
|
16
|
+
LanguageDetect.detect("bliiiioaarg oqsdkqsdf")
|
17
|
+
}.should raise_error(UnreliableDetection)
|
18
|
+
end
|
19
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'rspec', '~> 1.1.3'
|
3
|
+
require 'spec'
|
4
|
+
|
5
|
+
# add lib directory
|
6
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
7
|
+
|
8
|
+
Spec::Runner.configure do |config|
|
9
|
+
config.mock_with :mocha
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'google_translate'
|
13
|
+
include GoogleTranslate
|
14
|
+
|
15
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe GoogleTranslate, 'translate' do
|
4
|
+
it "should raise an error if the from language is missing or doesn't exist" do
|
5
|
+
lambda {
|
6
|
+
Translator.new(nil,'en')
|
7
|
+
}.should raise_error(InvalidLanguage)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should raise an error if the to language is missing or doen't exist" do
|
11
|
+
lambda {
|
12
|
+
Translator.new('en','wk') # wookie speak
|
13
|
+
}.should raise_error(InvalidLanguage)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should translate a string" do
|
17
|
+
translator = Translator.new('en','fr')
|
18
|
+
result = translator.translate("nice day today")
|
19
|
+
result.should be == "belle journée aujourd'hui"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should raise an error if no response" do
|
23
|
+
translator = Translator.new('en','fr')
|
24
|
+
translator.stubs(:open).yields(StringIO.new) # stub empty response
|
25
|
+
lambda {
|
26
|
+
result = translator.translate("nice day today")
|
27
|
+
}.should raise_error(GoogleUnavailable)
|
28
|
+
end
|
29
|
+
|
30
|
+
# think how to handle this ... should it give a signal if the
|
31
|
+
# translation == the input ?
|
32
|
+
it "should not translate if untranslateable string" do
|
33
|
+
translator = Translator.new('en','fr')
|
34
|
+
result = translator.translate("blaargh")
|
35
|
+
result.should be == "blaargh"
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should leave html encoding if required" do
|
39
|
+
translator = Translator.new('en','fr')
|
40
|
+
result = translator.translate("nice day today", :html => true)
|
41
|
+
result.should be == "belle journée aujourd'hui"
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: crutch-google_translate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Elise Huard
|
8
|
+
- Michal Barla
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-08-26 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Google Translate Ruby API - based on google documentation
|
18
|
+
email: michal.barla@gmail.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- VERSION.yml
|
27
|
+
- lib/google_translate
|
28
|
+
- lib/google_translate/api_call.rb
|
29
|
+
- lib/google_translate/exceptions.rb
|
30
|
+
- lib/google_translate/language_detect.rb
|
31
|
+
- lib/google_translate/languages.rb
|
32
|
+
- lib/google_translate/parsed_response.rb
|
33
|
+
- lib/google_translate/translator.rb
|
34
|
+
- lib/google_translate.rb
|
35
|
+
- spec/detect_spec.rb
|
36
|
+
- spec/spec_helper.rb
|
37
|
+
- spec/translate_spec.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/crutch/google_translate
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options:
|
42
|
+
- --inline-source
|
43
|
+
- --charset=UTF-8
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.2.0
|
62
|
+
signing_key:
|
63
|
+
specification_version: 2
|
64
|
+
summary: Google Translate Ruby API
|
65
|
+
test_files: []
|
66
|
+
|