class SemanticCrawler::Freebase::Country
Freebase
Country
entity. Currently it is very abstract and could be each entry on Freebase
.
Attributes
@attribute [r] The read only country name
Public Class Methods
Creates a new Freebase
object (JSON)
# File lib/semantic_crawler/freebase/country.rb, line 24 def initialize(new_country_name) @country_name = new_country_name if !@country_name.nil? @normalized_country = @country_name.gsub(" ", "_").gsub("USA", "United_States_of_America").downcase @url = @@URI_PREFIX + @normalized_country @subject = "http://rdf.freebase.com/ns/en.#{@normalized_country}" @root_node = nil begin fetch_rdf rescue => e $log.error("Not able to get country information, through exception: #{e}") end end end
Public Instance Methods
Extract fb:location.country.administrative_divisions properties @return [Array<String>]
# File lib/semantic_crawler/freebase/country.rb, line 67 def administrative_divisions return_list = [] list = query_root_node("fb:location.country.administrative_divisions/@rdf:resource", @@NAMESPACES) if !list.nil? list.each do |entry| return_list << entry.to_s end end return_list end
Extract fb:government.governmental_jurisdiction.agencies @return [Array<String>] Returns the governmental jurisdiction agencies as URLs
# File lib/semantic_crawler/freebase/country.rb, line 80 def agencies return_list = [] list = query_root_node("fb:government.governmental_jurisdiction.agencies/@rdf:resource", @@NAMESPACES) if !list.nil? list.each do |entry| return_list << entry.to_s end end return_list end
Extract the fb:location.location.contains properties @return [Array<String>]
# File lib/semantic_crawler/freebase/country.rb, line 54 def contains return_list = [] list = query_root_node("fb:location.location.contains/@rdf:resource", @@NAMESPACES) if !list.nil? list.each do |entry| return_list << entry.to_s end end return_list end
Extract the fb:type.object.name property @param [String] The language in ISO 3166-1 alpha-2 format @return [String] The country name
# File lib/semantic_crawler/freebase/country.rb, line 48 def object_name(lang = "en") query_root_node("fb:type.object.name[@xml:lang='#{lang}']/text()", @@NAMESPACES).to_s end
Executes a xpath query with optional a hash with namespaces @return [String]
# File lib/semantic_crawler/freebase/country.rb, line 106 def query_root_node(xpath_query, namespaces = {}) if !@root_node.nil? @root_node.xpath(xpath_query, namespaces) end end
Extract the owl:sameAs links @return [Array<String>] A list of same concept URLs, e.g. to dbpedia or nytimes
# File lib/semantic_crawler/freebase/country.rb, line 93 def same_as return_list = [] list = query_root_node("owl:sameAs/@rdf:resource", @@NAMESPACES) if !list.nil? list.each do |entry| return_list << entry.to_s end end return_list end
Extract the fb:common.topic.topical_webpage property @return [String] Website as String
# File lib/semantic_crawler/freebase/country.rb, line 41 def website query_root_node("fb:common.topic.topical_webpage/text()", @@NAMESPACES).to_s end
Outputs the document as XML @return [String] The document serialized as XML
# File lib/semantic_crawler/freebase/country.rb, line 114 def xml_document @root_node.to_s end
Private Instance Methods
Retrieves the RDF file
# File lib/semantic_crawler/freebase/country.rb, line 120 def fetch_rdf @doc = Nokogiri::XML(open(@url)) @root_node = @doc.xpath("//*[@rdf:about='#{@subject}']", @@NAMESPACES) end