Skip to content
This repository has been archived by the owner on Jul 29, 2021. It is now read-only.

Commit

Permalink
Added support for flipping from Freebase pages to dbpedialite.
Browse files Browse the repository at this point in the history
  • Loading branch information
njh committed May 6, 2012
1 parent 08d6cb9 commit 677c8fb
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 13 deletions.
7 changes: 7 additions & 0 deletions dbpedialite.rb
Expand Up @@ -219,6 +219,13 @@ def format_iso8061(datetime)
redirect_from_title($2)
elsif params[:url] =~ %r{^http://dbpedia.org/(page|resource|data)/(.+)$}
redirect_from_title($2)
elsif params[:url] =~ %r{^http://www.freebase.com/(view|inspect|edit/topic)(/.+)$}
begin
data = FreebaseApi.lookup_by_id($2)
redirect "/things/#{data['key']['value']}", 301
rescue FreebaseApi::NotFound
not_found
end
elsif params[:url] =~ %r{^http://([\w\.\-\:]+)/(things|categories)/(\d+)$}
begin
data = WikipediaApi.page_info(:pageids => $3)
Expand Down
34 changes: 27 additions & 7 deletions lib/freebase_api.rb
Expand Up @@ -7,6 +7,12 @@ module FreebaseApi
RDF_BASE_URI = URI.parse('http://rdf.freebase.com/rdf/')
HTTP_TIMEOUT = 2

class Exception < Exception
end

class NotFound < FreebaseApi::Exception
end

def self.mqlread(query)
uri = MQLREAD_URI.clone
uri.query = 'query='+CGI::escape(JSON.dump({'query' => query}))
Expand All @@ -22,14 +28,18 @@ def self.mqlread(query)
# Throw and exception if the JSON response was unsuccessful
data = JSON.parse(res.body)
unless data['code'] == '/api/status/ok'
raise "Freebase query failed: #{data['messages'][0]['message']}"
raise FreebaseApi::Exception.new("Freebase query failed: #{data['messages'][0]['message']}")
end

if data['result'].nil?
raise FreebaseApi::NotFound.new("Freebase query failed return no results")
end

data['result']
end

def self.lookup_wikipedia_pageid(pageid, language='en')
data = mqlread({
mqlread({
'key' => {
'namespace' => "/wikipedia/#{language}_id",
'value' => pageid.to_s,
Expand All @@ -41,11 +51,21 @@ def self.lookup_wikipedia_pageid(pageid, language='en')
'guid' => nil,
'limit' => 1
})
end

if data.nil? or data['mid'].nil?
raise "Failed to lookup wikipedia page id"
end

data
def self.lookup_by_id(identifier, language='en')
mqlread({
'id' => identifier,
'key' => {
'namespace' => "/wikipedia/#{language}_id",
'value' => nil,
'limit' => 1
},
'name' => nil,
'mid' => nil,
'guid' => nil,
'limit' => 1
})
end

end
24 changes: 22 additions & 2 deletions spec/dbpedialite_spec.rb
Expand Up @@ -237,7 +237,7 @@ def app
)
FakeWeb.register_uri(
:get, %r[http://www.freebase.com/api/service/mqlread],
:body => fixture_data('freebase-mqlread-ceres.json'),
:body => fixture_data('freebase-mqlread-934787.json'),
:content_type => 'application/json'
)
end
Expand Down Expand Up @@ -340,7 +340,7 @@ def app
)
FakeWeb.register_uri(
:get, %r[http://www.freebase.com/api/service/mqlread],
:body => fixture_data('freebase-mqlread-ceres.json'),
:body => fixture_data('freebase-mqlread-934787.json'),
:content_type => 'application/json'
)
header "Accept", "text/plain"
Expand Down Expand Up @@ -692,6 +692,26 @@ def app
end
end

context "flipping from a Freebase page" do
before :each do
FakeWeb.register_uri(
:get, %r[http://www.freebase.com/api/service/mqlread],
:body => fixture_data('freebase-mqlread-en-new-york.json'),
:content_type => 'application/json'
)
get '/flipr?url=http%3A%2F%2Fwww.freebase.com%2Fview%2Fen%2Fnew_york'
end

it "should redirect to the coresponding Dbpedia lite thing page" do
last_response.status.should == 301
last_response.location.should == 'http://example.org/things/645042'
end

it "should be cachable" do
last_response.headers['Cache-Control'].should =~ /max-age=([1-9]+)/
end
end

context "flipping from an unknown page" do
before :each do
get '/flipr?url=http%3A%2F%2Fwww.bbc.co.uk%2F'
Expand Down
15 changes: 15 additions & 0 deletions spec/fixtures/freebase-mqlread-en-new-york.json
@@ -0,0 +1,15 @@
{
"code": "/api/status/ok",
"result": {
"guid": "#9202a8c04000641f80000000002f8906",
"id": "/en/new_york",
"key": {
"namespace": "/wikipedia/en_id",
"value": "645042"
},
"mid": "/m/02_286",
"name": "New York City"
},
"status": "200 OK",
"transaction_id": "cache;cache03.p01.sjc1:8101;2012-05-06T21:48:31Z;0052"
}
46 changes: 42 additions & 4 deletions spec/freebase_api_spec.rb
Expand Up @@ -6,12 +6,12 @@
before :each do
FakeWeb.register_uri(:get,
%r[http://www.freebase.com/api/service/mqlread],
:body => fixture_data('freebase-mqlread-ceres.json')
:body => fixture_data('freebase-mqlread-934787.json')
)
@data = FreebaseApi.lookup_wikipedia_pageid(934787)
end

it "should not return a Hash" do
it "should return a Hash" do
@data.should be_a(Hash)
end

Expand All @@ -32,6 +32,44 @@
end
end

context "looking up the Wikipedia Pageid for a Freebase identifier" do
before :each do
FakeWeb.register_uri(:get,
%r[http://www.freebase.com/api/service/mqlread],
:body => fixture_data('freebase-mqlread-en-new-york.json')
)
@data = FreebaseApi.lookup_by_id('/en/new_york')
end

it "should return a Hash" do
@data.should be_a(Hash)
end

it "should return the name of the topic" do
@data['name'].should == 'New York City'
end

it "should return the GUID for the topic" do
@data['guid'].should == '#9202a8c04000641f80000000002f8906'
end

it "should return the Machine ID for the topic" do
@data['mid'].should == '/m/02_286'
end

it "should return the Freebase ID for the topic" do
@data['id'].should == '/en/new_york'
end

it "should have a key for the English Wikipedia page id" do
@data['key']['namespace'].should == '/wikipedia/en_id'
end

it "should return the Wikipedia page id" do
@data['key']['value'].should == '645042'
end
end

context "lookup up a Wikipedia pageid that doesn't exist in Freebase" do
before :each do
FakeWeb.register_uri(:get,
Expand All @@ -43,7 +81,7 @@
it "should throw an exception" do
lambda do
FreebaseApi.lookup_wikipedia_pageid(4309010)
end.should raise_error(RuntimeError, 'Failed to lookup wikipedia page id')
end.should raise_error(FreebaseApi::NotFound, 'Freebase query failed return no results')
end
end

Expand All @@ -58,7 +96,7 @@
it "should throw an exception" do
lambda do
FreebaseApi.mqlread(:foo => :bar)
end.should raise_error(RuntimeError, 'Freebase query failed: Type /type/object does not have property foo')
end.should raise_error(FreebaseApi::Exception, 'Freebase query failed: Type /type/object does not have property foo')
end
end

Expand Down

0 comments on commit 677c8fb

Please sign in to comment.