dbpedia 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # DBpedia Client for Ruby
2
+
3
+ ## Features
4
+
5
+ * Search for Dbpedia entries by *keyword* or *prefix* (e.g. autocompleter)
6
+ * Use SPARQL via [Ruby RDFs great *sparql-client*](https://github.com/ruby-rdf/sparql-client) to query Dbpedia
7
+
8
+ ## Installation
9
+
10
+ Simply add it to your Gemfile:
11
+
12
+ gem "dbpedia"
13
+ gem "sparql-client" # if you want to use sparql
14
+
15
+ ## Examples
16
+
17
+ ### Search by *keyword* and *prefix* (notice their different results)
18
+
19
+ # Default search is by keyword:
20
+ Dbpedia.search('Ham').collect(&:label)
21
+ Dbpedia.search('Ham', method: 'keyword').collect(&:label)
22
+ #=> ["West Ham United F.C.", "Ham", "West Ham", "East Ham", "Hamarkameratene"]
23
+
24
+ # To perform a prefix-search (which is usefull for autocompleter) pass `method`:
25
+ Dbpedia.search('Ham', method: 'prefix').collect(&:label)
26
+ #=> ["Hamburg", "Hampshire", "Hamlet (place)", "Hamlet", "Hamilton, Ontario"]
27
+
28
+ ### Fetch search details
29
+
30
+ results = Dbpedia.search('Hamburg')
31
+ puts results.first.label #=> "Hamburg"
32
+ puts results.first.categories.count #=> 15
33
+ puts results.first.categories.first.label #=> Populated places established in the 9th century"
34
+
35
+ #### Possible methods
36
+
37
+ [results] (SearchResult)
38
+ |-- label
39
+ |-- uri
40
+ |-- description
41
+ +-- [categories] (SearchResult::Category)
42
+ | |-- label
43
+ | |-- uri
44
+ +-- [classes] (SearchResult::Klass)
45
+ |-- label
46
+ |-- uri
47
+
48
+ ### SPARQL Queries
49
+
50
+ Marke sure you've installed [*sparql-client*](https://github.com/ruby-rdf/sparql-client)!
51
+
52
+ Dbpedia.sparql.select.whatever
53
+
54
+ ## Milestones
55
+
56
+ * Implement `find` for known resources
57
+ * Gemify
58
+
59
+ ## Specs
60
+
61
+ Run `$ bundle exec rspec -c`
62
+
63
+ ## Gloss
64
+
65
+ Copyright © 2013 [Roman Ernst](http://wanderwort.de), released under [CC BY-SA 3.0](http://creativecommons.org/licenses/by-sa/3.0/) license
66
+
67
+ .. without warranty of any kind!
@@ -0,0 +1,9 @@
1
+ dbpedia:
2
+ uris:
3
+ search:
4
+ keyword: 'http://lookup.dbpedia.org/api/search.asmx/KeywordSearch'
5
+ prefix: 'http://lookup.dbpedia.org/api/search.asmx/PrefixSearch'
6
+ sparql: 'http://dbpedia.org/sparql'
7
+ params:
8
+ query_class: QueryClass
9
+ query_string: QueryString
data/lib/dbpedia.rb ADDED
@@ -0,0 +1,40 @@
1
+ require 'yaml'
2
+
3
+ module Dbpedia
4
+
5
+ File.open(File.expand_path('../../config/dbpedia.yml', __FILE__)) do |file|
6
+ Dbpedia::Config = YAML::load(file)['dbpedia']
7
+ end
8
+
9
+ require 'dbpedia/exception'
10
+ require 'dbpedia/request'
11
+ require 'dbpedia/client'
12
+ require 'dbpedia/result'
13
+ require 'dbpedia/search_result'
14
+
15
+ ###
16
+ # Returns a Dbpedia::Result object for _name_
17
+ def self.find(name, *args)
18
+ client.find(name, *args)
19
+ end
20
+
21
+ ###
22
+ # Returns an array of matching strings for _query_
23
+ def self.search(query, *args)
24
+ client.search(query, *args)
25
+ end
26
+
27
+ ###
28
+ # Access to sparql delegator
29
+ def self.sparql
30
+ client.sparql
31
+ end
32
+
33
+ private
34
+
35
+ ###
36
+ # Initialize a new Dbpedia::Client
37
+ def self.client
38
+ @client ||= Dbpedia::Client.new
39
+ end
40
+ end
@@ -0,0 +1,43 @@
1
+ module Dbpedia
2
+ class Client
3
+
4
+ def initialize
5
+ @uris = Dbpedia::Config['uris']
6
+ end
7
+
8
+ def find(query, opts={})
9
+
10
+ end
11
+
12
+ ###
13
+ # Possible keys for _opts_:
14
+ # * *search_method*: Either *keyword_search* (default) or *prefix_search*
15
+ # * *query_class*: A DBpedia class that defines a search scope
16
+ def search(query, opts={})
17
+ search_method = (opts[:method] || :keyword).to_s
18
+ result = request @uris['search'][search_method] do |params|
19
+ params.query_class = opts[:query_class] if opts.has_key?(:query_class)
20
+ params.query_string = query
21
+ end
22
+ Dbpedia::SearchResult.load_many_from(result)
23
+ end
24
+
25
+ ###
26
+ # Delegate sparql calls to `sparql-client` gem if available
27
+ def sparql
28
+ return @sparql if @sparql
29
+ begin
30
+ require 'sparql/client'
31
+ rescue LoadError
32
+ raise Exception::SparqlLoadError
33
+ end
34
+ @sparql = SPARQL::Client.new(@uris['sparql'])
35
+ end
36
+
37
+ private
38
+
39
+ def request(uri, &block)
40
+ Dbpedia::Request.new(uri, &block)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,22 @@
1
+ module Dbpedia
2
+ module Exception
3
+
4
+ class SparqlLoadError < StandardError
5
+ def message
6
+ """
7
+ You need to to install `sparql-client` in order to use `sparql`.
8
+ In your Gemfile: `gem \"sparql-client\"` and run `$ bundle install`.
9
+ """
10
+ end
11
+ end
12
+
13
+ class NoParserMethod < StandardError
14
+ def message
15
+ """
16
+ You should add a `parse` method to each Dbpedia::Parser class.
17
+ """
18
+ end
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,35 @@
1
+ module Dbpedia
2
+ class Parser
3
+ require 'nokogiri'
4
+ attr_reader :doc
5
+
6
+ ###
7
+ # Create a new SearchResult object for each result
8
+ def self.load_many_from(doc, selector='Result')
9
+ return [] if doc.empty?
10
+ doc = Nokogiri::XML(doc.body) unless doc.is_a? Nokogiri::XML::NodeSet
11
+ doc.css(selector).map do |result|
12
+ self.new(result).parse
13
+ end
14
+ end
15
+
16
+ def initialize(doc)
17
+ @doc = doc
18
+ end
19
+
20
+ def read_xml(attr)
21
+ @doc.css(attr)
22
+ end
23
+
24
+ def read(attr)
25
+ read_xml(attr).inner_text
26
+ end
27
+
28
+ ##
29
+ # Each SearchResult object shall
30
+ # define it's own parsing method
31
+ def parse
32
+ raise Exception::NoParserMethod
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,22 @@
1
+ require 'dbpedia/request/params'
2
+
3
+ module Dbpedia
4
+ class Request
5
+ require 'uri'
6
+ require 'open-uri'
7
+
8
+ attr_accessor :params, :uri, :body
9
+
10
+ def initialize(uri)
11
+ @params = Params.new and (yield(@params, uri) if block_given?)
12
+ @uri = URI.parse(uri)
13
+ @uri.query = URI.encode_www_form(@params.active)
14
+ @uri.tap { |uri| @body = uri.open.read }
15
+ end
16
+
17
+ def empty?
18
+ body.empty?
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ module Dbpedia
2
+ class Request
3
+ class Params
4
+ ###
5
+ # Read available params from config
6
+ # Initialize param setter methods
7
+ Dbpedia::Config['params'].each do |method_name, param_name|
8
+ define_method "#{method_name}=" do |value|
9
+ @active[param_name] = value
10
+ end
11
+ end
12
+
13
+ attr_accessor :active
14
+
15
+ def initialize
16
+ @active = {}
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module Dbpedia
2
+ class Result
3
+
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ require 'dbpedia/parser'
2
+
3
+ module Dbpedia
4
+ class SearchResult < Dbpedia::Parser
5
+ require 'dbpedia/search_result/category'
6
+ require 'dbpedia/search_result/klass'
7
+
8
+ attr_accessor :label, :uri, :description, :label, :categories, :classes
9
+
10
+ def parse
11
+ self.tap do |obj|
12
+ obj.label = read '> Label'
13
+ obj.uri = read '> URI'
14
+ obj.description = read '> Description'
15
+ obj.categories = Category.load_many_from(read_xml('> Categories'), 'Category')
16
+ obj.classes = Klass.load_many_from(read_xml('> Classes'), 'Class')
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ module Dbpedia
2
+ class SearchResult
3
+ class Category < Dbpedia::Parser
4
+
5
+ attr_accessor :label, :uri
6
+
7
+ def parse
8
+ self.tap do |obj|
9
+ obj.label = read '> Label'
10
+ obj.uri = read '> URI'
11
+ end
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module Dbpedia
2
+ class SearchResult
3
+ class Klass < Dbpedia::Parser
4
+
5
+ attr_accessor :label, :uri
6
+
7
+ def parse
8
+ self.tap do |obj|
9
+ obj.label = read '> Label'
10
+ obj.uri = read '> URI'
11
+ end
12
+ end
13
+
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dbpedia
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Roman Ernst
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.5.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.5.5
30
+ description: Simple search for DBpedia resources. Optional support for sparql.
31
+ email: rernst@farbenmeer.bet
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - README.md
37
+ - lib/dbpedia/client.rb
38
+ - lib/dbpedia/exception.rb
39
+ - lib/dbpedia/parser.rb
40
+ - lib/dbpedia/request/params.rb
41
+ - lib/dbpedia/request.rb
42
+ - lib/dbpedia/result.rb
43
+ - lib/dbpedia/search_result/category.rb
44
+ - lib/dbpedia/search_result/klass.rb
45
+ - lib/dbpedia/search_result.rb
46
+ - lib/dbpedia.rb
47
+ - config/dbpedia.yml
48
+ homepage: https://github.com/farbenmeer/dbpedia
49
+ licenses:
50
+ - CC BY-SA 3.0
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: 1.9.2
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.23
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: DBpedia Client for Ruby
73
+ test_files: []
74
+ has_rdoc: