Commit efb902224ab2245fac5ea7c46801af7e8d4e448a

Authored by Tássia Camões Araújo
1 parent 8ce5c499
Exists in master and in 1 other branch add_vagrant

Added weighting scheme and clustering options to recommender; changed method for…

… setting recommender strategy; do not use eval due to security risks; implemmented tests for recommender.
Showing 1 changed file with 27 additions and 42 deletions   Show diff stats
src/recommender.py
@@ -19,10 +19,10 @@ __license__ = """ @@ -19,10 +19,10 @@ __license__ = """
19 along with this program. If not, see <http://www.gnu.org/licenses/>. 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """ 20 """
21 21
22 -from operator import itemgetter  
23 -from data import *  
24 -from strategy import *  
25 -from error import Error 22 +import xapian
  23 +import operator
  24 +import data
  25 +import strategy
26 26
27 class RecommendationResult: 27 class RecommendationResult:
28 """ 28 """
@@ -40,7 +40,7 @@ class RecommendationResult: @@ -40,7 +40,7 @@ class RecommendationResult:
40 """ 40 """
41 result = self.get_prediction() 41 result = self.get_prediction()
42 str = "\n" 42 str = "\n"
43 - for i in range(len(result)): 43 + for i in range(len((list(result)))):
44 str += "%2d: %s\n" % (i,result[i][0]) 44 str += "%2d: %s\n" % (i,result[i][0])
45 return str 45 return str
46 46
@@ -48,8 +48,10 @@ class RecommendationResult: @@ -48,8 +48,10 @@ class RecommendationResult:
48 """ 48 """
49 Return prediction based on recommendation size (number of items). 49 Return prediction based on recommendation size (number of items).
50 """ 50 """
51 - sorted_result = sorted(self.item_score.items(), key=itemgetter(1))  
52 - return reversed(sorted_result[-size:]) 51 + if size > len(self.item_score): size = len(self.item_score)
  52 + sorted_result = sorted(self.item_score.items(),
  53 + key=operator.itemgetter(1))
  54 + return list(reversed(sorted_result[-size:]))
53 55
54 class Recommender: 56 class Recommender:
55 """ 57 """
@@ -59,47 +61,30 @@ class Recommender: @@ -59,47 +61,30 @@ class Recommender:
59 """ 61 """
60 Set initial parameters. 62 Set initial parameters.
61 """ 63 """
62 - try:  
63 - strategy = "self."+cfg.strategy+"(cfg)"  
64 - exec(strategy)  
65 - except (NameError, AttributeError, SyntaxError) as err:  
66 - print err  
67 - logging.critical("Could not perform recommendation strategy '%s'" %  
68 - cfg.strategy)  
69 - raise Error  
70 -  
71 - def ct(self,cfg):  
72 - """  
73 - Set recommender attributes to perform content-based recommendation  
74 - using tags index as source data.  
75 - """  
76 - self.items_repository = TagsXapianIndex(cfg)  
77 - self.strategy = ContentBasedStrategy()  
78 -  
79 - def cta(self,cfg):  
80 - """  
81 - Set recommender attributes to perform content-based recommendation  
82 - using apt-xapian-index as source data.  
83 - """  
84 self.items_repository = xapian.Database(cfg.axi) 64 self.items_repository = xapian.Database(cfg.axi)
85 - self.strategy = AxiContentBasedStrategy()  
86 -  
87 - def col(self,cfg):  
88 - """  
89 - Set recommender attributes to perform collaborative recommendation  
90 - using popcon-xapian-index as source data.  
91 - """  
92 - self.users_repository = PopconXapianIndex(cfg)  
93 - self.strategy = CollaborativeStrategy() 65 + self.users_repository = data.PopconXapianIndex(cfg) #[FIXME] only cfg fields
  66 + self.clustered_users_repository = data.PopconXapianIndex(cfg) #[FIXME]
  67 + self.set_strategy(cfg.strategy)
  68 + if cfg.weight == "bm25":
  69 + self.weight = xapian.BM25Weight()
  70 + else:
  71 + self.weight = xapian.TradWeight()
94 72
95 - def set_strategy(self,strategy): 73 + def set_strategy(self,strategy_str):
96 """ 74 """
97 Set the recommendation strategy. 75 Set the recommendation strategy.
98 """ 76 """
99 - self.strategy = strategy 77 + if strategy_str == "cb":
  78 + self.strategy = strategy.ContentBasedStrategy("full")
  79 + if strategy_str == "cbt":
  80 + self.strategy = strategy.ContentBasedStrategy("tag")
  81 + if strategy_str == "cbd":
  82 + self.strategy = strategy.ContentBasedStrategy("desc")
  83 + if strategy_str == "col":
  84 + self.strategy = strategy.CollaborativeStrategy(20)
100 85
101 - def get_recommendation(self,user): 86 + def get_recommendation(self,user,limit=20):
102 """ 87 """
103 Produces recommendation using previously loaded strategy. 88 Produces recommendation using previously loaded strategy.
104 """ 89 """
105 - return self.strategy.run(self,user) 90 + return self.strategy.run(self,user,limit)