Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 1 commit
  • 3 files changed
  • 0 comments
  • 1 contributor
Sep 11, 2012
:game_die: adding tag_suggestions, tag_suggestions_hash and some really shitty…
… specs for covering them
5d7aba3
2  README.md
Source Rendered
@@ -55,6 +55,8 @@ If you'd like to compare yourself to someone else by a distance rather than an a
55 55
 
56 56
 To get the distance between two tags, try `User.tag_distance 'sexy', 'beast'`.
57 57
 
  58
+If you want tag suggestions for a taggable based on what tags is already has, use `a.tag_suggestions` or `a.tag_suggestions_hash`.
  59
+
58 60
 ## Config!
59 61
 
60 62
 `Dank.config` takes a hash.  Options currently are:
28  lib/dank/mixin.rb
@@ -44,6 +44,26 @@ def get_hash
44 44
       Hash[redis.zrange("dank:#{Dank.app_name}:#{@taggable_name}:#{@objekt.id}",0,-1,{withscores:true})]
45 45
     end
46 46
 
  47
+    def tag_suggestions_hash
  48
+      weights = {}
  49
+      self.get_hash.each do |k,v|
  50
+        self.class.tag_neighbors_hash(@taggable_name, @tag_name, k).each do |k2,v2|
  51
+          unless self.get_array.index k2
  52
+            weights[k2] ||= 0
  53
+            weights[k2] += v*v2
  54
+          end
  55
+        end
  56
+      end
  57
+      weights
  58
+    end
  59
+
  60
+    def tag_suggestions
  61
+      weights = tag_suggestions_hash
  62
+      weights.keys.sort do |a,b|
  63
+        self.class.sort_weights weights[a], weights[b]
  64
+      end
  65
+    end
  66
+
47 67
     def reorder(tags)
48 68
       return false unless @objekt.id
49 69
       return false unless tags.sort == get_array.sort # for making sure that we don't allow a reorder without every tag present
@@ -190,6 +210,14 @@ def tag_name(name)
190 210
           tag_lib.reorder(tags)
191 211
         end
192 212
 
  213
+        define_method :"#{name}_suggestions" do
  214
+          tag_lib.tag_suggestions
  215
+        end
  216
+
  217
+        define_method :"#{name}_suggestions_hash" do
  218
+          tag_lib.tag_suggestions_hash
  219
+        end
  220
+
193 221
         define_method :"shared_#{name}s" do |other|
194 222
           other_id = other.respond_to?(:id) ? other.id : other
195 223
           tag_lib.get_shared other_id
4  spec/dank_spec.rb
@@ -114,6 +114,10 @@ def id
114 114
           other_user.add_genre 'pop'
115 115
           other_user.remove_genre 'rap'
116 116
           klass.genre_neighbors('pop').should == []
  117
+          user.remove_genre 'rap'
  118
+          user.genre_suggestions.should == ['rap']
  119
+          user.remove_genre 'country'
  120
+          user.genre_suggestions.should =~ ['rap', 'country']
117 121
         end
118 122
       end
119 123
     end

No commit comments for this range

Something went wrong with that request. Please try again.