Class: DbpediaConceptSearch
- Inherits:
-
Object
- Object
- DbpediaConceptSearch
- Defined in:
- lib/dbpedia_concept_search.rb
Instance Attribute Summary collapse
-
#query_class ⇒ Object
readonly
Returns the value of attribute query_class.
-
#query_string ⇒ Object
readonly
Returns the value of attribute query_string.
Class Method Summary collapse
Instance Method Summary collapse
- #cleaned_query_string ⇒ Object
- #collapse_singular_child_into_plural_parent(hash) ⇒ Object
- #dbpedia_url ⇒ Object
- #downcase_hash(hash) ⇒ Object
- #hash_from_xml ⇒ Object
- #hash_results ⇒ Object
- #hash_results_array ⇒ Object
- #infanticide(hash) ⇒ Object
-
#initialize(query_class, query_string) ⇒ DbpediaConceptSearch
constructor
A new instance of DbpediaConceptSearch.
- #processed_hash(hash) ⇒ Object
- #response ⇒ Object
- #results ⇒ Object
- #to_json ⇒ Object
- #xml ⇒ Object
Constructor Details
#initialize(query_class, query_string) ⇒ DbpediaConceptSearch
Returns a new instance of DbpediaConceptSearch.
17 18 19 20 |
# File 'lib/dbpedia_concept_search.rb', line 17 def initialize(query_class, query_string) @query_class = query_class @query_string = query_string end |
Instance Attribute Details
#query_class ⇒ Object (readonly)
Returns the value of attribute query_class.
7 8 9 |
# File 'lib/dbpedia_concept_search.rb', line 7 def query_class @query_class end |
#query_string ⇒ Object (readonly)
Returns the value of attribute query_string.
7 8 9 |
# File 'lib/dbpedia_concept_search.rb', line 7 def query_string @query_string end |
Class Method Details
.hostname ⇒ Object
9 10 11 |
# File 'lib/dbpedia_concept_search.rb', line 9 def self.hostname @hostname ||= 'lookup.dbpedia.org' end |
.hostname=(hostname) ⇒ Object
13 14 15 |
# File 'lib/dbpedia_concept_search.rb', line 13 def self.hostname=(hostname) @hostname = hostname end |
Instance Method Details
#cleaned_query_string ⇒ Object
26 27 28 |
# File 'lib/dbpedia_concept_search.rb', line 26 def cleaned_query_string query_string.gsub(/\s/, '%20') end |
#collapse_singular_child_into_plural_parent(hash) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/dbpedia_concept_search.rb', line 95 def collapse_singular_child_into_plural_parent(hash) parents = Hash.new hash.each do |key, value| if value.kind_of?(Hash) and Regexp.new("^#{value.keys.first.gsub(/y\s*$/, 'i')}(s|es)") =~ key parents[key] = value.values.first end end hash.merge(parents) end |
#dbpedia_url ⇒ Object
22 23 24 |
# File 'lib/dbpedia_concept_search.rb', line 22 def dbpedia_url %Q{http://#{DbpediaConceptSearch.hostname}/api/search.asmx/KeywordSearch?QueryClass=#{query_class}&QueryString=#{cleaned_query_string}} end |
#downcase_hash(hash) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dbpedia_concept_search.rb', line 47 def downcase_hash(hash) if hash.kind_of? Hash hash.each_with_object({}) do |(k, v), h| h[k.downcase] = downcase_hash(v) h.delete(k) unless k == k.downcase end elsif hash.kind_of? Array hash.collect do |h| downcase_hash(h) end else return hash end end |
#hash_from_xml ⇒ Object
79 80 81 |
# File 'lib/dbpedia_concept_search.rb', line 79 def hash_from_xml @hash ||= Nori.new.parse(xml) end |
#hash_results ⇒ Object
87 88 89 |
# File 'lib/dbpedia_concept_search.rb', line 87 def hash_results hash_from_xml['ArrayOfResult']['Result'] if hash_from_xml['ArrayOfResult'] end |
#hash_results_array ⇒ Object
91 92 93 |
# File 'lib/dbpedia_concept_search.rb', line 91 def hash_results_array hash_results.kind_of?(Array) ? hash_results : [hash_results] end |
#infanticide(hash) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/dbpedia_concept_search.rb', line 62 def infanticide(hash) if hash.kind_of?(Hash) hash = collapse_singular_child_into_plural_parent(hash) hash.each_with_object({}) do |(k, v), h| h[k] = infanticide(v) end end if hash.kind_of?(Array) hash.collect do |h| infanticide(h) end else return hash end end |
#processed_hash(hash) ⇒ Object
42 43 44 45 |
# File 'lib/dbpedia_concept_search.rb', line 42 def processed_hash(hash) hash = infanticide(hash) hash = downcase_hash(hash) end |
#response ⇒ Object
34 35 36 |
# File 'lib/dbpedia_concept_search.rb', line 34 def response @response ||= Typhoeus::Request.get(dbpedia_url) end |
#results ⇒ Object
38 39 40 |
# File 'lib/dbpedia_concept_search.rb', line 38 def results hash_results_array.compact.collect{|result| Hashie::Mash.new(processed_hash(result))} end |
#to_json ⇒ Object
83 84 85 |
# File 'lib/dbpedia_concept_search.rb', line 83 def to_json processed_hash(hash_results_array.compact).to_json end |
#xml ⇒ Object
30 31 32 |
# File 'lib/dbpedia_concept_search.rb', line 30 def xml @xml ||= response.body end |