dbpedia_concept_search 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README.md +25 -0
- data/lib/dbpedia_concept_search.rb +97 -0
- data/lib/example.rb +27 -0
- metadata +93 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Rob Nichols
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
DbpediaConceptSearch
|
2
|
+
====================
|
3
|
+
|
4
|
+
A tool that queries dbpedia.org's lookup tool to find concepts, and then
|
5
|
+
bundles the results into either ruby objects (using Hashie) or json.
|
6
|
+
|
7
|
+
Usage
|
8
|
+
-----
|
9
|
+
|
10
|
+
The initial search is defined by creating a new DbpediaConceptSearch:
|
11
|
+
|
12
|
+
search = DbpediaConceptSearch.new('place', 'London')
|
13
|
+
|
14
|
+
This will lead to a query matching [this](http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=place&QueryString=London)
|
15
|
+
|
16
|
+
Results
|
17
|
+
-------
|
18
|
+
|
19
|
+
The results method will return an array of objects. See lib/example.rb
|
20
|
+
|
21
|
+
JSON
|
22
|
+
----
|
23
|
+
|
24
|
+
Use search.to_json to return a JSON representation of the data.
|
25
|
+
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'typhoeus'
|
2
|
+
require 'nori'
|
3
|
+
require 'hashie'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
class DbpediaConceptSearch
|
7
|
+
attr_reader :query_class, :query_string
|
8
|
+
|
9
|
+
def initialize(query_class, query_string)
|
10
|
+
@query_class = query_class
|
11
|
+
@query_string = query_string
|
12
|
+
end
|
13
|
+
|
14
|
+
def dbpedia_url
|
15
|
+
%Q{http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=#{query_class}&QueryString=#{cleaned_query_string}}
|
16
|
+
end
|
17
|
+
|
18
|
+
def cleaned_query_string
|
19
|
+
query_string.gsub(/\s/, '%20')
|
20
|
+
end
|
21
|
+
|
22
|
+
def xml
|
23
|
+
@xml ||= response.body
|
24
|
+
end
|
25
|
+
|
26
|
+
def response
|
27
|
+
@response ||= Typhoeus::Request.get(dbpedia_url)
|
28
|
+
end
|
29
|
+
|
30
|
+
def results
|
31
|
+
hash_results_array.compact.collect{|result| Hashie::Mash.new(processed_hash(result))}
|
32
|
+
end
|
33
|
+
|
34
|
+
def processed_hash(hash)
|
35
|
+
hash = infanticide(hash)
|
36
|
+
hash = downcase_hash(hash)
|
37
|
+
end
|
38
|
+
|
39
|
+
def downcase_hash(hash)
|
40
|
+
if hash.kind_of? Hash
|
41
|
+
hash.each_with_object({}) do |(k, v), h|
|
42
|
+
h[k.downcase] = downcase_hash(v)
|
43
|
+
h.delete(k) unless k == k.downcase
|
44
|
+
end
|
45
|
+
elsif hash.kind_of? Array
|
46
|
+
hash.collect do |h|
|
47
|
+
downcase_hash(h)
|
48
|
+
end
|
49
|
+
else
|
50
|
+
return hash
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def infanticide(hash)
|
55
|
+
if hash.kind_of?(Hash)
|
56
|
+
hash = collapse_singular_child_into_plural_parent(hash)
|
57
|
+
hash.each_with_object({}) do |(k, v), h|
|
58
|
+
h[k] = infanticide(v)
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
if hash.kind_of?(Array)
|
63
|
+
hash.collect do |h|
|
64
|
+
infanticide(h)
|
65
|
+
end
|
66
|
+
else
|
67
|
+
return hash
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def hash_from_xml
|
72
|
+
@hash ||= Nori.parse(xml)
|
73
|
+
end
|
74
|
+
|
75
|
+
def to_json
|
76
|
+
processed_hash(hash_results_array.compact).to_json
|
77
|
+
end
|
78
|
+
|
79
|
+
def hash_results
|
80
|
+
hash_from_xml['ArrayOfResult']['Result']
|
81
|
+
end
|
82
|
+
|
83
|
+
def hash_results_array
|
84
|
+
hash_results.kind_of?(Array) ? hash_results : [hash_results]
|
85
|
+
end
|
86
|
+
|
87
|
+
def collapse_singular_child_into_plural_parent(hash)
|
88
|
+
parents = Hash.new
|
89
|
+
hash.each do |key, value|
|
90
|
+
if value.kind_of?(Hash) and Regexp.new("^#{value.keys.first.gsub(/y\s*$/, 'i')}(s|es)") =~ key
|
91
|
+
parents[key] = value.values.first
|
92
|
+
end
|
93
|
+
end
|
94
|
+
hash.merge(parents)
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
data/lib/example.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative 'dbpedia_concept_search'
|
2
|
+
|
3
|
+
search = DbpediaConceptSearch.new('person', 'Yukihiro Matsumoto ')
|
4
|
+
|
5
|
+
puts "\nLabels for each result:"
|
6
|
+
puts "======================="
|
7
|
+
search.results.each{|result| puts "\t#{result.label}"}
|
8
|
+
|
9
|
+
|
10
|
+
puts "\nDetails of first result"
|
11
|
+
puts "======================="
|
12
|
+
|
13
|
+
one = search.results.first
|
14
|
+
puts " Label = #{one.label}"
|
15
|
+
puts one.description
|
16
|
+
puts " URI = #{one.uri}"
|
17
|
+
|
18
|
+
puts " With Classes"
|
19
|
+
one.classes.each do |klass|
|
20
|
+
puts " URI = #{klass.uri}"
|
21
|
+
end
|
22
|
+
|
23
|
+
puts "\ and Caterogies"
|
24
|
+
one.categories.each do |category|
|
25
|
+
puts " #{category.label}"
|
26
|
+
end
|
27
|
+
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dbpedia_concept_search
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rob Nichols
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-16 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: hashie
|
16
|
+
requirement: &12501460 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.2.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *12501460
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: json
|
27
|
+
requirement: &12501020 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *12501020
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: nori
|
38
|
+
requirement: &12500300 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *12500300
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: typhoeus
|
49
|
+
requirement: &12499800 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *12499800
|
58
|
+
description: A tool that queries dbpedia.org's lookup tool to find concepts, and then
|
59
|
+
bundles the results into either ruby objects (using Hashie) or json.
|
60
|
+
email: rob@undervale.co.uk
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- README.md
|
66
|
+
- LICENSE
|
67
|
+
- lib/dbpedia_concept_search.rb
|
68
|
+
- lib/example.rb
|
69
|
+
homepage: https://github.com/reggieb/DbpediaConceptSearch
|
70
|
+
licenses: []
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.8.10
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: Dbpedia Concept Search queries dbpedia.org's lookup tool to find concepts
|
93
|
+
test_files: []
|