Module: Similus

Defined in:
lib/similus/core.rb,
lib/similus/redis.rb,
lib/similus/config.rb

Defined Under Namespace

Classes: Config

Class Method Summary collapse

Class Method Details

.add_activity(a, action, b) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/similus/core.rb', line 5

def add_activity(a, action, b)
  # Find or create objects
  a = add_object(a)
  b = add_object(b)

  # Find or create action
  action = add_action({:name => action})

  # Create activities (both sides)
  create_activities(a, action, b)
  create_activities(b, action, a)
end

.clear_database!Object



12
13
14
15
# File 'lib/similus/redis.rb', line 12

def self.clear_database!
  @cache = {}
  redis.flushdb
end

.configObject



2
3
4
5
# File 'lib/similus/config.rb', line 2

def self.config
  @config ||= Config.new
  block_given? ? yield(@config) : @config
end


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/similus/core.rb', line 39

def recommended_for(src, options={}, &block)
  default_options = { :load_objects => true, :max_similar => 10,
                      :limit => 10, :offset => 0, :method => :similarity,
                      :source => :all, :target => :all }

  ropt = default_options.update(options)
  sopt = options.merge(:load_objects => false)
  maxs = ropt.delete(:max_similar)

  # Find objects for user
  src_act = load_activity(src, sopt)

  # Get similar objects, sort and get first N elements
  src_sim = similar_to(src, sopt, src_act).sort {|x,y| y[1] <=> x[1]}[0..maxs]

  # Get recommended score of similar objects's activity
  result = src_sim.inject(empty_hash) do |res, sim|
    dst_act = redis.smembers("activity:#{sim[0]}")
    rscore(ropt[:method], res, dst_act, src_act, sim[1], sim[0], &block)
  end

  # Remove already seen, sort and apply limit/offset
  result.reject! { |key,value| src_act.include?(key) }
  result = result.sort { |x,y| y[1] <=> x[1] }[ropt[:offset],ropt[:limit]]

  # Load original objects
  ropt[:load_objects] ? load_objects(result) : result
end

.redisObject



2
3
4
5
6
7
8
9
10
# File 'lib/similus/redis.rb', line 2

def self.redis
  @redis ||= begin
    host, port = config.redis_server.split(':')
    ::Redis.new(:host => host, :port => port, :db => config.redis_db)
  rescue Exception => e
    config.logger.error "Error connecting redis server: #{e.message}"
    nil
  end
end

.similar_to(src, options = {}, objects = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/similus/core.rb', line 18

def similar_to(src, options={}, objects=nil)
  sopt = {:source => :all, :load_objects => true}.update(options)
  src = add_object(src)

  objects ||= load_activity(src, options)

  similar_objects = objects.inject(empty_hash) do |result, id|
    redis.smembers("activity:#{id}").each { |similar| result[similar] += 1 }
    result
  end

  similar_objects.delete(src[:obj_id])      # delete object itself form list
  similar_objects.reject! { |k,v| v == 1 }   # remove similars just by one

  if sopt[:load_objects] 
    load_objects(similar_objects.sort { |x,y| y[1] <=> x[1] })
  else
    similar_objects
  end
end