Skip to content

Commit 71931b3

Browse files
committedAug 11, 2012
Initial commit
0 parents  commit 71931b3

15 files changed

+392
-0
lines changed
 

‎.gitignore

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
.yardoc
6+
.idea
7+
Gemfile.lock
8+
InstalledFiles
9+
_yardoc
10+
coverage
11+
doc/
12+
lib/bundler/man
13+
pkg
14+
rdoc
15+
spec/reports
16+
test/tmp
17+
test/version_tmp
18+
tmp

‎.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--format progress

‎.travis.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
rvm:
2+
- 1.9.3
3+
- 1.9.2
4+
- jruby
5+
- rbx
6+
script: "bundle exec rake spec"
7+

‎Gemfile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec
4+
5+
gem 'rake'
6+
gem 'json'
7+
8+
group :test do
9+
gem "rspec"
10+
end

‎LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2012 Laurynas Butkus
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎README.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Detect Language API Client [![Build Status](https://secure.travis-ci.org/detectlanguage/detect_language.png)](http://travis-ci.org/detectlanguage/detect_language)
2+
========
3+
4+
Detects language of given text. Returns detected language codes and scores.
5+
6+
## Installation
7+
8+
Add this line to your application's Gemfile:
9+
10+
gem 'detect_language'
11+
12+
Or install it yourself as:
13+
14+
$ gem install detect_language
15+
16+
### Configuration
17+
18+
Before using Detect Language API client you setup your personal API key.
19+
You can get it by signing up at http://detectlanguage.com
20+
21+
If you are using Rails, create initializer `config/initializers/detect_language.rb` and add following code there.
22+
Otherwise just integrate following code into your apps configuration.
23+
24+
DetectLanguage.configure do |config|
25+
config.api_key = "YOUR API KEY"
26+
end
27+
28+
## Usage
29+
30+
### Language detection
31+
32+
DetectLanguage.detect("Buenos dias señor")
33+
34+
#### Result
35+
36+
[ {"language"=>"es", "isReliable"=>false, "confidence"=>0.3271028037383178},
37+
{"language"=>"pt", "isReliable"=>false, "confidence"=>0.08356545961002786} ]
38+
39+
### Simple language detection
40+
41+
If you need just a language code you can use `simple_detect`. It returns just the language code.
42+
43+
DetectLanguage.simple_detect("Buenos dias señor")
44+
45+
#### Result
46+
47+
"es"
48+
49+
## License
50+
51+
Detect Language API Client is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.

‎Rakefile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env rake
2+
require "bundler/gem_tasks"
3+
require 'rspec/core/rake_task'
4+
5+
RSpec::Core::RakeTask.new
6+
7+
task :default => :spec

‎detect_language.gemspec

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- encoding: utf-8 -*-
2+
require File.expand_path('../lib/detect_language/version', __FILE__)
3+
4+
Gem::Specification.new do |gem|
5+
gem.authors = ["Laurynas Butkus"]
6+
gem.email = ["laurynas.butkus@gmail.com"]
7+
gem.description = %q{Language Detection API Client}
8+
gem.summary = %q{Detects language of given text. Returns detected language codes and scores.}
9+
gem.homepage = "https://github.com/detectlanguage/detect_language"
10+
11+
gem.files = `git ls-files`.split($\)
12+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14+
gem.name = "detect_language"
15+
gem.require_paths = ["lib"]
16+
gem.version = DetectLanguage::VERSION
17+
end

‎lib/detect_language.rb

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require "detect_language/version"
2+
require "detect_language/exception"
3+
require "detect_language/configuration"
4+
require "detect_language/client"
5+
6+
module DetectLanguage
7+
class << self
8+
attr_writer :configuration
9+
10+
def configure
11+
yield(configuration)
12+
end
13+
14+
# The configuration object.
15+
# @see DetectLanguage.configure
16+
def configuration
17+
@configuration ||= Configuration.new
18+
end
19+
20+
def client
21+
@client ||= Client.new(configuration)
22+
end
23+
24+
def detect(text)
25+
result = client.execute(:detect, :q => text)
26+
result['data']['detections']
27+
end
28+
29+
def simple_detect(text)
30+
detections = detect(text)
31+
32+
if detections.empty?
33+
nil
34+
else
35+
detections[0]['language']
36+
end
37+
end
38+
end
39+
end

‎lib/detect_language/client.rb

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require 'net/http'
2+
require 'json'
3+
4+
module DetectLanguage
5+
class Client
6+
attr_reader :configuration
7+
8+
def initialize(configuration)
9+
@configuration = configuration
10+
end
11+
12+
def execute(method, params)
13+
http = setup_http_connection
14+
15+
request_params = params.merge(:key => configuration.api_key)
16+
17+
request = Net::HTTP::Post.new(request_uri(method))
18+
request.set_form_data(request_params)
19+
request.add_field('User-Agent', configuration.user_agent)
20+
21+
response = http.request(request)
22+
23+
case response
24+
when Net::HTTPSuccess then
25+
parse_response(response.body)
26+
else
27+
raise "Failure: #{response.class}"
28+
end
29+
end
30+
31+
private
32+
33+
def parse_response(response_body)
34+
response = JSON.parse(response_body)
35+
36+
if response["error"].nil?
37+
response
38+
else
39+
raise Exception.new(response["error"]["message"])
40+
end
41+
end
42+
43+
def request_uri(method)
44+
"/#{configuration.api_version}/#{method}"
45+
end
46+
47+
def setup_http_connection
48+
http =
49+
Net::HTTP::Proxy(configuration.proxy_host, configuration.proxy_port, configuration.proxy_user,
50+
configuration.proxy_pass).
51+
new(configuration.host, configuration.port)
52+
53+
http.read_timeout = configuration.http_read_timeout
54+
http.open_timeout = configuration.http_open_timeout
55+
56+
if configuration.secure?
57+
http.use_ssl = true
58+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
59+
else
60+
http.use_ssl = false
61+
end
62+
63+
http
64+
end
65+
end
66+
end

‎lib/detect_language/configuration.rb

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
module DetectLanguage
2+
class Configuration
3+
# The API key for your project, found on your homepage after you login into detectlanguage.com website
4+
# Defaults to 'demo', which has a limited number of requests.
5+
attr_accessor :api_key
6+
7+
# The API version you are using (defaults to 0.2).
8+
attr_accessor :api_version
9+
10+
# HTTP request user agent (defaults to 'Detect Language API ruby gem').
11+
attr_accessor :user_agent
12+
13+
# The host to connect to (defaults to ws.detectlanguage.com).
14+
attr_accessor :host
15+
16+
# The port on which your DetectLanguage server runs (defaults to 443 for secure
17+
# connections, 80 for insecure connections).
18+
attr_accessor :port
19+
20+
# +true+ for https connections, +false+ for http connections.
21+
attr_accessor :secure
22+
23+
# The HTTP open timeout in seconds.
24+
attr_accessor :http_open_timeout
25+
26+
# The HTTP read timeout in seconds.
27+
attr_accessor :http_read_timeout
28+
29+
# The hostname of your proxy server (if using a proxy).
30+
attr_accessor :proxy_host
31+
32+
# The port of your proxy server (if using a proxy).
33+
attr_accessor :proxy_port
34+
35+
# The username to use when logging into your proxy server (if using a proxy).
36+
attr_accessor :proxy_user
37+
38+
# The password to use when logging into your proxy server (if using a proxy).
39+
attr_accessor :proxy_pass
40+
41+
alias_method :secure?, :secure
42+
43+
def initialize
44+
@api_key = "demo"
45+
@api_version = "0.2"
46+
@host = "ws.detectlanguage.com"
47+
@user_agent = "Detect Language API ruby gem"
48+
end
49+
50+
def protocol
51+
if secure?
52+
'https'
53+
else
54+
'http'
55+
end
56+
end
57+
58+
def port
59+
@port || default_port
60+
end
61+
62+
# Allows config options to be read like a hash
63+
#
64+
# @param [Symbol] option Key for a given attribute
65+
def [](option)
66+
send(option)
67+
end
68+
69+
private
70+
71+
# Determines what port should we use for sending requests.
72+
# @return [Fixnum] Returns 443 if you've set secure to true in your
73+
# configuration, and 80 otherwise.
74+
def default_port
75+
if secure?
76+
443
77+
else
78+
80
79+
end
80+
end
81+
82+
end
83+
end

‎lib/detect_language/exception.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module DetectLanguage
2+
class Exception < ::Exception
3+
4+
end
5+
end

‎lib/detect_language/version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module DetectLanguage
2+
VERSION = "1.0.0"
3+
end

0 commit comments

Comments
 (0)
Please sign in to comment.