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.



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

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



134
135
136
# File 'lib/freebase_api/topic.rb', line 134

def description
  @description ||= extract_description
end

#idObject



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

def id
  @data['id']
end

#image(options = {}) ⇒ Object



157
158
159
# File 'lib/freebase_api/topic.rb', line 157

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

#inspectObject



161
162
163
# File 'lib/freebase_api/topic.rb', line 161

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

#langObject



122
123
124
# File 'lib/freebase_api/topic.rb', line 122

def lang
  @data['lang']
end

#nameObject



126
127
128
# File 'lib/freebase_api/topic.rb', line 126

def name
  @name ||= extract_name
end

#properties_domainsObject



142
143
144
145
146
147
148
149
150
# File 'lib/freebase_api/topic.rb', line 142

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



138
139
140
# File 'lib/freebase_api/topic.rb', line 138

def property(name)
  @properties[name]
end

#syncObject



152
153
154
155
# File 'lib/freebase_api/topic.rb', line 152

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

#textObject



118
119
120
# File 'lib/freebase_api/topic.rb', line 118

def text
  @data['text']
end

#typesObject



130
131
132
# File 'lib/freebase_api/topic.rb', line 130

def types
  @types ||= extract_types
end