urbanlexicophile 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +22 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +57 -0
- data/Rakefile +55 -0
- data/lib/urbanlexicophile/definition.rb +11 -0
- data/lib/urbanlexicophile/reader.rb +119 -0
- data/lib/urbanlexicophile.rb +20 -0
- data/test/helper.rb +18 -0
- data/test/test_urbanlexicophile.rb +30 -0
- metadata +128 -0
data/.document
ADDED
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
|
+
gem 'nokogiri'
|
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.4"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.6.4)
|
6
|
+
bundler (~> 1.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
nokogiri (1.5.0)
|
10
|
+
rake (0.9.2.2)
|
11
|
+
rcov (0.9.11)
|
12
|
+
shoulda (2.11.3)
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
ruby
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
bundler (~> 1.0.0)
|
19
|
+
jeweler (~> 1.6.4)
|
20
|
+
nokogiri
|
21
|
+
rcov
|
22
|
+
shoulda
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Dave Kerr
|
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,57 @@
|
|
1
|
+
= UrbanLexicophile
|
2
|
+
|
3
|
+
UrbanLexicophile allows you to retrieve definitions of terms from UrbanDictionary.com
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
gem install urbanlexicophile
|
8
|
+
|
9
|
+
== Note on Usage
|
10
|
+
|
11
|
+
UrbanDictionary.com does not provider a public API, please be mindful of this if you use this gem. It works by scraping each page for the definitions. It's probably better if you don't use this gem for anything but personal research, but if you do use it for otherwise please be considerate and cache results and give credit and a link to UrbanDictionary.com
|
12
|
+
|
13
|
+
== Usage
|
14
|
+
|
15
|
+
First, call this to get an array of all definitions:
|
16
|
+
definitions = UrbanLexicophile.define("urban dictionary")
|
17
|
+
|
18
|
+
Then you can iterate through them in the usual manner:
|
19
|
+
definitions.each do |definition|
|
20
|
+
# display all information about each definition
|
21
|
+
puts definition.title
|
22
|
+
puts definition.definition
|
23
|
+
puts definition.example
|
24
|
+
puts definition.author
|
25
|
+
end
|
26
|
+
|
27
|
+
As the definitions are returned as an array, you could just do this if you only one the most popular definition:
|
28
|
+
most_popular = definition.first
|
29
|
+
|
30
|
+
If there are no definitions for the desired term, the return is just an empty array.
|
31
|
+
|
32
|
+
== Dependencies
|
33
|
+
|
34
|
+
* Nokogiri
|
35
|
+
|
36
|
+
== TODO
|
37
|
+
|
38
|
+
* Retrieve vote information about each definition and implement Definition.score()
|
39
|
+
* Retrieve the Urban Word of the Day
|
40
|
+
* Retrieve information about definition authors
|
41
|
+
|
42
|
+
|
43
|
+
== Contributing to UrbanLexicophile
|
44
|
+
|
45
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
46
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
47
|
+
* Fork the project
|
48
|
+
* Start a feature/bugfix branch
|
49
|
+
* Commit and push until you are happy with your contribution
|
50
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
51
|
+
* 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.
|
52
|
+
|
53
|
+
== Copyright
|
54
|
+
|
55
|
+
Copyright (c) 2011 Dave Kerr. See LICENSE.txt for
|
56
|
+
further details.
|
57
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
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
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "urbanlexicophile"
|
18
|
+
gem.homepage = "http://github.com/DaveKerr/urbanlexicophile"
|
19
|
+
gem.version = "0.1.0"
|
20
|
+
gem.license = "MIT"
|
21
|
+
gem.summary = "A way of retrieving UrbanDictionary.com definitions."
|
22
|
+
gem.description = "A way of retrieving UrbanDictionary.com definitions. Gives you access to all definitions of a term and associated data."
|
23
|
+
gem.email = "dave.kerr@gmail.com"
|
24
|
+
gem.authors = ["Dave Kerr"]
|
25
|
+
gem.add_dependency 'nokogiri'
|
26
|
+
# dependencies defined in Gemfile
|
27
|
+
end
|
28
|
+
Jeweler::RubygemsDotOrgTasks.new
|
29
|
+
|
30
|
+
require 'rake/testtask'
|
31
|
+
Rake::TestTask.new(:test) do |test|
|
32
|
+
test.libs << 'lib' << 'test'
|
33
|
+
test.pattern = 'test/**/test_*.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
|
37
|
+
require 'rcov/rcovtask'
|
38
|
+
Rcov::RcovTask.new do |test|
|
39
|
+
test.libs << 'test'
|
40
|
+
test.pattern = 'test/**/test_*.rb'
|
41
|
+
test.verbose = true
|
42
|
+
test.rcov_opts << '--exclude "gems/*"'
|
43
|
+
end
|
44
|
+
|
45
|
+
task :default => :test
|
46
|
+
|
47
|
+
require 'rake/rdoctask'
|
48
|
+
Rake::RDocTask.new do |rdoc|
|
49
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
50
|
+
|
51
|
+
rdoc.rdoc_dir = 'rdoc'
|
52
|
+
rdoc.title = "urbanlexicophile #{version}"
|
53
|
+
rdoc.rdoc_files.include('README*')
|
54
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
55
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
|
3
|
+
class UrbanLexicophile
|
4
|
+
class Reader
|
5
|
+
|
6
|
+
def lookup_term(term)
|
7
|
+
@address = "http://www.urbandictionary.com/define.php?term="
|
8
|
+
url = @address + term.gsub(" ", "+")
|
9
|
+
|
10
|
+
definitions = []
|
11
|
+
|
12
|
+
# perform the search
|
13
|
+
@ng = Nokogiri::HTML(open(url+"&page=1"))
|
14
|
+
|
15
|
+
#determine if search has a defined term
|
16
|
+
if self.term_defined?
|
17
|
+
#get definitions from first page of definitions
|
18
|
+
|
19
|
+
definitions = get_all_definitions
|
20
|
+
|
21
|
+
# same again for check for more pages
|
22
|
+
page = 1
|
23
|
+
while @ng.css("a.next_page").count > 0
|
24
|
+
page += 1
|
25
|
+
@ng = Nokogiri::HTML(open(url+"&page=#{page.to_s}"))
|
26
|
+
definitions += get_all_definitions
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
return definitions
|
33
|
+
end
|
34
|
+
|
35
|
+
def get_all_definitions
|
36
|
+
|
37
|
+
table = @ng.css("#entries")
|
38
|
+
|
39
|
+
# a definition takes up at least 2 <tr> and has no semantic separator between definitions
|
40
|
+
# to make it even harder, some definitions have more than 2 <tr>s
|
41
|
+
# this is a best attempt at discovering definitions, and passing them to get_one_definition for parsing
|
42
|
+
|
43
|
+
finished_definitions = []
|
44
|
+
definition_in_progress = []
|
45
|
+
|
46
|
+
table.css("tr").each do |row|
|
47
|
+
|
48
|
+
# check if the row is an index. If it is, clear out the in_progress definition and parese the first line
|
49
|
+
if is_index_row? row
|
50
|
+
definition_in_progress = []
|
51
|
+
definition_in_progress << parse_title_row(row)
|
52
|
+
|
53
|
+
elsif is_definition_row? row
|
54
|
+
definition_in_progress << parse_definition_row(row)
|
55
|
+
finished_definitions << build_definition_object(definition_in_progress)
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
return finished_definitions # should be an array of complete Definition objects
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
def term_defined?
|
67
|
+
if @ng.css("#not_defined_yet").count > 1
|
68
|
+
return false
|
69
|
+
else
|
70
|
+
return true
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def is_index_row?(row)
|
75
|
+
if row.css("td.index").count > 0
|
76
|
+
return true
|
77
|
+
else
|
78
|
+
return false
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def is_definition_row?(row)
|
83
|
+
if row.css("div.definition").count > 0
|
84
|
+
return true
|
85
|
+
else
|
86
|
+
return false
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def parse_title_row(row)
|
91
|
+
|
92
|
+
title = row.css(".word").first.content.to_s.strip
|
93
|
+
return {:title => title}
|
94
|
+
end
|
95
|
+
|
96
|
+
def parse_definition_row(row)
|
97
|
+
|
98
|
+
definition = row.css(".definition").first.content.to_s.strip
|
99
|
+
example = row.css(".example").first.content.to_s.strip
|
100
|
+
author = row.css(".greenery a.author").first.content.to_s.strip
|
101
|
+
|
102
|
+
return {:definition => definition, :example => example, :author => author}
|
103
|
+
end
|
104
|
+
|
105
|
+
def build_definition_object(def_array)
|
106
|
+
title_details = def_array.first
|
107
|
+
definition_details = def_array.last
|
108
|
+
|
109
|
+
definition = Definition.new
|
110
|
+
definition.title = title_details[:title]
|
111
|
+
definition.author = definition_details[:author]
|
112
|
+
definition.definition = definition_details[:definition]
|
113
|
+
definition.example = definition_details[:example]
|
114
|
+
|
115
|
+
return definition
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require File.expand_path('../urbanlexicophile/definition', __FILE__)
|
3
|
+
require File.expand_path('../urbanlexicophile/reader', __FILE__)
|
4
|
+
|
5
|
+
class UrbanLexicophile
|
6
|
+
module Version
|
7
|
+
MAJOR = 0
|
8
|
+
MINOR = 1
|
9
|
+
PATCH = 0
|
10
|
+
|
11
|
+
STRING = [MAJOR, MINOR, PATCH].compact.join('.')
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.define(term)
|
15
|
+
reader = UrbanLexicophile::Reader.new
|
16
|
+
definitions = reader.lookup_term(term)
|
17
|
+
return definitions
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
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 'urbanlexicophile'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestUrbanLexicophile < Test::Unit::TestCase
|
4
|
+
should "define() should return an array of definitions" do
|
5
|
+
a = UrbanLexicophile.define("lol")
|
6
|
+
unless a.is_a?(Array) && a.first.is_a?(UrbanLexicophile::Definition)
|
7
|
+
flunk "Returned value is not an array"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
should "non existing definitions should be handled gracefully" do
|
12
|
+
begin
|
13
|
+
UrbanLexicophile.define("dfgsf nwe ask2j 9")
|
14
|
+
rescue
|
15
|
+
flunk "Yeah, that's broke"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
should "everything should just work " do
|
20
|
+
begin
|
21
|
+
a = UrbanLexicophile.define("okay")
|
22
|
+
puts "Definition for #{a.first.title}:"
|
23
|
+
puts "\t #{a.first.definition}"
|
24
|
+
puts "\t Usage: #{a.first.example}"
|
25
|
+
puts "\t Author: #{a.first.author}"
|
26
|
+
rescue
|
27
|
+
flunk "Something is broken :("
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: urbanlexicophile
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dave Kerr
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-16 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: nokogiri
|
16
|
+
requirement: &70133494235960 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70133494235960
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: shoulda
|
27
|
+
requirement: &70133494221460 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70133494221460
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: bundler
|
38
|
+
requirement: &70133494217180 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.0.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70133494217180
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: jeweler
|
49
|
+
requirement: &70133494206060 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.6.4
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70133494206060
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rcov
|
60
|
+
requirement: &70133494195220 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70133494195220
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: nokogiri
|
71
|
+
requirement: &70133494100120 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :runtime
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70133494100120
|
80
|
+
description: A way of retrieving UrbanDictionary.com definitions. Gives you access
|
81
|
+
to all definitions of a term and associated data.
|
82
|
+
email: dave.kerr@gmail.com
|
83
|
+
executables: []
|
84
|
+
extensions: []
|
85
|
+
extra_rdoc_files:
|
86
|
+
- LICENSE.txt
|
87
|
+
- README.rdoc
|
88
|
+
files:
|
89
|
+
- .document
|
90
|
+
- Gemfile
|
91
|
+
- Gemfile.lock
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.rdoc
|
94
|
+
- Rakefile
|
95
|
+
- lib/urbanlexicophile.rb
|
96
|
+
- lib/urbanlexicophile/definition.rb
|
97
|
+
- lib/urbanlexicophile/reader.rb
|
98
|
+
- test/helper.rb
|
99
|
+
- test/test_urbanlexicophile.rb
|
100
|
+
homepage: http://github.com/DaveKerr/urbanlexicophile
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
segments:
|
114
|
+
- 0
|
115
|
+
hash: -574315987937456029
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
118
|
+
requirements:
|
119
|
+
- - ! '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 1.8.10
|
125
|
+
signing_key:
|
126
|
+
specification_version: 3
|
127
|
+
summary: A way of retrieving UrbanDictionary.com definitions.
|
128
|
+
test_files: []
|