Class: FreebaseAPI::Topic

Inherits:
Object
  • Object
show all
Defined in:
lib/freebase_api/topic.rb

Constant Summary collapse

PROPERTIES_LIMIT =

The maximum number of property values to return.

100

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, options = {}) ⇒ Topic

Returns a new instance of Topic.



69
70
71
72
73
74
75
# File 'lib/freebase_api/topic.rb', line 69

def initialize(id, options={})
  @properties = {}
  @excluded_properties = parse_exclusion(options.delete(:exclude))
  @options = { :filter => 'commons' }.merge(options)
  @data = { 'id' => id }.merge(options[:data] || {})
  build
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



7
8
9
# File 'lib/freebase_api/topic.rb', line 7

def properties
  @properties
end

Class Method Details

.get(id, options = {}) ⇒ Topic

Returns a new Topic filled with all the Freebase properties

Parameters:

  • id (String)

    the Freebase ID

  • options (Hash) (defaults to: {})

    the options

Returns:



15
16
17
18
19
# File 'lib/freebase_api/topic.rb', line 15

def get(id, options={})
  topic = Topic.new(id, options)
  topic.sync
  topic
end

.search(query, options = {}) ⇒ Hash

Search using a query and returns the results as a hash of topics score based

Parameters:

  • query (String)

    the string query

  • options (Hash) (defaults to: {})

    the options

Returns:

  • (Hash)

    the topics



26
27
28
29
30
31
32
# File 'lib/freebase_api/topic.rb', line 26

def search(query, options={})
  hash = {}
  FreebaseAPI.session.search(query, options).each do |topic|
    hash[topic['score'].to_f] = Topic.new(topic['mid'], :data => construct_data(topic))
  end
  hash
end

Instance Method Details

#descriptionObject



97
98
99
# File 'lib/freebase_api/topic.rb', line 97

def description
  @description ||= extract_description
end

#idObject



77
78
79
# File 'lib/freebase_api/topic.rb', line 77

def id
  @data['id']
end

#image(options = {}) ⇒ Object



120
121
122
# File 'lib/freebase_api/topic.rb', line 120

def image(options={})
  FreebaseAPI::Image.get(self.id, options)
end

#inspectObject



124
125
126
# File 'lib/freebase_api/topic.rb', line 124

def inspect
  "#<#{self.class}:0x#{self.__id__.to_s(16)} id: \"#{self.id}\", name: \"#{self.name}\">"
end

#langObject



85
86
87
# File 'lib/freebase_api/topic.rb', line 85

def lang
  @data['lang']
end

#nameObject



89
90
91
# File 'lib/freebase_api/topic.rb', line 89

def name
  @name ||= extract_name
end

#properties_domainsObject



105
106
107
108
109
110
111
112
113
# File 'lib/freebase_api/topic.rb', line 105

def properties_domains
  domains = {}
  properties.keys.each do |prop|
    d = prop.split('/')[1]
    domains[d] ||= 0
    domains[d] += properties[prop].size
  end
  domains
end

#property(name) ⇒ Object



101
102
103
# File 'lib/freebase_api/topic.rb', line 101

def property(name)
  @properties[name]
end

#syncObject



115
116
117
118
# File 'lib/freebase_api/topic.rb', line 115

def sync
  @data = FreebaseAPI.session.topic(self.id, @options)
  build
end

#textObject



81
82
83
# File 'lib/freebase_api/topic.rb', line 81

def text
  @data['text']
end

#typesObject



93
94
95
# File 'lib/freebase_api/topic.rb', line 93

def types
  @types ||= extract_types
end