detect_language 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - jruby-18mode # JRuby in 1.8 mode
6
+ - jruby-19mode # JRuby in 1.9 mode
7
+ - rbx-18mode
8
+ - rbx-19mode
9
+ script: "bundle exec rake spec"
10
+
data/Gemfile ADDED
@@ -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
data/LICENSE ADDED
@@ -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.
data/README.md ADDED
@@ -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
+ Before using Detect Language API client you setup your personal API key.
7
+ You can get it by signing up at http://detectlanguage.com
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'detect_language'
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install detect_language
18
+
19
+ ### Configuration
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.
data/Rakefile ADDED
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,67 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'json'
4
+
5
+ module DetectLanguage
6
+ class Client
7
+ attr_reader :configuration
8
+
9
+ def initialize(configuration)
10
+ @configuration = configuration
11
+ end
12
+
13
+ def execute(method, params)
14
+ http = setup_http_connection
15
+
16
+ request_params = params.merge(:key => configuration.api_key)
17
+
18
+ request = Net::HTTP::Post.new(request_uri(method))
19
+ request.set_form_data(request_params)
20
+ request.add_field('User-Agent', configuration.user_agent)
21
+
22
+ response = http.request(request)
23
+
24
+ case response
25
+ when Net::HTTPSuccess then
26
+ parse_response(response.body)
27
+ else
28
+ raise "Failure: #{response.class}"
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def parse_response(response_body)
35
+ response = JSON.parse(response_body)
36
+
37
+ if response["error"].nil?
38
+ response
39
+ else
40
+ raise Exception.new(response["error"]["message"])
41
+ end
42
+ end
43
+
44
+ def request_uri(method)
45
+ "/#{configuration.api_version}/#{method}"
46
+ end
47
+
48
+ def setup_http_connection
49
+ http =
50
+ Net::HTTP::Proxy(configuration.proxy_host, configuration.proxy_port, configuration.proxy_user,
51
+ configuration.proxy_pass).
52
+ new(configuration.host, configuration.port)
53
+
54
+ http.read_timeout = configuration.http_read_timeout
55
+ http.open_timeout = configuration.http_open_timeout
56
+
57
+ if configuration.secure?
58
+ http.use_ssl = true
59
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
60
+ else
61
+ http.use_ssl = false
62
+ end
63
+
64
+ http
65
+ end
66
+ end
67
+ end
@@ -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
@@ -0,0 +1,5 @@
1
+ module DetectLanguage
2
+ class Exception < ::Exception
3
+
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module DetectLanguage
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe DetectLanguage do
6
+
7
+ context "configuration" do
8
+ it "should have default configuration values" do
9
+ subject.configuration.api_key.should == 'demo'
10
+ subject.configuration.api_version.should == '0.2'
11
+ subject.configuration.host.should == 'ws.detectlanguage.com'
12
+ subject.configuration.user_agent.should == 'Detect Language API ruby gem'
13
+ end
14
+
15
+ it "should allow configuring" do
16
+ subject.configure do |config|
17
+ config.user_agent = "Detect Language testing"
18
+ end
19
+
20
+ subject.configuration.user_agent.should == "Detect Language testing"
21
+ end
22
+ end
23
+
24
+ context "detection" do
25
+ before do
26
+ # testing key
27
+ subject.configuration.api_key = "24c3185fef623b537a4df60df0a8d4d9"
28
+ end
29
+
30
+ it "should detect languages" do
31
+ result = subject.detect("Hello world")
32
+ result[0]['language'].should == "en"
33
+
34
+ result = subject.detect("Jau saulelÄ— vÄ—l atkopdama budino svietÄ…")
35
+ result[0]['language'].should == "lt"
36
+ end
37
+
38
+ it "should have simple way to detect a language" do
39
+ subject.simple_detect("Hello world").should == "en"
40
+ end
41
+ end
42
+
43
+ it "should raise exception for invalid key" do
44
+ old_api_key = subject.configuration.api_key
45
+
46
+ subject.configuration.api_key = "invalid"
47
+
48
+ lambda {
49
+ subject.detect("Hello world")
50
+ }.should raise_error(::DetectLanguage::Exception)
51
+
52
+ subject.configuration.api_key = old_api_key
53
+ end
54
+
55
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'detect_language'
4
+
5
+ RSpec.configure do |config|
6
+
7
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: detect_language
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Laurynas Butkus
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-11 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Language Detection API Client
15
+ email:
16
+ - laurynas.butkus@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rspec
23
+ - .travis.yml
24
+ - Gemfile
25
+ - LICENSE
26
+ - README.md
27
+ - Rakefile
28
+ - detect_language.gemspec
29
+ - lib/detect_language.rb
30
+ - lib/detect_language/client.rb
31
+ - lib/detect_language/configuration.rb
32
+ - lib/detect_language/exception.rb
33
+ - lib/detect_language/version.rb
34
+ - spec/detect_language_spec.rb
35
+ - spec/spec_helper.rb
36
+ homepage: https://github.com/detectlanguage/detect_language
37
+ licenses: []
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 1.8.24
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Detects language of given text. Returns detected language codes and scores.
60
+ test_files:
61
+ - spec/detect_language_spec.rb
62
+ - spec/spec_helper.rb
63
+ has_rdoc: