Commit a7a750fa8dc07c6e7166f04a6565b3eb19f9fefe
1 parent
39001e40
Exists in
master
and in
1 other branch
Small refactoring.
Showing
2 changed files
with
12 additions
and
9 deletions
Show diff stats
src/recommender.py
| @@ -33,6 +33,7 @@ class RecommendationResult: | @@ -33,6 +33,7 @@ class RecommendationResult: | ||
| 33 | Set initial parameters. | 33 | Set initial parameters. |
| 34 | """ | 34 | """ |
| 35 | self.item_score = item_score | 35 | self.item_score = item_score |
| 36 | + self.size = len(item_score) | ||
| 36 | 37 | ||
| 37 | def __str__(self): | 38 | def __str__(self): |
| 38 | """ | 39 | """ |
| @@ -44,14 +45,14 @@ class RecommendationResult: | @@ -44,14 +45,14 @@ class RecommendationResult: | ||
| 44 | str += "%2d: %s\n" % (i,result[i][0]) | 45 | str += "%2d: %s\n" % (i,result[i][0]) |
| 45 | return str | 46 | return str |
| 46 | 47 | ||
| 47 | - def get_prediction(self,size=20): | 48 | + def get_prediction(self,limit=20): |
| 48 | """ | 49 | """ |
| 49 | Return prediction based on recommendation size (number of items). | 50 | Return prediction based on recommendation size (number of items). |
| 50 | """ | 51 | """ |
| 51 | - if size > len(self.item_score): size = len(self.item_score) | 52 | + if limit > self.size: limit = self.size |
| 52 | sorted_result = sorted(self.item_score.items(), | 53 | sorted_result = sorted(self.item_score.items(), |
| 53 | key=operator.itemgetter(1)) | 54 | key=operator.itemgetter(1)) |
| 54 | - return list(reversed(sorted_result[-size:])) | 55 | + return list(reversed(sorted_result[-limit:])) |
| 55 | 56 | ||
| 56 | class Recommender: | 57 | class Recommender: |
| 57 | """ | 58 | """ |
| @@ -83,8 +84,8 @@ class Recommender: | @@ -83,8 +84,8 @@ class Recommender: | ||
| 83 | if strategy_str == "col": | 84 | if strategy_str == "col": |
| 84 | self.strategy = strategy.CollaborativeStrategy(20) | 85 | self.strategy = strategy.CollaborativeStrategy(20) |
| 85 | 86 | ||
| 86 | - def get_recommendation(self,user,limit=20): | 87 | + def get_recommendation(self,user,result_size=20): |
| 87 | """ | 88 | """ |
| 88 | Produces recommendation using previously loaded strategy. | 89 | Produces recommendation using previously loaded strategy. |
| 89 | """ | 90 | """ |
| 90 | - return self.strategy.run(self,user,limit) | 91 | + return self.strategy.run(self,user,result_size) |
src/strategy.py
| @@ -95,15 +95,17 @@ class ContentBasedStrategy(RecommendationStrategy): | @@ -95,15 +95,17 @@ class ContentBasedStrategy(RecommendationStrategy): | ||
| 95 | """ | 95 | """ |
| 96 | Content-based recommendation strategy based on Apt-xapian-index. | 96 | Content-based recommendation strategy based on Apt-xapian-index. |
| 97 | """ | 97 | """ |
| 98 | - def __init__(self,content): | 98 | + def __init__(self,content,profile_size=50): |
| 99 | self.description = "Content-based" | 99 | self.description = "Content-based" |
| 100 | self.content = content | 100 | self.content = content |
| 101 | + self.profile_size = profile_size | ||
| 101 | 102 | ||
| 102 | def run(self,rec,user,limit): | 103 | def run(self,rec,user,limit): |
| 103 | """ | 104 | """ |
| 104 | Perform recommendation strategy. | 105 | Perform recommendation strategy. |
| 105 | """ | 106 | """ |
| 106 | - profile = user.profile(rec.items_repository,self.content,50) | 107 | + profile = user.profile(rec.items_repository,self.content, |
| 108 | + self.profile_size) | ||
| 107 | # prepair index for querying user profile | 109 | # prepair index for querying user profile |
| 108 | query = xapian.Query(xapian.Query.OP_OR,profile) | 110 | query = xapian.Query(xapian.Query.OP_OR,profile) |
| 109 | enquire = xapian.Enquire(rec.items_repository) | 111 | enquire = xapian.Enquire(rec.items_repository) |
| @@ -129,7 +131,7 @@ class CollaborativeStrategy(RecommendationStrategy): | @@ -129,7 +131,7 @@ class CollaborativeStrategy(RecommendationStrategy): | ||
| 129 | self.clustering = clustering | 131 | self.clustering = clustering |
| 130 | self.neighbours = k | 132 | self.neighbours = k |
| 131 | 133 | ||
| 132 | - def run(self,rec,user,limit): | 134 | + def run(self,rec,user,result_size): |
| 133 | """ | 135 | """ |
| 134 | Perform recommendation strategy. | 136 | Perform recommendation strategy. |
| 135 | """ | 137 | """ |
| @@ -153,7 +155,7 @@ class CollaborativeStrategy(RecommendationStrategy): | @@ -153,7 +155,7 @@ class CollaborativeStrategy(RecommendationStrategy): | ||
| 153 | rset.add_document(m.document.get_docid()) | 155 | rset.add_document(m.document.get_docid()) |
| 154 | logging.debug(m.document.get_data()) | 156 | logging.debug(m.document.get_data()) |
| 155 | # retrieve most relevant packages | 157 | # retrieve most relevant packages |
| 156 | - eset = enquire.get_eset(limit,rset,PkgExpandDecider()) | 158 | + eset = enquire.get_eset(result_size,rset,PkgExpandDecider()) |
| 157 | # compose result dictionary | 159 | # compose result dictionary |
| 158 | item_score = {} | 160 | item_score = {} |
| 159 | for package in eset: | 161 | for package in eset: |