Install
gem sources --add http://webget.com gem install webget_ruby_freebase
Example
require 'rubygems'
require 'freebase'
require 'pp'
query = '{"id":"/en/china","capital":null,"type":"/location/country"}'
pp Freebase.read(query)
#=> {"id"=>"/en/china", "type"=>"/location/country", "capital"=>"Beijing"}
pp Freebase.read(query)['capital']
#=> "Beijing"
Example of multiple queries
query1 = '{"id":"/en/india","capital":null,"type":"/location/country"}'
query2 = '{"id":"/en/japan","capital":null,"type":"/location/country"}'
pp Freebase.read(query1,query2)
#=> [{"id"=>"/en/india", "type"=>"/location/country", "capital"=>"New Delhi"},
# {"id"=>"/en/japan", "type"=>"/location/country", "capital"=>"Tokyo"}]
pp Freebase.read(query1,query2).map{|result| result['capital']}
#=> ["New Delhi","Tokyo"]
Name Query
Example
query = Freebase.name_query('Cher')
#=> {"/type/reflect/any_value"=>[{
"lang"=>"/lang/en",
"link|="=>
[
"/type/object/name",
"/common/topic/alias"
],
"type"=>"/type/text",
"value"=>"cher"
}],
"t1:type"=>"/people/person",
"t2:type"=>"/common/topic",
"t3:type"=>{
"name"=>"/people/deceased_person",
"optional"=>"optional"
},
"id"=>"null"
}
Example of finding a person’s id
Freebase.read(Freebase.name_query('Cher'))['id']
#=> "/en/cher"
Query Formats
You can provide a query as a JSON string or as a Hash.
The query methods will convert:
- a Hash to a JSON string e.g. {:foo=>’bar’} to ’{“foo”:“bar”}
- a nil value to the freebase ‘null’ string e.g. {:foo=>nil} to {“foo”:null}
- anything else: we call to_s then to_json