Created
November 18, 2009 02:33
-
-
Save kitplummer/237504 to your computer and use it in GitHub Desktop.
OpenCalais API from Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'calais' | |
require 'rdf/redland' | |
require 'restclient' | |
require 'rexml/document' | |
require 'cgi' | |
include REXML | |
# Simple module to manage the MediaWiki interface | |
module Wiki | |
class Connection | |
@@token = nil | |
@@name = nil | |
@@id = nil | |
@@session = nil | |
@@prefix = nil | |
@@cookies = nil | |
def login | |
xml = RestClient.post 'http://semwiki/mediawiki/api.php', | |
:action => "login", | |
:lgname => "rubot", | |
:lgpassword => "test", | |
:format => "xml" | |
doc = Document.new xml | |
@@token = doc.root.elements["login"].attributes["lgtoken"] | |
@@name = doc.root.elements["login"].attributes["lgusername"] | |
@@id = doc.root.elements["login"].attributes["lguserid"] | |
@@session = doc.root.elements["login"].attributes["sessionid"] | |
@@prefix = doc.root.elements["login"].attributes["cookieprefix"] | |
puts xml | |
@@cookies = {"#{@@prefix}UserName" => @@name, | |
"#{@@prefix}UserId" => @@id, | |
"#{@@prefix}Token" => @@token, | |
"#{@@prefix}_session" => @@session} | |
end | |
def edit(page, section, text) | |
puts "TOKEN: #{@@token}" | |
title = page.to_s.scan(/\w+/).to_s | |
puts "TITLE: #{title}" | |
# get edit token | |
query_xml = RestClient.post('http://semwiki/mediawiki/api.php', | |
{:action => 'query', | |
:prop => 'info', | |
:intoken => 'edit', | |
:titles => title, | |
:format => 'xml'}, | |
{:content_type => 'application/x-www-form-urlencoded', | |
:cookies => @@cookies}) | |
puts query_xml | |
doc = Document.new query_xml | |
etoken = doc.root.root.elements["query"].elements["pages"].elements["page"].attributes["edittoken"] | |
puts "etoken: #{etoken}" | |
edit_xml = RestClient.post('http://semwiki/mediawiki/api.php', | |
{:createonly => true, | |
:token => etoken, | |
:action => "edit", | |
:title => title, | |
:section => 'new', | |
:summary => section, | |
:text => text, | |
:format => 'xml'}, | |
{:content_type => 'application/x-www-form-urlencoded', | |
:cookies => {@@cookies}) | |
puts edit_xml | |
end | |
end | |
end | |
puts "** OpenCalais to Semantic MediaWiki Demo **" | |
puts "#{DateTime.now} Calais.englighten() - Fetching response from OpenCalais..." | |
rdf = Calais.enlighten( | |
:content => "The government of the United Kingdom has given corporations like fast food chain McDonald's the right to award high school qualifications to employees who complete a company training program.", | |
:content_type => :text, | |
:license_id => '<CALAIS API KEY>' | |
) | |
puts "#{DateTime.now} Redland::TripleStore.new() - Setting up RDF processor..." | |
storage = Redland::TripleStore.new("hashes", "test", "new='yes',hash-type='bdb',dir='.'") | |
raise "Failed to create RDF storage" if !storage | |
puts "#{DateTime.now} Redland::Model.new() - Setting up RDF modeler..." | |
model = Redland::Model.new(storage) | |
if !model then | |
raise "Failed to create RDF model" | |
end | |
puts "#{DateTime.now} Redland::Parser.new() - Setting up RDF parser..." | |
parser = Redland::Parser.new("rdfxml", "", nil) | |
if !parser then | |
raise "Failed to create RDF parser" | |
end | |
puts "#{DateTime.now} Redland::Parser.parse() - Parsing OpenCalais response..." | |
parser.parse_string_into_model(model, | |
rdf, | |
Redland::Uri.new('http://www.w3.org/1999/02/22-rdf-syntax-ns#') | |
) | |
puts "** RDF Model Query for ns0:name **" | |
country = Redland::Query.new(" PREFIX ns0: <http://s.opencalais.com/1/pred/> SELECT ?a ?c WHERE { ?a ns0:name ?c } ") | |
results=country.execute(model) | |
while !results.finished?() | |
for k in 0..results.bindings_count()-1 | |
if (results.binding_value(k).to_s.include?("http://d.opencalais.com/comphash-1")) | |
@company = results.binding_value(k+1) | |
end | |
end | |
results.next() | |
end | |
puts "** RDF Model Query for ns0:latitude **" | |
q = Redland::Query.new(" PREFIX ns0: <http://s.opencalais.com/1/pred/> SELECT ?a ?c WHERE { ?a ns0:latitude ?c } ") | |
results=q.execute(model) | |
while !results.finished?() | |
@lat = results.binding_value(1) | |
results.next() | |
end | |
puts "** RDF Model Query for ns0:longitude **" | |
q2 = Redland::Query.new(" PREFIX ns0: <http://s.opencalais.com/1/pred/> SELECT ?a ?c WHERE { ?a ns0:longitude ?c } ") | |
results=q2.execute(model) | |
while !results.finished?() | |
@lng = results.binding_value(1) | |
results.next() | |
end | |
puts " Company: #{@company}" | |
puts " Latitude: #{@lat}" | |
puts " Longitude: #{@lng}" | |
puts "#{DateTime.now} Logging into SemWiki..." | |
wiki = Wiki::Connection.new | |
wiki.login | |
wiki.edit(@company, "Geo", "#{@lat}, #{@lng}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment