dbpedia_concept_search 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,11 +4,23 @@ DbpediaConceptSearch
4
4
  A tool that queries dbpedia.org's lookup tool to find concepts, and then
5
5
  bundles the results into either ruby objects (using Hashie) or json.
6
6
 
7
+ Installation
8
+ ------------
9
+
10
+ gem install 'dbpedia_concept_search'
11
+
12
+ or if using bundler, add this to your Gemfile:
13
+
14
+ gem 'dbpedia_concept_search'
15
+
16
+
7
17
  Usage
8
18
  -----
9
19
 
10
20
  The initial search is defined by creating a new DbpediaConceptSearch:
11
21
 
22
+ require 'dbpedia_concept_search'
23
+
12
24
  search = DbpediaConceptSearch.new('place', 'London')
13
25
 
14
26
  This will lead to a query matching [this](http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=place&QueryString=London)
@@ -5,37 +5,37 @@ require 'json'
5
5
 
6
6
  class DbpediaConceptSearch
7
7
  attr_reader :query_class, :query_string
8
-
8
+
9
9
  def initialize(query_class, query_string)
10
10
  @query_class = query_class
11
11
  @query_string = query_string
12
12
  end
13
-
13
+
14
14
  def dbpedia_url
15
15
  %Q{http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=#{query_class}&QueryString=#{cleaned_query_string}}
16
16
  end
17
-
17
+
18
18
  def cleaned_query_string
19
19
  query_string.gsub(/\s/, '%20')
20
20
  end
21
-
21
+
22
22
  def xml
23
23
  @xml ||= response.body
24
24
  end
25
-
25
+
26
26
  def response
27
27
  @response ||= Typhoeus::Request.get(dbpedia_url)
28
28
  end
29
-
29
+
30
30
  def results
31
31
  hash_results_array.compact.collect{|result| Hashie::Mash.new(processed_hash(result))}
32
32
  end
33
-
33
+
34
34
  def processed_hash(hash)
35
35
  hash = infanticide(hash)
36
36
  hash = downcase_hash(hash)
37
37
  end
38
-
38
+
39
39
  def downcase_hash(hash)
40
40
  if hash.kind_of? Hash
41
41
  hash.each_with_object({}) do |(k, v), h|
@@ -43,21 +43,21 @@ class DbpediaConceptSearch
43
43
  h.delete(k) unless k == k.downcase
44
44
  end
45
45
  elsif hash.kind_of? Array
46
- hash.collect do |h|
46
+ hash.collect do |h|
47
47
  downcase_hash(h)
48
48
  end
49
49
  else
50
50
  return hash
51
51
  end
52
52
  end
53
-
53
+
54
54
  def infanticide(hash)
55
55
  if hash.kind_of?(Hash)
56
56
  hash = collapse_singular_child_into_plural_parent(hash)
57
57
  hash.each_with_object({}) do |(k, v), h|
58
58
  h[k] = infanticide(v)
59
59
  end
60
-
60
+
61
61
  end
62
62
  if hash.kind_of?(Array)
63
63
  hash.collect do |h|
@@ -67,31 +67,31 @@ class DbpediaConceptSearch
67
67
  return hash
68
68
  end
69
69
  end
70
-
70
+
71
71
  def hash_from_xml
72
72
  @hash ||= Nori.parse(xml)
73
73
  end
74
-
74
+
75
75
  def to_json
76
76
  processed_hash(hash_results_array.compact).to_json
77
77
  end
78
-
78
+
79
79
  def hash_results
80
- hash_from_xml['ArrayOfResult']['Result']
80
+ hash_from_xml['ArrayOfResult']['Result'] if hash_from_xml['ArrayOfResult']
81
81
  end
82
-
82
+
83
83
  def hash_results_array
84
84
  hash_results.kind_of?(Array) ? hash_results : [hash_results]
85
85
  end
86
-
86
+
87
87
  def collapse_singular_child_into_plural_parent(hash)
88
88
  parents = Hash.new
89
- hash.each do |key, value|
89
+ hash.each do |key, value|
90
90
  if value.kind_of?(Hash) and Regexp.new("^#{value.keys.first.gsub(/y\s*$/, 'i')}(s|es)") =~ key
91
91
  parents[key] = value.values.first
92
92
  end
93
93
  end
94
94
  hash.merge(parents)
95
95
  end
96
-
96
+
97
97
  end
@@ -2,6 +2,8 @@ require_relative 'dbpedia_concept_search'
2
2
 
3
3
  search = DbpediaConceptSearch.new('person', 'Yukihiro Matsumoto ')
4
4
 
5
+ raise "Search failed" if search.results.empty?
6
+
5
7
  puts "\nLabels for each result:"
6
8
  puts "======================="
7
9
  search.results.each{|result| puts "\t#{result.label}"}
@@ -20,7 +22,7 @@ one.classes.each do |klass|
20
22
  puts " URI = #{klass.uri}"
21
23
  end
22
24
 
23
- puts "\ and Caterogies"
25
+ puts " and Caterogies"
24
26
  one.categories.each do |category|
25
27
  puts " #{category.label}"
26
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dbpedia_concept_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-08-16 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hashie
16
- requirement: &12501460 !ruby/object:Gem::Requirement
16
+ requirement: &13869640 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.2.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *12501460
24
+ version_requirements: *13869640
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: json
27
- requirement: &12501020 !ruby/object:Gem::Requirement
27
+ requirement: &13869060 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *12501020
35
+ version_requirements: *13869060
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: nori
38
- requirement: &12500300 !ruby/object:Gem::Requirement
38
+ requirement: &13868460 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *12500300
46
+ version_requirements: *13868460
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: typhoeus
49
- requirement: &12499800 !ruby/object:Gem::Requirement
49
+ requirement: &13867900 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *12499800
57
+ version_requirements: *13867900
58
58
  description: A tool that queries dbpedia.org's lookup tool to find concepts, and then
59
59
  bundles the results into either ruby objects (using Hashie) or json.
60
60
  email: rob@undervale.co.uk