wtf_lang 0.3.1 → 0.4.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
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor/bundle
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
data/Gemfile CHANGED
@@ -1,10 +1,8 @@
1
- source "http://rubygems.org"
2
-
3
- gem "json"
4
-
5
- group :development do
6
- gem "shoulda", ">= 0"
7
- gem "bundler", "~> 1.0.0"
8
- gem "jeweler", "~> 1.6.0"
9
- gem "rcov", ">= 0"
10
- end
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+ gem 'vcr', '2.0.0.rc2'
7
+ gem 'webmock'
8
+ gem 'minitest', '~> 2.11.0'
data/LICENSE.txt CHANGED
@@ -1,20 +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.
1
+ Copyright (c) 2011 Vasiliy Ermolovich
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.md CHANGED
@@ -1,54 +1,56 @@
1
- ### wtf_lang
2
-
3
- Can't stop dreaming about an easy language detector? Just use wtf_lang!
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
- "ruby is so awesome!".english? # => true
21
-
22
- If you want to cache results to make less requests to google API, use variables
23
-
24
- text = "ruby is so awesome!"
25
- text.lang #=> "en"
26
- text.full_lang #=> "english"
27
-
28
- It works with symbols too!
29
-
30
- :"ruby is so awesome!".lang # => "en"
31
-
32
-
33
- ### Contributing to wtf_lang
34
-
35
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
36
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
37
- * Fork the project
38
- * Start a feature/bugfix branch
39
- * Commit and push until you are happy with your contribution
40
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
41
- * 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.
42
-
43
- ### Authors
44
-
45
- #### [Contributors](http://github.com/nashby/wtf_lang/contributors)
46
- - [Bernard Potocki](https://github.com/imanel)
47
-
48
- [Vasiliy Ermolovich](http://github.com/nashby/)<br/>
49
-
50
- ### Copyright
51
-
52
- Copyright (c) 2011 nashby. See LICENSE.txt for
53
- further details.
54
-
1
+ # wtf_lang [![TravisCI](https://secure.travis-ci.org/nashby/wtf_lang.png?branch=master)](http://travis-ci.org/nashby/wtf_lang)
2
+
3
+ Can't stop dreaming about an easy language detector? Just use wtf_lang!
4
+
5
+ ### Install
6
+
7
+ gem install wtf_lang
8
+
9
+ ### Usage
10
+
11
+ require 'wtf_lang'
12
+
13
+ WtfLang::API.key = "your_api_key" # you can get it here http://detectlanguage.com/
14
+
15
+ "ruby is so awesome!".lang # => "en"
16
+ "ruby is so awesome!".full_lang # => "ENGLISH"
17
+
18
+ #with a numeric value between 0-1.0 that represents the confidence level in the language code for the given text.
19
+ "ruby is so awesome!".lang_confidence # => ["en", 0.15502742]
20
+
21
+ "ruby is so awesome!".en? # => true
22
+ "ruby is so awesome!".english? # => true
23
+
24
+ If you want to cache results to make less requests to google API, use variables
25
+
26
+ text = "ruby is so awesome!"
27
+ text.lang #=> "en"
28
+ text.full_lang #=> "english"
29
+
30
+ It works with symbols too!
31
+
32
+ :"ruby is so awesome!".lang # => "en"
33
+
34
+
35
+ ### Contributing to wtf_lang
36
+
37
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
38
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
39
+ * Fork the project
40
+ * Start a feature/bugfix branch
41
+ * Commit and push until you are happy with your contribution
42
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
43
+ * 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.
44
+
45
+ ### Authors
46
+
47
+ #### [Contributors](http://github.com/nashby/wtf_lang/contributors)
48
+ - [Bernard Potocki](https://github.com/imanel)
49
+
50
+ [Vasiliy Ermolovich](http://github.com/nashby/)<br/>
51
+
52
+ ### Copyright
53
+
54
+ Copyright (c) 2012 nashby. See LICENSE.txt for
55
+ further details.
56
+
data/Rakefile CHANGED
@@ -1,59 +1,11 @@
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
- gem.add_dependency 'json'
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
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ t.pattern = 'test/*_test.rb'
8
+ t.verbose = true
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,151 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://ws.detectlanguage.com/0.2/detect?key=demo&q=ruby%20is%20so%20awesome!
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - ! '*/*'
12
+ User-Agent:
13
+ - Ruby
14
+ response:
15
+ status:
16
+ code: 200
17
+ message: OK
18
+ headers:
19
+ Content-Type:
20
+ - application/json;charset=utf-8
21
+ Connection:
22
+ - keep-alive
23
+ Status:
24
+ - '200'
25
+ X-Powered-By:
26
+ - Phusion Passenger (mod_rails/mod_rack) 3.0.6
27
+ X-Frame-Options:
28
+ - sameorigin
29
+ X-Xss-Protection:
30
+ - 1; mode=block
31
+ Content-Length:
32
+ - '162'
33
+ Server:
34
+ - nginx/0.8.54 + Phusion Passenger 3.0.6 (mod_rails/mod_rack)
35
+ body:
36
+ encoding: US-ASCII
37
+ string: ! '{"data":{"detections":[{"isReliable":false,"confidence":0.137763371150729,"language":"en"},{"isReliable":false,"confidence":0.0441014332965821,"language":"sl"}]}}'
38
+ http_version: !!null
39
+ recorded_at: Sat, 03 Mar 2012 21:26:33 GMT
40
+ - request:
41
+ method: get
42
+ uri: http://ws.detectlanguage.com/0.2/detect?key=demo&q=%D1%80%D1%83%D0%B1%D0%B8%20%D1%82%D0%B0%D0%BA%D0%BE%D0%B9%20%D0%BA%D1%80%D1%83%D1%82%D0%BE%D0%B9!
43
+ body:
44
+ encoding: US-ASCII
45
+ string: ''
46
+ headers:
47
+ Accept:
48
+ - ! '*/*'
49
+ User-Agent:
50
+ - Ruby
51
+ response:
52
+ status:
53
+ code: 200
54
+ message: OK
55
+ headers:
56
+ Content-Type:
57
+ - application/json;charset=utf-8
58
+ Connection:
59
+ - keep-alive
60
+ Status:
61
+ - '200'
62
+ X-Powered-By:
63
+ - Phusion Passenger (mod_rails/mod_rack) 3.0.6
64
+ X-Frame-Options:
65
+ - sameorigin
66
+ X-Xss-Protection:
67
+ - 1; mode=block
68
+ Content-Length:
69
+ - '92'
70
+ Server:
71
+ - nginx/0.8.54 + Phusion Passenger 3.0.6 (mod_rails/mod_rack)
72
+ body:
73
+ encoding: US-ASCII
74
+ string: ! '{"data":{"detections":[{"isReliable":true,"confidence":0.247718383311604,"language":"ru"}]}}'
75
+ http_version: !!null
76
+ recorded_at: Sat, 03 Mar 2012 21:26:33 GMT
77
+ - request:
78
+ method: get
79
+ uri: http://ws.detectlanguage.com/0.2/detect?key=demo&q=1234
80
+ body:
81
+ encoding: US-ASCII
82
+ string: ''
83
+ headers:
84
+ Accept:
85
+ - ! '*/*'
86
+ User-Agent:
87
+ - Ruby
88
+ response:
89
+ status:
90
+ code: 200
91
+ message: OK
92
+ headers:
93
+ Content-Type:
94
+ - application/json;charset=utf-8
95
+ Connection:
96
+ - keep-alive
97
+ Status:
98
+ - '200'
99
+ X-Powered-By:
100
+ - Phusion Passenger (mod_rails/mod_rack) 3.0.6
101
+ X-Frame-Options:
102
+ - sameorigin
103
+ X-Xss-Protection:
104
+ - 1; mode=block
105
+ Content-Length:
106
+ - '26'
107
+ Server:
108
+ - nginx/0.8.54 + Phusion Passenger 3.0.6 (mod_rails/mod_rack)
109
+ body:
110
+ encoding: US-ASCII
111
+ string: ! '{"data":{"detections":[]}}'
112
+ http_version: !!null
113
+ recorded_at: Sat, 03 Mar 2012 21:26:34 GMT
114
+ - request:
115
+ method: get
116
+ uri: http://ws.detectlanguage.com/0.2/detect?key=demo&q=%E3%83%AB%E3%83%93%E3%83%BC%E3%81%AF%E3%81%A8%E3%81%A6%E3%82%82%E7%B4%A0%E6%99%B4%E3%82%89%E3%81%97%E3%81%84%E3%81%A7%E3%81%99!
117
+ body:
118
+ encoding: US-ASCII
119
+ string: ''
120
+ headers:
121
+ Accept:
122
+ - ! '*/*'
123
+ User-Agent:
124
+ - Ruby
125
+ response:
126
+ status:
127
+ code: 200
128
+ message: OK
129
+ headers:
130
+ Content-Type:
131
+ - application/json;charset=utf-8
132
+ Connection:
133
+ - keep-alive
134
+ Status:
135
+ - '200'
136
+ X-Powered-By:
137
+ - Phusion Passenger (mod_rails/mod_rack) 3.0.6
138
+ X-Frame-Options:
139
+ - sameorigin
140
+ X-Xss-Protection:
141
+ - 1; mode=block
142
+ Content-Length:
143
+ - '79'
144
+ Server:
145
+ - nginx/0.8.54 + Phusion Passenger 3.0.6 (mod_rails/mod_rack)
146
+ body:
147
+ encoding: US-ASCII
148
+ string: ! '{"data":{"detections":[{"isReliable":true,"confidence":1.44,"language":"ja"}]}}'
149
+ http_version: !!null
150
+ recorded_at: Sat, 03 Mar 2012 21:26:34 GMT
151
+ recorded_with: VCR 2.0.0.rc2
data/lib/wtf_lang.rb CHANGED
@@ -1,4 +1,4 @@
1
- $LOAD_PATH << File.join(File.dirname(__FILE__), '.', 'lib')
2
-
3
- require 'wtf_lang/core_ext/string'
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '.', 'lib')
2
+
3
+ require 'wtf_lang/core_ext/string'
4
4
  require 'wtf_lang/core_ext/symbol'
data/lib/wtf_lang/api.rb CHANGED
@@ -1,35 +1,42 @@
1
- #encoding: utf-8
2
-
3
- require 'net/http'
4
- require 'json'
5
- require 'cgi'
6
- require "wtf_lang/language"
7
-
8
- module WtfLang
9
- class API
10
-
11
- BASE_URL = "http://www.google.com/uds/GlangDetect"
12
- VERSION = "?v=1.0"
13
-
14
- class << self
15
- def get_url(text)
16
- BASE_URL+VERSION+"&q="+CGI.escape(text)
17
- end
18
-
19
- def send(text)
20
- Net::HTTP.get(URI.parse(get_url(text)))
21
- end
22
-
23
- def parse(response)
24
- JSON.parse(response)
25
- end
26
-
27
- def detect(text)
28
- response = send(text.to_s)
29
- response_data = parse(response)["responseData"]
30
- Language.new(response_data["language"], response_data["confidence"])
31
- end
32
-
33
- end
34
- end
35
- end
1
+ #encoding: utf-8
2
+
3
+ require 'net/http'
4
+ require 'json'
5
+ require 'cgi'
6
+ require "wtf_lang/language"
7
+
8
+ module WtfLang
9
+ class API
10
+
11
+ BASE_URL = "http://ws.detectlanguage.com/0.2/detect"
12
+
13
+ class << self
14
+ def get_url(text)
15
+ "#{BASE_URL}?key=#{key}&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.to_s)
28
+ if response_data = parse(response)["data"]["detections"].first
29
+ Language.new(response_data["language"], response_data["confidence"])
30
+ end
31
+ end
32
+
33
+ def key
34
+ @@key
35
+ end
36
+
37
+ def key=(value)
38
+ @@key = value
39
+ end
40
+ end
41
+ end
42
+ end