Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 117 lines (98 sloc) 2.783 kb
f6cef84 @dlt Exercise from the 2nd chapter
authored
1 require 'rubygems'
2 require 'httparty'
3 require 'digest/md5'
4 require 'recommendations.rb'
5764f42 @dlt removed the del.ici.ous api logic and put it in another project named…
authored
5 require '../rublicious/rublicious.rb'
f6cef84 @dlt Exercise from the 2nd chapter
authored
6
7 class DeliciousRecommender
8
c7a25d1 @dlt More retabs.
authored
9 def initialize
10 @recommender = Recommendations.new
11 @api = Rublicious::Feeds.new
521dd78 @dlt Separated the code interfacing with del.ici.ous API in different classes
authored
12 set_handlers
c7a25d1 @dlt More retabs.
authored
13 end
f6cef84 @dlt Exercise from the 2nd chapter
authored
14
c7a25d1 @dlt More retabs.
authored
15 def user_recommendations(user, tag, similarity = :sim_pearson, count = 10)
16 user_hash = initialize_user_hash(user, tag)
17 fill_items(user_hash) {|user| @api.get_userposts(user, tag, count) }
18 @recommender.get_recommendations(user_hash, user, similarity)
19 end
f015e3c @dlt Added multi-thread capability
authored
20
c7a25d1 @dlt More retabs.
authored
21 def tag_recommendations(tag, similarity = :sim_pearson, count = 10)
521dd78 @dlt Separated the code interfacing with del.ici.ous API in different classes
authored
22 tag_hash = init_tag_hash(tag, count)
59e56a7 @dlt Adding a method to find tag-based similarity using the delicious API
authored
23 fill_tag_items(tag_hash)
b308e92 @dlt Exercise 2 completed. Tag recommendation
authored
24 tag_hash = @recommender.transform_prefs tag_hash
521dd78 @dlt Separated the code interfacing with del.ici.ous API in different classes
authored
25 @recommender.get_recommendations(tag_hash, tag, similarity)
c7a25d1 @dlt More retabs.
authored
26 end
59e56a7 @dlt Adding a method to find tag-based similarity using the delicious API
authored
27
c7a25d1 @dlt More retabs.
authored
28 private
521dd78 @dlt Separated the code interfacing with del.ici.ous API in different classes
authored
29 def set_handlers
5764f42 @dlt removed the del.ici.ous api logic and put it in another project named…
authored
30 @api.xml_client.add_response_handler do |response|
521dd78 @dlt Separated the code interfacing with del.ici.ous API in different classes
authored
31 response = response['rss']['channel']['item']
32 # if returned 1 or 0 items in the response, makes it look like an array
33 response = [response] if response.is_a? Hash
34 response ||= []
35 response
36 end
37 end
38
c7a25d1 @dlt More retabs.
authored
39 def init_tag_hash(tag, count = 10)
40 categories = [tag]
521dd78 @dlt Separated the code interfacing with del.ici.ous API in different classes
authored
41
c7a25d1 @dlt More retabs.
authored
42 @api.get_popular(tag, count).each do |post|
43 categs = post.category
44 categs = [categs] unless categs.is_a? Array
45 categories += categs
46 end
521dd78 @dlt Separated the code interfacing with del.ici.ous API in different classes
authored
47
c7a25d1 @dlt More retabs.
authored
48 init_hash_with_keys(categories)
49 end
f6cef84 @dlt Exercise from the 2nd chapter
authored
50
c7a25d1 @dlt More retabs.
authored
51 def initialize_user_hash(user, tag, count = 5)
52 creators = [user]
53
5764f42 @dlt removed the del.ici.ous api logic and put it in another project named…
authored
54 @api.get_popular(tag, count).each do |post|
f015e3c @dlt Added multi-thread capability
authored
55 Thread.new do
c7a25d1 @dlt More retabs.
authored
56 @api.get_urlposts(post.link).each do |post2|
57 creator = post2.dc_creator
58 creators << creator
59 end
f015e3c @dlt Added multi-thread capability
authored
60 end
c7a25d1 @dlt More retabs.
authored
61 end
5764f42 @dlt removed the del.ici.ous api logic and put it in another project named…
authored
62
f015e3c @dlt Added multi-thread capability
authored
63 join_all
c7a25d1 @dlt More retabs.
authored
64 init_hash_with_keys(creators)
65 end
59e56a7 @dlt Adding a method to find tag-based similarity using the delicious API
authored
66
67 def fill_tag_items(hash)
68 copy = hash.dup
69 copy.each_key do |tag|
521dd78 @dlt Separated the code interfacing with del.ici.ous API in different classes
authored
70 Thread.new do
71 popular_posts = @api.get_popular(tag)
72 popular_posts.each do |post|
73 Thread.new do
b8ec1d6 @dlt refactored code dealing with delicious API to use new rublicious methods
authored
74 top_tags = @api.get_urlinfo(post.link).top_tags
521dd78 @dlt Separated the code interfacing with del.ici.ous API in different classes
authored
75 hash[url] = top_tags
76 end
77 end
59e56a7 @dlt Adding a method to find tag-based similarity using the delicious API
authored
78 end
79 end
5764f42 @dlt removed the del.ici.ous api logic and put it in another project named…
authored
80
521dd78 @dlt Separated the code interfacing with del.ici.ous API in different classes
authored
81 join_all
59e56a7 @dlt Adding a method to find tag-based similarity using the delicious API
authored
82 hash
83 end
f6cef84 @dlt Exercise from the 2nd chapter
authored
84
c7a25d1 @dlt More retabs.
authored
85 def fill_items(hash, extract = 'link')
86 all_items = []
87 hash.each_key do |item|
f015e3c @dlt Added multi-thread capability
authored
88 Thread.new do
59e56a7 @dlt Adding a method to find tag-based similarity using the delicious API
authored
89 posts = yield(item)
f015e3c @dlt Added multi-thread capability
authored
90 posts.each do |post|
59e56a7 @dlt Adding a method to find tag-based similarity using the delicious API
authored
91 value = post[extract]
92 hash[item][value] = 1.0
93 all_items << value
f015e3c @dlt Added multi-thread capability
authored
94 end
95 end
c7a25d1 @dlt More retabs.
authored
96 end
5764f42 @dlt removed the del.ici.ous api logic and put it in another project named…
authored
97
f015e3c @dlt Added multi-thread capability
authored
98 join_all
f6cef84 @dlt Exercise from the 2nd chapter
authored
99
c7a25d1 @dlt More retabs.
authored
100 hash.each_pair do |key, ratings|
101 all_items.each do |item|
102 ratings[item] = 0.0 unless ratings.has_key? item
103 end
104 end
105 end
f6cef84 @dlt Exercise from the 2nd chapter
authored
106
c7a25d1 @dlt More retabs.
authored
107 def init_hash_with_keys(keys)
521dd78 @dlt Separated the code interfacing with del.ici.ous API in different classes
authored
108 hash = {}
c7a25d1 @dlt More retabs.
authored
109 keys.uniq.each { |k| hash[k] = {} }
110 hash
111 end
521dd78 @dlt Separated the code interfacing with del.ici.ous API in different classes
authored
112
113 def join_all
114 Thread.list.each {|t| t.join unless t == Thread.current || t == Thread.main }
115 end
116 end
Something went wrong with that request. Please try again.