babylon 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +2 -0
- data/History.txt +6 -0
- data/Manifest.txt +7 -0
- data/README.txt +47 -0
- data/Rakefile +19 -0
- data/lib/babylon.rb +63 -0
- data/spec/babylon_spec.rb +45 -0
- data/spec/spec_helper.rb +9 -0
- metadata +102 -0
- metadata.gz.sig +1 -0
data.tar.gz.sig
ADDED
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
= Babylon
|
2
|
+
|
3
|
+
== DESCRIPTION:
|
4
|
+
|
5
|
+
Babylon allows you to easily determine the language of a text using the Google
|
6
|
+
AJAX Language API (http://code.google.com/apis/ajaxlanguage/)
|
7
|
+
|
8
|
+
== SYNOPSIS:
|
9
|
+
|
10
|
+
require 'rubygems'
|
11
|
+
require 'babylon'
|
12
|
+
|
13
|
+
Babylon.language_of('¿Dónde está el baño?', :ensure_reliability => true)
|
14
|
+
#=> :spanish
|
15
|
+
|
16
|
+
== REQUIREMENTS:
|
17
|
+
|
18
|
+
* HTTParty gem by John Nunemaker (http://railstips.org/2008/7/29/it-s-an-httparty-and-everyone-is-invited)
|
19
|
+
|
20
|
+
== INSTALL:
|
21
|
+
|
22
|
+
* sudo gem install babylon
|
23
|
+
|
24
|
+
== LICENSE:
|
25
|
+
|
26
|
+
(The MIT License)
|
27
|
+
|
28
|
+
Copyright (c) 2008 FIX
|
29
|
+
|
30
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
31
|
+
a copy of this software and associated documentation files (the
|
32
|
+
'Software'), to deal in the Software without restriction, including
|
33
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
34
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
35
|
+
permit persons to whom the Software is furnished to do so, subject to
|
36
|
+
the following conditions:
|
37
|
+
|
38
|
+
The above copyright notice and this permission notice shall be
|
39
|
+
included in all copies or substantial portions of the Software.
|
40
|
+
|
41
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
42
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
43
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
44
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
45
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
46
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
47
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require 'httparty'
|
6
|
+
require './lib/babylon.rb'
|
7
|
+
|
8
|
+
Hoe.new('babylon', Babylon::VERSION) do |p|
|
9
|
+
p.rubyforge_name = "kickassrb"
|
10
|
+
p.name = "babylon"
|
11
|
+
p.author = "Thorben Schröder"
|
12
|
+
p.description = "Free language determination from content using Google API."
|
13
|
+
p.email = 'thorben@fetmab.net'
|
14
|
+
p.summary = "Free language determination from content using Google API."
|
15
|
+
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
16
|
+
p.extra_deps << ['httparty'," >=0.1.3"]
|
17
|
+
end
|
18
|
+
|
19
|
+
# vim: syntax=Ruby
|
data/lib/babylon.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
module Babylon
|
2
|
+
require 'rubygems'
|
3
|
+
require 'httparty'
|
4
|
+
include HTTParty
|
5
|
+
|
6
|
+
class Error < ArgumentError; end;
|
7
|
+
|
8
|
+
class UncertaintyError < StandardError; end;
|
9
|
+
|
10
|
+
VERSION = '0.1.0'
|
11
|
+
|
12
|
+
SERVICE_URL = 'http://www.google.com/uds/GlangDetect'
|
13
|
+
format :json
|
14
|
+
|
15
|
+
#
|
16
|
+
# Determines the language of a string
|
17
|
+
#
|
18
|
+
# options: An options hash which can take two options:
|
19
|
+
#
|
20
|
+
# :min_certainty: Google tells us how certain it is with it's decission of
|
21
|
+
# what language the content is. If you want to avoid wild
|
22
|
+
# guesses you can set the min_certainty to a level and if
|
23
|
+
# the resulting certainty is below this threshold an
|
24
|
+
# Babylon::UncertaintyError is raised.
|
25
|
+
# Tip: For shorter strings 0.2 seems to mean a good result.
|
26
|
+
# For medium long strings 0.6 is ok and for any text more
|
27
|
+
# than 150 words or so google tends to be really confident
|
28
|
+
# about what it does which leads to a certainty around 0.9
|
29
|
+
#
|
30
|
+
# :ensure_reliability: In addition to the raw certainty value Google
|
31
|
+
# delivers a boolean value in the result set to let us
|
32
|
+
# know if it thinks the result is reliable or not.
|
33
|
+
# If you set this option to true an
|
34
|
+
# Babylon::UncertaintyError is raised unless Google
|
35
|
+
# think the result is reliable.
|
36
|
+
#
|
37
|
+
# Babylon.language_of('¿Dónde está el baño?') #=> :spanish
|
38
|
+
# Babylon.language_of('sgffdg sfdjkhsdfkh') #=> :english
|
39
|
+
# Babylon.language_of('sgffdg sfdjkhsdfkh', :min_certainty => 0.1) #=> raises Babylon::UncertaintyError
|
40
|
+
# Babylon.language_of('sgffdg sfdjkhsdfkh', :ensure_reliability => true) #=> raises Babylon::UncertaintyError
|
41
|
+
def self.language_of(content, options = {})
|
42
|
+
min_certainty = options[:min_certainty] || 0.0
|
43
|
+
ensure_reliability = options[:ensure_reliability]
|
44
|
+
|
45
|
+
result = self.raw_language_query(content)
|
46
|
+
|
47
|
+
certainty = result['responseData']['confidence']
|
48
|
+
reliable = result['responseData']['isReliable']
|
49
|
+
raise UncertaintyError if certainty < min_certainty || (ensure_reliability && !reliable)
|
50
|
+
|
51
|
+
result['responseData']['language'];
|
52
|
+
end
|
53
|
+
|
54
|
+
# Method to get the raw result Google sends us.
|
55
|
+
#
|
56
|
+
# Babylon.raw_language_query('¿Dónde está el baño?') => {"responseData"=>{"language"=>"es", "confidence"=>0.24530953, "isReliable"=>true}, "responseStatus"=>200, "responseDetails"=>nil
|
57
|
+
def self.raw_language_query(content)
|
58
|
+
result = get(SERVICE_URL, :query => {:v => 1.0, :q => content})
|
59
|
+
|
60
|
+
raise Error.new('Bad response from Google!') unless result['responseStatus'] == 200
|
61
|
+
result
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe Babylon do
|
4
|
+
it "should raise an Babylon::Error on erroneous Google response" do
|
5
|
+
Babylon.should_receive(:get).and_return({"responseStatus"=>400, "responseDetails"=>nil})
|
6
|
+
|
7
|
+
begin
|
8
|
+
Babylon.language_of("Text that causes an error")
|
9
|
+
rescue Babylon::Error
|
10
|
+
else
|
11
|
+
flunk 'Should raise an Babylon::Error'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should raise an Babylon::UncertaintyError if the text can not be clearly determined an reliability is demanded" do
|
16
|
+
Babylon.should_receive(:get).and_return({"responseData"=>{"language"=>"en", "confidence"=>0.1771214, "isReliable"=>false}, "responseStatus"=>200, "responseDetails"=>nil})
|
17
|
+
|
18
|
+
begin
|
19
|
+
Babylon.language_of("Undeterminable text", :ensure_reliability => true)
|
20
|
+
rescue Babylon::UncertaintyError
|
21
|
+
else
|
22
|
+
flunk 'Should raise an Babylon::UncertaintyError'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should raise no Babylon::UncertaintyError if the certainty equals the demanded one" do
|
27
|
+
Babylon.should_receive(:get).and_return({"responseData"=>{"language"=>"en", "confidence"=>0.20, "isReliable"=>true}, "responseStatus"=>200, "responseDetails"=>nil})
|
28
|
+
Babylon.language_of("Undeterminable text", :min_certainty => 0.20)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should raise no Babylon::UncertaintyError if the certainty is above the demanded one" do
|
32
|
+
Babylon.should_receive(:get).and_return({"responseData"=>{"language"=>"en", "confidence"=>0.201, "isReliable"=>true}, "responseStatus"=>200, "responseDetails"=>nil})
|
33
|
+
Babylon.language_of("Undeterminable text", :min_certainty => 0.20)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should raise Babylon::UncertaintyError if the certainty is below the demanded one" do
|
37
|
+
Babylon.should_receive(:get).and_return({"responseData"=>{"language"=>"en", "confidence"=>0.209, "isReliable"=>true}, "responseStatus"=>200, "responseDetails"=>nil})
|
38
|
+
begin
|
39
|
+
Babylon.language_of("Undeterminable text", :min_certainty => 0.21)
|
40
|
+
rescue Babylon::UncertaintyError
|
41
|
+
else
|
42
|
+
flunk 'Should raise an Babylon::UncertaintyError'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: babylon
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Thorben Schr\xC3\xB6der"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain:
|
11
|
+
- |
|
12
|
+
-----BEGIN CERTIFICATE-----
|
13
|
+
MIIDMjCCAhqgAwIBAgIBADANBgkqhkiG9w0BAQUFADA/MRAwDgYDVQQDDAd0aG9y
|
14
|
+
YmVuMRYwFAYKCZImiZPyLGQBGRYGZmV0bWFiMRMwEQYKCZImiZPyLGQBGRYDbmV0
|
15
|
+
MB4XDTA4MTAwOTIxNTQwNloXDTA5MTAwOTIxNTQwNlowPzEQMA4GA1UEAwwHdGhv
|
16
|
+
cmJlbjEWMBQGCgmSJomT8ixkARkWBmZldG1hYjETMBEGCgmSJomT8ixkARkWA25l
|
17
|
+
dDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAOTrBWJAhXdd4FALdaz4
|
18
|
+
+2KKe6Loz7L8AQxvhYedX7trYpqrWmXNLyCZKvNDf7Hp0EmOn8k5Iti161bcWxwY
|
19
|
+
fj8ejQ02U3OUyKSQM7V7zUrzB9pkmZ8ROGWJmw+nWVu7ZF7UU6+kWwaSMU/unPno
|
20
|
+
c1PcfOQrwCjvXbedMTFPZ1b/W37DoaoVQJLzzx95ewXSZ7iPtLxTrjHESjWBPxFi
|
21
|
+
JMEVCZDM+5UTEm41ucAJJ58z54mKryRap4NMux9YmPFp13f0xFVKP5kST16Q96IV
|
22
|
+
qJaPKd4WApsB8WOOyxGVFzp6Lf1fAHKjrXca6ywHeAM070Ki6GzAXKPBzUV13/R7
|
23
|
+
azECAwEAAaM5MDcwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0OBBYEFMCo
|
24
|
+
Q0fsO/qD4FD6zVoAIBw5ehlOMA0GCSqGSIb3DQEBBQUAA4IBAQCT+qucnHSHu9t0
|
25
|
+
Ntxpnm5gpQPVFz+kI6WCAqUeVlV5cbifH7/T+HKEePe+H3sF1eHG2X0QiXMYDZ26
|
26
|
+
Vgp6S9LCofXhJySOGYO26gcUyGfmkmQ//+YiwpJ0k+uznEM+RBNw/CSpFoXrnKa2
|
27
|
+
39/buzR3VtgPAcAOHb+5+WDIdX6NGgrKFF8udOqQ+rAvsoQXpJXfpfdqoFiOdfCa
|
28
|
+
Bqd6tQGVy0qUttoqMCOTxwMYWzoNs5GFXqtmbXxV6W2F81ipkELVVoWtSvRRkqtx
|
29
|
+
dX2CCcpgG+qXnji1CJyb6Dgm5ICJO/+B8ZKQ5qAYg798KOB7gyddzwRZWImtRoYU
|
30
|
+
kX4sVHCM
|
31
|
+
-----END CERTIFICATE-----
|
32
|
+
|
33
|
+
date: 2008-10-16 00:00:00 +02:00
|
34
|
+
default_executable:
|
35
|
+
dependencies:
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: httparty
|
38
|
+
type: :runtime
|
39
|
+
version_requirement:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 0.1.3
|
45
|
+
version:
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: hoe
|
48
|
+
type: :development
|
49
|
+
version_requirement:
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.8.0
|
55
|
+
version:
|
56
|
+
description: Free language determination from content using Google API.
|
57
|
+
email: thorben@fetmab.net
|
58
|
+
executables: []
|
59
|
+
|
60
|
+
extensions: []
|
61
|
+
|
62
|
+
extra_rdoc_files:
|
63
|
+
- History.txt
|
64
|
+
- Manifest.txt
|
65
|
+
- README.txt
|
66
|
+
files:
|
67
|
+
- History.txt
|
68
|
+
- Manifest.txt
|
69
|
+
- README.txt
|
70
|
+
- Rakefile
|
71
|
+
- lib/babylon.rb
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
- spec/babylon_spec.rb
|
74
|
+
has_rdoc: true
|
75
|
+
homepage:
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options:
|
78
|
+
- --main
|
79
|
+
- README.txt
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: "0"
|
87
|
+
version:
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: "0"
|
93
|
+
version:
|
94
|
+
requirements: []
|
95
|
+
|
96
|
+
rubyforge_project: kickassrb
|
97
|
+
rubygems_version: 1.2.0
|
98
|
+
signing_key:
|
99
|
+
specification_version: 2
|
100
|
+
summary: Free language determination from content using Google API.
|
101
|
+
test_files: []
|
102
|
+
|
metadata.gz.sig
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
�6�`h�%����3�Қ2I��\�]��T8:�?��|�a��h�e捞��<�b��O�6K�m��Z�z��C'gN"ӗ?`���� 5cd�|csE^+�a`zo��*.M��s�
|