wtf_lang 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.0"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.0)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ shoulda (2.11.3)
12
+
13
+ PLATFORMS
14
+ x86-mingw32
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.6.0)
19
+ rcov
20
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 nashby
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,36 @@
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
+
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ $LOAD_PATH.unshift('lib')
15
+
16
+ require 'jeweler'
17
+ require 'wtf_lang/version'
18
+
19
+ Jeweler::Tasks.new do |gem|
20
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
21
+ gem.name = "wtf_lang"
22
+ gem.homepage = "http://github.com/nashby/wtf_lang"
23
+ gem.version = WtfLang::Version::STRING
24
+ gem.license = "MIT"
25
+ gem.summary = %Q{An awesome language detector}
26
+ gem.description = %Q{Can't stop dreaming about an easy language detector? Just use wtf_lang!}
27
+ gem.email = "younash@gmail.com"
28
+ gem.authors = ["nashby"]
29
+ gem.files.include Dir.glob('lib/**/*.rb')
30
+ # dependencies defined in Gemfile
31
+ end
32
+ Jeweler::RubygemsDotOrgTasks.new
33
+
34
+ require 'rake/testtask'
35
+ Rake::TestTask.new(:test) do |test|
36
+ test.libs << 'lib' << 'test'
37
+ test.pattern = 'test/**/test_*.rb'
38
+ test.verbose = true
39
+ end
40
+
41
+ require 'rcov/rcovtask'
42
+ Rcov::RcovTask.new do |test|
43
+ test.libs << 'test'
44
+ test.pattern = 'test/**/test_*.rb'
45
+ test.verbose = true
46
+ test.rcov_opts << '--exclude "gems/*"'
47
+ end
48
+
49
+ task :default => :test
50
+
51
+ require 'rake/rdoctask'
52
+ Rake::RDocTask.new do |rdoc|
53
+ version = WtfLang::Version::STRING
54
+
55
+ rdoc.rdoc_dir = 'rdoc'
56
+ rdoc.title = "wtf_lang #{version}"
57
+ rdoc.rdoc_files.include('README*')
58
+ rdoc.rdoc_files.include('lib/**/*.rb')
59
+ end
@@ -0,0 +1,42 @@
1
+ #encoding: utf-8
2
+
3
+ require 'net/http'
4
+ require 'json'
5
+ require 'cgi'
6
+
7
+ module WtfLang
8
+ class API
9
+
10
+ BASE_URL = "http://www.google.com/uds/GlangDetect"
11
+ VERSION = "?v=1.0"
12
+
13
+ class << self
14
+ def get_url(text)
15
+ BASE_URL+VERSION+"&q="+CGI.escape(text)
16
+ end
17
+
18
+ def send(text)
19
+ Net::HTTP.get(URI.parse(get_url(text)))
20
+ end
21
+
22
+ def parse(response)
23
+ JSON.parse(response)
24
+ end
25
+
26
+ def detect(text)
27
+ response = send(text)
28
+ parse(response)
29
+ end
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"]]
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,26 @@
1
+ #encoding: utf-8
2
+
3
+ require "wtf_lang/api"
4
+ require "wtf_lang/languages"
5
+
6
+ class String
7
+ def lang
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
26
+ end
@@ -0,0 +1,107 @@
1
+ module WtfLang
2
+ LANGUAGES = {
3
+ 'AFRIKAANS' => 'af',
4
+ 'ALBANIAN' => 'sq',
5
+ 'AMHARIC' => 'am',
6
+ 'ARABIC' => 'ar',
7
+ 'ARMENIAN' => 'hy',
8
+ 'AZERBAIJANI' => 'az',
9
+ 'BASQUE' => 'eu',
10
+ 'BELARUSIAN' => 'be',
11
+ 'BENGALI' => 'bn',
12
+ 'BIHARI' => 'bh',
13
+ 'BRETON' => 'br',
14
+ 'BULGARIAN' => 'bg',
15
+ 'BURMESE' => 'my',
16
+ 'CATALAN' => 'ca',
17
+ 'CHEROKEE' => 'chr',
18
+ 'CHINESE' => 'zh',
19
+ 'CORSICAN' => 'co',
20
+ 'CROATIAN' => 'hr',
21
+ 'CZECH' => 'cs',
22
+ 'DANISH' => 'da',
23
+ 'DHIVEHI' => 'dv',
24
+ 'DUTCH'=> 'nl',
25
+ 'ENGLISH' => 'en',
26
+ 'ESPERANTO' => 'eo',
27
+ 'ESTONIAN' => 'et',
28
+ 'FAROESE' => 'fo',
29
+ 'FILIPINO' => 'tl',
30
+ 'FINNISH' => 'fi',
31
+ 'FRENCH' => 'fr',
32
+ 'FRISIAN' => 'fy',
33
+ 'GALICIAN' => 'gl',
34
+ 'GEORGIAN' => 'ka',
35
+ 'GERMAN' => 'de',
36
+ 'GREEK' => 'el',
37
+ 'GUJARATI' => 'gu',
38
+ 'HAITIAN_CREOLE' => 'ht',
39
+ 'HEBREW' => 'iw',
40
+ 'HINDI' => 'hi',
41
+ 'HUNGARIAN' => 'hu',
42
+ 'ICELANDIC' => 'is',
43
+ 'INDONESIAN' => 'id',
44
+ 'INUKTITUT' => 'iu',
45
+ 'IRISH' => 'ga',
46
+ 'ITALIAN' => 'it',
47
+ 'JAPANESE' => 'ja',
48
+ 'JAVANESE' => 'jw',
49
+ 'KANNADA' => 'kn',
50
+ 'KAZAKH' => 'kk',
51
+ 'KHMER' => 'km',
52
+ 'KOREAN' => 'ko',
53
+ 'KURDISH'=> 'ku',
54
+ 'KYRGYZ'=> 'ky',
55
+ 'LAO' => 'lo',
56
+ 'LATIN' => 'la',
57
+ 'LATVIAN' => 'lv',
58
+ 'LITHUANIAN' => 'lt',
59
+ 'LUXEMBOURGISH' => 'lb',
60
+ 'MACEDONIAN' => 'mk',
61
+ 'MALAY' => 'ms',
62
+ 'MALAYALAM' => 'ml',
63
+ 'MALTESE' => 'mt',
64
+ 'MAORI' => 'mi',
65
+ 'MARATHI' => 'mr',
66
+ 'MONGOLIAN' => 'mn',
67
+ 'NEPALI' => 'ne',
68
+ 'NORWEGIAN' => 'no',
69
+ 'OCCITAN' => 'oc',
70
+ 'PASHTO' => 'ps',
71
+ 'PERSIAN' => 'fa',
72
+ 'POLISH' => 'pl',
73
+ 'PORTUGUESE' => 'pt',
74
+ 'PUNJABI' => 'pa',
75
+ 'QUECHUA' => 'qu',
76
+ 'ROMANIAN' => 'ro',
77
+ 'RUSSIAN' => 'ru',
78
+ 'SANSKRIT' => 'sa',
79
+ 'SCOTS_GAELIC' => 'gd',
80
+ 'SERBIAN' => 'sr',
81
+ 'SINDHI' => 'sd',
82
+ 'SINHALESE' => 'si',
83
+ 'SLOVAK' => 'sk',
84
+ 'SLOVENIAN' => 'sl',
85
+ 'SPANISH' => 'es',
86
+ 'SUNDANESE' => 'su',
87
+ 'SWAHILI' => 'sw',
88
+ 'SWEDISH' => 'sv',
89
+ 'SYRIAC' => 'syr',
90
+ 'TAJIK' => 'tg',
91
+ 'TAMIL' => 'ta',
92
+ 'TATAR' => 'tt',
93
+ 'TELUGU' => 'te',
94
+ 'THAI' => 'th',
95
+ 'TIBETAN' => 'bo',
96
+ 'TONGA' => 'to',
97
+ 'TURKISH' => 'tr',
98
+ 'UKRAINIAN' => 'uk',
99
+ 'URDU' => 'ur',
100
+ 'UZBEK' => 'uz',
101
+ 'UIGHUR' => 'ug',
102
+ 'VIETNAMESE' => 'vi',
103
+ 'WELSH' => 'cy',
104
+ 'YIDDISH' => 'yi',
105
+ 'YORUBA' => 'yo'
106
+ }
107
+ end
@@ -0,0 +1,10 @@
1
+ module WtfLang
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+ BUILD = nil
7
+
8
+ STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
9
+ end
10
+ end
data/lib/wtf_lang.rb ADDED
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '.', 'lib')
2
+
3
+ require 'wtf_lang/core_ext/string'
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'wtf_lang'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,31 @@
1
+ #encoding: utf-8
2
+
3
+ require 'helper'
4
+
5
+ class TestWtfLang < Test::Unit::TestCase
6
+
7
+ should "return en language" do
8
+ assert_equal "en", "ruby is so awesome!".lang
9
+ end
10
+
11
+ should "return ru language" do
12
+ assert_equal "ru", "руби такой крутой!".lang
13
+ end
14
+
15
+ should "return jp language" do
16
+ assert_equal "ja", "ルビーはとても素晴らしいです!".lang
17
+ end
18
+
19
+ should "return full language name" do
20
+ assert_equal "RUSSIAN", "руби такой крутой!".full_lang
21
+ end
22
+
23
+ should "return assert language" do
24
+ assert "ruby is so awesome!".en?
25
+ end
26
+
27
+ should "return confidence" do
28
+ assert "руби такой крутой!".lang_confidence.kind_of? Array
29
+ end
30
+
31
+ end
data/wtf_lang.gemspec ADDED
@@ -0,0 +1,62 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{wtf_lang}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["nashby"]
12
+ s.date = %q{2011-05-07}
13
+ s.description = %q{Can't stop dreaming about an easy language detector? Just use wtf_lang!}
14
+ s.email = %q{younash@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "lib/wtf_lang.rb",
27
+ "lib/wtf_lang/api.rb",
28
+ "lib/wtf_lang/core_ext/string.rb",
29
+ "lib/wtf_lang/languages.rb",
30
+ "lib/wtf_lang/version.rb",
31
+ "test/helper.rb",
32
+ "test/test_wtf_lang.rb",
33
+ "wtf_lang.gemspec"
34
+ ]
35
+ s.homepage = %q{http://github.com/nashby/wtf_lang}
36
+ s.licenses = ["MIT"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.6.2}
39
+ s.summary = %q{An awesome language detector}
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
46
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
47
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
48
+ s.add_development_dependency(%q<rcov>, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<shoulda>, [">= 0"])
51
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
52
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
53
+ s.add_dependency(%q<rcov>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<shoulda>, [">= 0"])
57
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
58
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
59
+ s.add_dependency(%q<rcov>, [">= 0"])
60
+ end
61
+ end
62
+
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wtf_lang
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - nashby
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-05-07 00:00:00.000000000 +03:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: shoulda
17
+ requirement: &9757692 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *9757692
26
+ - !ruby/object:Gem::Dependency
27
+ name: bundler
28
+ requirement: &9757404 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *9757404
37
+ - !ruby/object:Gem::Dependency
38
+ name: jeweler
39
+ requirement: &9756924 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.6.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *9756924
48
+ - !ruby/object:Gem::Dependency
49
+ name: rcov
50
+ requirement: &9756396 !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: *9756396
59
+ description: Can't stop dreaming about an easy language detector? Just use wtf_lang!
60
+ email: younash@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files:
64
+ - LICENSE.txt
65
+ - README.rdoc
66
+ files:
67
+ - .document
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE.txt
71
+ - README.rdoc
72
+ - Rakefile
73
+ - lib/wtf_lang.rb
74
+ - lib/wtf_lang/api.rb
75
+ - lib/wtf_lang/core_ext/string.rb
76
+ - lib/wtf_lang/languages.rb
77
+ - lib/wtf_lang/version.rb
78
+ - test/helper.rb
79
+ - test/test_wtf_lang.rb
80
+ - wtf_lang.gemspec
81
+ has_rdoc: true
82
+ homepage: http://github.com/nashby/wtf_lang
83
+ licenses:
84
+ - MIT
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ segments:
96
+ - 0
97
+ hash: 871381903
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 1.6.2
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: An awesome language detector
110
+ test_files: []