linguo 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +24 -0
- data/README.md +72 -0
- data/Rakefile +9 -0
- data/lib/linguo.rb +29 -0
- data/lib/linguo/config.rb +16 -0
- data/lib/linguo/core_ext/string.rb +17 -0
- data/lib/linguo/detect.rb +32 -0
- data/lib/linguo/errors.rb +18 -0
- data/lib/linguo/version.rb +3 -0
- data/lib/rails/generators/linguo/config_generator.rb +11 -0
- data/linguo.gemspec +23 -0
- data/spec/fixtures/response.json +1 -0
- data/spec/fixtures/wrong_api_key.json +1 -0
- data/spec/linguo/config_spec.rb +15 -0
- data/spec/linguo/core_ext_spec.rb +16 -0
- data/spec/linguo/detect_spec.rb +39 -0
- data/spec/linguo_spec.rb +6 -0
- data/spec/spec_helper.rb +7 -0
- metadata +126 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm: 1.9.3
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
Copyright (c) 2012 Nihad Abbasov / NARKOZ
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
1. Redistributions of source code must retain the above copyright notice,
|
8
|
+
this list of conditions and the following disclaimer.
|
9
|
+
|
10
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
12
|
+
and/or other materials provided with the distribution.
|
13
|
+
|
14
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
15
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
17
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
18
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
19
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
20
|
+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
21
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
22
|
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
23
|
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
24
|
+
POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# Linguo
|
2
|
+
|
3
|
+
Linguo detects the language of a given text using [Free Language Detection API](http://detectlanguage.com).
|
4
|
+
|
5
|
+
[![Build Status](https://secure.travis-ci.org/NARKOZ/linguo.png)](http://travis-ci.org/NARKOZ/linguo)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install it from rubygems:
|
10
|
+
|
11
|
+
```sh
|
12
|
+
gem install linguo
|
13
|
+
```
|
14
|
+
|
15
|
+
If you're using Rails, add to your Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'linguo'
|
19
|
+
# gem 'linguo', :git => 'git://github.com/NARKOZ/linguo.git'
|
20
|
+
```
|
21
|
+
|
22
|
+
and run:
|
23
|
+
|
24
|
+
```sh
|
25
|
+
bundle
|
26
|
+
```
|
27
|
+
|
28
|
+
You can run `rails g linguo:config` to generate an initializer and set up API key at `config/initializers/linguo.rb`.
|
29
|
+
|
30
|
+
## Usage examples
|
31
|
+
|
32
|
+
You can use demo API key (`demo`) to test out any examples below.
|
33
|
+
|
34
|
+
There are a few ways to set an API key. You can set default API key globally in `Linguo::Config.api_key`:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
Linguo.api_key = "your_api_key"
|
38
|
+
```
|
39
|
+
|
40
|
+
or set the environment variable `'LINGUO_API_KEY'` and Linguo will use it.
|
41
|
+
|
42
|
+
You can also pass it directly:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
Linguo.detect("your text to detect", "your_api_key")
|
46
|
+
```
|
47
|
+
|
48
|
+
Identify text language and get confidence scores using the `detect` method:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
Linguo.detect("こんにちは")
|
52
|
+
```
|
53
|
+
|
54
|
+
Get a list of languages with confidence greater than 0.3:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
Linguo.detect("こんにちは").detections.map {|x| x['language'] if x['confidence'] > 0.3}.compact
|
58
|
+
```
|
59
|
+
|
60
|
+
Linguo adds `lang` method to `String` class:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
"こんにちは".lang
|
64
|
+
```
|
65
|
+
|
66
|
+
It will return an array containing detected languages.
|
67
|
+
|
68
|
+
For more information, refer to [documentation](http://rubydoc.info/gems/linguo/frames).
|
69
|
+
|
70
|
+
## License
|
71
|
+
|
72
|
+
Released under the BSD 2-clause license. See LICENSE.txt for details.
|
data/Rakefile
ADDED
data/lib/linguo.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'linguo/version'
|
2
|
+
require 'linguo/config'
|
3
|
+
require 'linguo/detect'
|
4
|
+
require 'linguo/errors'
|
5
|
+
require 'linguo/core_ext/string'
|
6
|
+
require 'net/http'
|
7
|
+
require 'json'
|
8
|
+
|
9
|
+
module Linguo
|
10
|
+
# Sets API key obtained from detectlanguage.com.
|
11
|
+
#
|
12
|
+
# @see Linguo::Config
|
13
|
+
def self.api_key=(val)
|
14
|
+
Config.api_key = val
|
15
|
+
end
|
16
|
+
|
17
|
+
# Creates a new instance of Linguo::Detect.
|
18
|
+
#
|
19
|
+
# Note that api_key is optional and isn't required once you set
|
20
|
+
# <tt>Linguo::Config.api_key</tt> or <tt>ENV['LINGUO_API_KEY']</tt>.
|
21
|
+
#
|
22
|
+
# @param [String] text The text required to detect.
|
23
|
+
# @param [String] api_key The API key obtained from detectlanguage.com.
|
24
|
+
# @return [Linguo::Detect]
|
25
|
+
def self.detect(text, api_key=nil)
|
26
|
+
api_key = api_key || Linguo::Config.api_key || ENV['LINGUO_API_KEY']
|
27
|
+
Detect.new(text, api_key)
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Linguo
|
2
|
+
# Defines methods to set API key.
|
3
|
+
module Config
|
4
|
+
def self.api_key=(val)
|
5
|
+
@@api_key = val
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.api_key
|
9
|
+
begin
|
10
|
+
@@api_key
|
11
|
+
rescue NameError
|
12
|
+
raise Linguo::Errors::MissingApiKey, "No API key has been set!"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class String
|
2
|
+
# Retrieves all the languages detected for the current string
|
3
|
+
# sorted by confidence.
|
4
|
+
#
|
5
|
+
# Requires <tt>Linguo::Config.api_key</tt> or <tt>ENV['LINGUO_API_KEY']</tt>
|
6
|
+
# to be set up.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# "simple text".lang # => ["en", "fr"]
|
10
|
+
# @return [Array]
|
11
|
+
def lang
|
12
|
+
l ||= Linguo.detect(self, Linguo::Config.api_key)
|
13
|
+
l.detections.map {|x| x['language']}
|
14
|
+
rescue => e
|
15
|
+
raise Linguo::Errors::LinguoError, e.message
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Linguo
|
2
|
+
class Detect
|
3
|
+
# The URL to the API endpoint.
|
4
|
+
API_URL = 'http://ws.detectlanguage.com/0.2/detect'
|
5
|
+
|
6
|
+
# An array of detections.
|
7
|
+
attr_reader :detections
|
8
|
+
|
9
|
+
# Initializes a new Detect object and parses the API response.
|
10
|
+
#
|
11
|
+
# @param [String] text The text required to detect.
|
12
|
+
# @param [String] api_key The API key obtained from detectlanguage.com.
|
13
|
+
# @return [Linguo::Detect]
|
14
|
+
def initialize(text, api_key)
|
15
|
+
raise Errors::MissingApiKey, "No API key provided." unless api_key
|
16
|
+
|
17
|
+
uri = URI API_URL
|
18
|
+
req = Net::HTTP::Post.new(uri.path)
|
19
|
+
req.set_form_data('q' => text, 'key' => api_key)
|
20
|
+
res = Net::HTTP.start(uri.hostname, uri.port) {|http| http.request(req)}
|
21
|
+
|
22
|
+
if res.code.to_i == 200
|
23
|
+
data = JSON.parse(res.body)
|
24
|
+
raise Errors::ApiError, data['error']['message'] if data['error']
|
25
|
+
raise Errors::UnexpectedApiException if data['data'].nil?
|
26
|
+
@detections = data['data']['detections']
|
27
|
+
else
|
28
|
+
raise Errors::ApiConnectionError, "#{response.code} #{response.message}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Linguo
|
2
|
+
module Errors
|
3
|
+
# Custom error class for rescuing from all Linguo errors.
|
4
|
+
class LinguoError < StandardError; end
|
5
|
+
|
6
|
+
# Raised when Linguo API key is missed.
|
7
|
+
class MissingApiKey < LinguoError; end
|
8
|
+
|
9
|
+
# Raised when API endpoint returns error.
|
10
|
+
class ApiError < LinguoError; end
|
11
|
+
|
12
|
+
# Raised when API endpoint doesn't return the HTTP status code 200.
|
13
|
+
class ApiConnectionError < LinguoError; end
|
14
|
+
|
15
|
+
# Raised when API endpoint returns unexpected response.
|
16
|
+
class UnexpectedApiException < LinguoError; end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Linguo
|
2
|
+
class ConfigGenerator < Rails::Generators::Base
|
3
|
+
desc "Creates a configuration file at config/initializers/linguo.rb"
|
4
|
+
|
5
|
+
# Creates a configuration file at <tt>config/initializers/linguo.rb</tt>
|
6
|
+
# when running <tt>rails g linguo:config</tt>.
|
7
|
+
def create_linguo_file
|
8
|
+
create_file 'config/initializers/linguo.rb', "Linguo.api_key = ENV['LINGUO_API_KEY']"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/linguo.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/linguo/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Nihad Abbasov", "Laurynas Butkus"]
|
6
|
+
gem.email = ["mail@narkoz.me", "laurynas.butkus@gmail.com"]
|
7
|
+
gem.description = %q{Language detection library for Ruby}
|
8
|
+
gem.summary = %q{Linguo detects the language of a given piece of text}
|
9
|
+
gem.homepage = "https://github.com/narkoz/linguo"
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "linguo"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Linguo::VERSION
|
17
|
+
|
18
|
+
gem.add_runtime_dependency 'json'
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rake'
|
21
|
+
gem.add_development_dependency 'rspec'
|
22
|
+
gem.add_development_dependency 'webmock'
|
23
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
{"data":{"detections":[{"isReliable":false,"confidence":0.6,"language":"ja"},{"isReliable":false,"confidence":0.01,"language":"zh"}]}}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"error":{"code":1,"message":"Invalid API key"}}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Linguo::Config do
|
4
|
+
describe ".api_key" do
|
5
|
+
it "should raise an error when api_key is not set" do
|
6
|
+
expect { Linguo::Config.api_key }.
|
7
|
+
to raise_error Linguo::Errors::MissingApiKey
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should return an API key when api_key is set" do
|
11
|
+
Linguo::Config.api_key = 'valid_api_key'
|
12
|
+
Linguo::Config.api_key.should == 'valid_api_key'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe String do
|
5
|
+
describe "#lang" do
|
6
|
+
before do
|
7
|
+
stub_request(:post, "http://ws.detectlanguage.com/0.2/detect").
|
8
|
+
with(:body => {"q"=>"こんにちは", "key"=>"valid_api_key"}).
|
9
|
+
to_return(:body => load_fixture("response"))
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return an array of detected languages" do
|
13
|
+
'こんにちは'.lang.should == ["ja", "zh"]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Linguo::Detect do
|
5
|
+
describe "#detections" do
|
6
|
+
context "with valid API key passed" do
|
7
|
+
before do
|
8
|
+
stub_request(:post, "http://ws.detectlanguage.com/0.2/detect").
|
9
|
+
with(:body => {"q"=>"こんにちは", "key"=>"valid_api_key"}).
|
10
|
+
to_return(:body => load_fixture("response"))
|
11
|
+
@linguo = Linguo.detect('こんにちは', 'valid_api_key')
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should request the correct resource" do
|
15
|
+
a_request(:post, Linguo::Detect::API_URL).should have_been_made
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should return the list of detections" do
|
19
|
+
@linguo.detections.should be_an Array
|
20
|
+
@linguo.detections.first.should be_a Hash
|
21
|
+
@linguo.detections.first['language'].should == "ja"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "with invalid API key passed" do
|
26
|
+
before do
|
27
|
+
stub_request(:post, "http://ws.detectlanguage.com/0.2/detect").
|
28
|
+
with(:body => {"q"=>"こんにちは", "key"=>""}).
|
29
|
+
to_return(:body => load_fixture("wrong_api_key"))
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should raise an API error" do
|
33
|
+
expect { Linguo.detect('こんにちは', '') }.
|
34
|
+
to raise_error Linguo::Errors::ApiError, "Invalid API key"
|
35
|
+
a_request(:post, Linguo::Detect::API_URL).should have_been_made
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/spec/linguo_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: linguo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nihad Abbasov
|
9
|
+
- Laurynas Butkus
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-03-14 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: json
|
17
|
+
requirement: &2157232700 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *2157232700
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rake
|
28
|
+
requirement: &2157232260 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *2157232260
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
requirement: &2157231840 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *2157231840
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: webmock
|
50
|
+
requirement: &2157231360 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *2157231360
|
59
|
+
description: Language detection library for Ruby
|
60
|
+
email:
|
61
|
+
- mail@narkoz.me
|
62
|
+
- laurynas.butkus@gmail.com
|
63
|
+
executables: []
|
64
|
+
extensions: []
|
65
|
+
extra_rdoc_files: []
|
66
|
+
files:
|
67
|
+
- .gitignore
|
68
|
+
- .rspec
|
69
|
+
- .travis.yml
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- lib/linguo.rb
|
75
|
+
- lib/linguo/config.rb
|
76
|
+
- lib/linguo/core_ext/string.rb
|
77
|
+
- lib/linguo/detect.rb
|
78
|
+
- lib/linguo/errors.rb
|
79
|
+
- lib/linguo/version.rb
|
80
|
+
- lib/rails/generators/linguo/config_generator.rb
|
81
|
+
- linguo.gemspec
|
82
|
+
- spec/fixtures/response.json
|
83
|
+
- spec/fixtures/wrong_api_key.json
|
84
|
+
- spec/linguo/config_spec.rb
|
85
|
+
- spec/linguo/core_ext_spec.rb
|
86
|
+
- spec/linguo/detect_spec.rb
|
87
|
+
- spec/linguo_spec.rb
|
88
|
+
- spec/spec_helper.rb
|
89
|
+
homepage: https://github.com/narkoz/linguo
|
90
|
+
licenses: []
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
segments:
|
102
|
+
- 0
|
103
|
+
hash: -3651711047218935527
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
hash: -3651711047218935527
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 1.8.17
|
116
|
+
signing_key:
|
117
|
+
specification_version: 3
|
118
|
+
summary: Linguo detects the language of a given piece of text
|
119
|
+
test_files:
|
120
|
+
- spec/fixtures/response.json
|
121
|
+
- spec/fixtures/wrong_api_key.json
|
122
|
+
- spec/linguo/config_spec.rb
|
123
|
+
- spec/linguo/core_ext_spec.rb
|
124
|
+
- spec/linguo/detect_spec.rb
|
125
|
+
- spec/linguo_spec.rb
|
126
|
+
- spec/spec_helper.rb
|