Class: SemanticCrawler::Freebase::Country

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/semantic_crawler/freebase/country.rb

Overview

Freebase Country entity. Currently it is very abstract and could be each entry on Freebase.

Constant Summary collapse

@@URI_PREFIX =

The URL prefix of an english Freebase RDF entity.

"http://rdf.freebase.com/rdf/en."
@@NAMESPACES =

Namespace hash

{
    "rdf" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "fb" => "http://rdf.freebase.com/ns/",
    "owl" => "http://www.w3.org/2002/07/owl#"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_country_name) ⇒ Country

Creates a new Freebase object (JSON)



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

Instance Attribute Details

#country_nameObject (readonly)

The read only country name



21
22
23
# File 'lib/semantic_crawler/freebase/country.rb', line 21

def country_name
  @country_name
end

Instance Method Details

#administrative_divisionsArray<String>

Extract fb:location.country.administrative_divisions properties

Returns:

  • (Array<String>)


67
68
69
70
71
72
73
74
75
76
# 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

#agenciesArray<String>

Extract fb:government.governmental_jurisdiction.agencies

Returns:

  • (Array<String>)

    Returns the governmental jurisdiction agencies as URLs



80
81
82
83
84
85
86
87
88
89
# 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

#containsArray<String>

Extract the fb:location.location.contains properties

Returns:

  • (Array<String>)


54
55
56
57
58
59
60
61
62
63
# 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

#object_name(lang = "en") ⇒ String

Extract the fb:type.object.name property

Parameters:

  • The (String)

    language in ISO 3166-1 alpha-2 format

Returns:

  • (String)

    The country name



48
49
50
# 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

#query_root_node(xpath_query, namespaces = {}) ⇒ String

Executes a xpath query with optional a hash with namespaces

Returns:

  • (String)


106
107
108
109
110
# 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

#same_asArray<String>

Extract the owl:sameAs links

Returns:

  • (Array<String>)

    A list of same concept URLs, e.g. to dbpedia or nytimes



93
94
95
96
97
98
99
100
101
102
# 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

#websiteString

Extract the fb:common.topic.topical_webpage property

Returns:

  • (String)

    Website as String



41
42
43
# File 'lib/semantic_crawler/freebase/country.rb', line 41

def website
    query_root_node("fb:common.topic.topical_webpage/text()", @@NAMESPACES).to_s
end

#xml_documentString

Outputs the document as XML

Returns:

  • (String)

    The document serialized as XML



114
115
116
# File 'lib/semantic_crawler/freebase/country.rb', line 114

def xml_document
    @root_node.to_s
end