wtf_lang 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +53 -0
- data/lib/wtf_lang.rb +2 -1
- data/lib/wtf_lang/api.rb +4 -11
- data/lib/wtf_lang/core_ext/extension.rb +31 -0
- data/lib/wtf_lang/core_ext/string.rb +2 -22
- data/lib/wtf_lang/core_ext/symbol.rb +6 -0
- data/lib/wtf_lang/language.rb +21 -0
- data/lib/wtf_lang/version.rb +1 -1
- data/test/test_wtf_lang.rb +4 -1
- data/wtf_lang.gemspec +6 -3
- metadata +15 -12
- data/README.rdoc +0 -36
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
### wtf_lang
|
2
|
+
|
3
|
+
Can't stop dreaming about an easy language detector? Just use wtf_language!
|
4
|
+
|
5
|
+
### Install
|
6
|
+
|
7
|
+
gem install wtf_lang
|
8
|
+
|
9
|
+
### Usage
|
10
|
+
|
11
|
+
require 'wtf_lang'
|
12
|
+
|
13
|
+
"ruby is so awesome!".lang # => "en"
|
14
|
+
"ruby is so awesome!".full_lang # => "ENGLISH"
|
15
|
+
|
16
|
+
#with a numeric value between 0-1.0 that represents the confidence level in the language code for the given text.
|
17
|
+
"ruby is so awesome!".lang_confidence # => ["en", 0.15502742]
|
18
|
+
|
19
|
+
"ruby is so awesome!".en? # => true
|
20
|
+
|
21
|
+
If you want to cache results to make less requests to google API, use variables
|
22
|
+
|
23
|
+
text = "ruby is so awesome!"
|
24
|
+
text.lang #=> "en"
|
25
|
+
text.full_lang #=> "ENGLISH"
|
26
|
+
|
27
|
+
It works with symbols too!
|
28
|
+
|
29
|
+
:"ruby is so awesome!".lang # => "en"
|
30
|
+
|
31
|
+
|
32
|
+
### Contributing to wtf_lang
|
33
|
+
|
34
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
35
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
36
|
+
* Fork the project
|
37
|
+
* Start a feature/bugfix branch
|
38
|
+
* Commit and push until you are happy with your contribution
|
39
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
40
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
41
|
+
|
42
|
+
### Authors
|
43
|
+
|
44
|
+
#### [Contributors](http://github.com/nashby/wtf_lang/contributors)
|
45
|
+
- [Bernard Potocki](https://github.com/imanel)
|
46
|
+
|
47
|
+
[Vasiliy Ermolovich](http://github.com/nashby/)<br/>
|
48
|
+
|
49
|
+
### Copyright
|
50
|
+
|
51
|
+
Copyright (c) 2011 nashby. See LICENSE.txt for
|
52
|
+
further details.
|
53
|
+
|
data/lib/wtf_lang.rb
CHANGED
data/lib/wtf_lang/api.rb
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
require 'net/http'
|
4
4
|
require 'json'
|
5
5
|
require 'cgi'
|
6
|
+
require "wtf_lang/language"
|
6
7
|
|
7
8
|
module WtfLang
|
8
9
|
class API
|
@@ -24,17 +25,9 @@ module WtfLang
|
|
24
25
|
end
|
25
26
|
|
26
27
|
def detect(text)
|
27
|
-
response = send(text)
|
28
|
-
parse(response)
|
29
|
-
|
30
|
-
|
31
|
-
def lang(text)
|
32
|
-
detect(text)["responseData"]["language"]
|
33
|
-
end
|
34
|
-
|
35
|
-
#A numeric value between 0-1.0 that represents the confidence level in the language code for the given text.
|
36
|
-
def lang_confidence(text)
|
37
|
-
[detect(text)["responseData"]["language"], detect(text)["responseData"]["confidence"]]
|
28
|
+
response = send(text.to_s)
|
29
|
+
response_data = parse(response)["responseData"]
|
30
|
+
Language.new(response_data["language"], response_data["confidence"])
|
38
31
|
end
|
39
32
|
|
40
33
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module WtfLang
|
2
|
+
module Extension
|
3
|
+
|
4
|
+
require "wtf_lang/api"
|
5
|
+
require "wtf_lang/languages"
|
6
|
+
|
7
|
+
def detect_lang
|
8
|
+
@detected_lang ||= WtfLang::API.detect self
|
9
|
+
end
|
10
|
+
|
11
|
+
def lang
|
12
|
+
detect_lang.lang
|
13
|
+
end
|
14
|
+
|
15
|
+
def full_lang
|
16
|
+
detect_lang.full_lang
|
17
|
+
end
|
18
|
+
|
19
|
+
def lang_confidence
|
20
|
+
detect_lang.lang_confidence
|
21
|
+
end
|
22
|
+
|
23
|
+
WtfLang::LANGUAGES.each do |lang, code|
|
24
|
+
self.class_eval <<-RUBY
|
25
|
+
def #{code}?
|
26
|
+
lang == "#{code}"
|
27
|
+
end
|
28
|
+
RUBY
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -1,26 +1,6 @@
|
|
1
1
|
#encoding: utf-8
|
2
|
-
|
3
|
-
require "wtf_lang/api"
|
4
|
-
require "wtf_lang/languages"
|
2
|
+
require 'wtf_lang/core_ext/extension'
|
5
3
|
|
6
4
|
class String
|
7
|
-
|
8
|
-
WtfLang::API.lang self
|
9
|
-
end
|
10
|
-
|
11
|
-
def full_lang
|
12
|
-
WtfLang::LANGUAGES.key(WtfLang::API.lang self)
|
13
|
-
end
|
14
|
-
|
15
|
-
def lang_confidence
|
16
|
-
WtfLang::API.lang_confidence self
|
17
|
-
end
|
18
|
-
|
19
|
-
WtfLang::LANGUAGES.each do |lang, code|
|
20
|
-
self.class_eval <<-RUBY
|
21
|
-
def #{code}?
|
22
|
-
WtfLang::API.lang(self) == "#{code}"
|
23
|
-
end
|
24
|
-
RUBY
|
25
|
-
end
|
5
|
+
include WtfLang::Extension
|
26
6
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
module WtfLang
|
4
|
+
class Language
|
5
|
+
attr_reader :lang, :confidence
|
6
|
+
|
7
|
+
def initialize(lang, confidence)
|
8
|
+
@lang = lang
|
9
|
+
@confidence = confidence
|
10
|
+
end
|
11
|
+
|
12
|
+
def full_lang
|
13
|
+
LANGUAGES.key(lang)
|
14
|
+
end
|
15
|
+
|
16
|
+
def lang_confidence
|
17
|
+
[lang, confidence]
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
data/lib/wtf_lang/version.rb
CHANGED
data/test/test_wtf_lang.rb
CHANGED
@@ -3,7 +3,6 @@
|
|
3
3
|
require 'helper'
|
4
4
|
|
5
5
|
class TestWtfLang < Test::Unit::TestCase
|
6
|
-
|
7
6
|
should "return en language" do
|
8
7
|
assert_equal "en", "ruby is so awesome!".lang
|
9
8
|
end
|
@@ -28,4 +27,8 @@ class TestWtfLang < Test::Unit::TestCase
|
|
28
27
|
assert "руби такой крутой!".lang_confidence.kind_of? Array
|
29
28
|
end
|
30
29
|
|
30
|
+
should "return en language for symbol" do
|
31
|
+
assert_equal "en", :"ruby is so awesome!".lang
|
32
|
+
end
|
33
|
+
|
31
34
|
end
|
data/wtf_lang.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{wtf_lang}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["nashby"]
|
@@ -14,18 +14,21 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.email = %q{younash@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE.txt",
|
17
|
-
"README.
|
17
|
+
"README.md"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
"Gemfile",
|
22
22
|
"Gemfile.lock",
|
23
23
|
"LICENSE.txt",
|
24
|
-
"README.
|
24
|
+
"README.md",
|
25
25
|
"Rakefile",
|
26
26
|
"lib/wtf_lang.rb",
|
27
27
|
"lib/wtf_lang/api.rb",
|
28
|
+
"lib/wtf_lang/core_ext/extension.rb",
|
28
29
|
"lib/wtf_lang/core_ext/string.rb",
|
30
|
+
"lib/wtf_lang/core_ext/symbol.rb",
|
31
|
+
"lib/wtf_lang/language.rb",
|
29
32
|
"lib/wtf_lang/languages.rb",
|
30
33
|
"lib/wtf_lang/version.rb",
|
31
34
|
"test/helper.rb",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wtf_lang
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -14,7 +14,7 @@ default_executable:
|
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: shoulda
|
17
|
-
requirement: &
|
17
|
+
requirement: &9682284 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *9682284
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: bundler
|
28
|
-
requirement: &
|
28
|
+
requirement: &9681720 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: 1.0.0
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *9681720
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: jeweler
|
39
|
-
requirement: &
|
39
|
+
requirement: &9681384 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ~>
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: 1.6.0
|
45
45
|
type: :development
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *9681384
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: rcov
|
50
|
-
requirement: &
|
50
|
+
requirement: &9681036 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ! '>='
|
@@ -55,24 +55,27 @@ dependencies:
|
|
55
55
|
version: '0'
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
58
|
+
version_requirements: *9681036
|
59
59
|
description: Can't stop dreaming about an easy language detector? Just use wtf_lang!
|
60
60
|
email: younash@gmail.com
|
61
61
|
executables: []
|
62
62
|
extensions: []
|
63
63
|
extra_rdoc_files:
|
64
64
|
- LICENSE.txt
|
65
|
-
- README.
|
65
|
+
- README.md
|
66
66
|
files:
|
67
67
|
- .document
|
68
68
|
- Gemfile
|
69
69
|
- Gemfile.lock
|
70
70
|
- LICENSE.txt
|
71
|
-
- README.
|
71
|
+
- README.md
|
72
72
|
- Rakefile
|
73
73
|
- lib/wtf_lang.rb
|
74
74
|
- lib/wtf_lang/api.rb
|
75
|
+
- lib/wtf_lang/core_ext/extension.rb
|
75
76
|
- lib/wtf_lang/core_ext/string.rb
|
77
|
+
- lib/wtf_lang/core_ext/symbol.rb
|
78
|
+
- lib/wtf_lang/language.rb
|
76
79
|
- lib/wtf_lang/languages.rb
|
77
80
|
- lib/wtf_lang/version.rb
|
78
81
|
- test/helper.rb
|
@@ -94,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
94
97
|
version: '0'
|
95
98
|
segments:
|
96
99
|
- 0
|
97
|
-
hash:
|
100
|
+
hash: -607690351
|
98
101
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
102
|
none: false
|
100
103
|
requirements:
|
data/README.rdoc
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
= wtf_lang
|
2
|
-
|
3
|
-
Can't stop dreaming about an easy language detector? Just use wtf_language!
|
4
|
-
|
5
|
-
== Install
|
6
|
-
|
7
|
-
gem install wtf_lang
|
8
|
-
|
9
|
-
== Usage
|
10
|
-
|
11
|
-
require 'wtf_lang'
|
12
|
-
|
13
|
-
"ruby is so awesome!".lang # => "en"
|
14
|
-
"ruby is so awesome!".full_lang # => "ENGLISH"
|
15
|
-
|
16
|
-
#with a numeric value between 0-1.0 that represents the confidence level in the language code for the given text.
|
17
|
-
"ruby is so awesome!".lang_confidence # => ["en", 0.15502742]
|
18
|
-
|
19
|
-
"ruby is so awesome!".en? # => true
|
20
|
-
|
21
|
-
|
22
|
-
== Contributing to wtf_lang
|
23
|
-
|
24
|
-
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
25
|
-
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
26
|
-
* Fork the project
|
27
|
-
* Start a feature/bugfix branch
|
28
|
-
* Commit and push until you are happy with your contribution
|
29
|
-
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
30
|
-
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
31
|
-
|
32
|
-
== Copyright
|
33
|
-
|
34
|
-
Copyright (c) 2011 nashby. See LICENSE.txt for
|
35
|
-
further details.
|
36
|
-
|