Commit 7174d45469281c3303b0b37bf7d2ad2399a1e48a
1 parent
43742989
Exists in
master
and in
1 other branch
Code Refactoring
- Detached cross-validation from app_recomender script - Moved load_options and set_logger to Config() initialization - Pythonized print_result method (renamed to __str__)
Showing
4 changed files
with
73 additions
and
18 deletions
Show diff stats
src/app_recommender.py
| @@ -47,19 +47,8 @@ def set_up_recommender(cfg): | @@ -47,19 +47,8 @@ def set_up_recommender(cfg): | ||
| 47 | 47 | ||
| 48 | return app_rec | 48 | return app_rec |
| 49 | 49 | ||
| 50 | -def cross_validation(recommender): | ||
| 51 | - metrics = [] | ||
| 52 | - metrics.append(Precision()) | ||
| 53 | - metrics.append(Recall()) | ||
| 54 | - validation = CrossValidation(0.1,10,recommender,metrics) | ||
| 55 | - validation.run(user) | ||
| 56 | - | ||
| 57 | if __name__ == '__main__': | 50 | if __name__ == '__main__': |
| 58 | cfg = Config() | 51 | cfg = Config() |
| 59 | - cfg.load_options() | ||
| 60 | - cfg.set_logger() | ||
| 61 | rec = set_up_recommender(cfg) | 52 | rec = set_up_recommender(cfg) |
| 62 | user = LocalSystem() | 53 | user = LocalSystem() |
| 63 | - result = rec.get_recommendation(user) | ||
| 64 | - result.print_result() | ||
| 65 | - cross_validation(rec) | 54 | + print rec.get_recommendation(user) |
src/config.py
| @@ -31,7 +31,7 @@ class Config(): | @@ -31,7 +31,7 @@ class Config(): | ||
| 31 | """ | 31 | """ |
| 32 | def __init__(self): | 32 | def __init__(self): |
| 33 | """ | 33 | """ |
| 34 | - Set default configuration options. | 34 | + Set configuration options. |
| 35 | """ | 35 | """ |
| 36 | self.debug = 0 | 36 | self.debug = 0 |
| 37 | self.verbose = 0 | 37 | self.verbose = 0 |
| @@ -43,6 +43,8 @@ class Config(): | @@ -43,6 +43,8 @@ class Config(): | ||
| 43 | self.axi_values = "/var/lib/apt-xapian-index/values" | 43 | self.axi_values = "/var/lib/apt-xapian-index/values" |
| 44 | self.strategy = "ct" # defaults to the cheapest one | 44 | self.strategy = "ct" # defaults to the cheapest one |
| 45 | self.reindex = 0 | 45 | self.reindex = 0 |
| 46 | + self.load_options() | ||
| 47 | + self.set_logger() | ||
| 46 | 48 | ||
| 47 | def usage(self): | 49 | def usage(self): |
| 48 | """ | 50 | """ |
| @@ -0,0 +1,62 @@ | @@ -0,0 +1,62 @@ | ||
| 1 | +#!/usr/bin/python | ||
| 2 | + | ||
| 3 | +# AppRecommender - A GNU/Linux application recommender | ||
| 4 | +# | ||
| 5 | +# Copyright (C) 2010 Tassia Camoes <tassia@gmail.com> | ||
| 6 | +# | ||
| 7 | +# This program is free software: you can redistribute it and/or modify | ||
| 8 | +# it under the terms of the GNU General Public License as published by | ||
| 9 | +# the Free Software Foundation, either version 3 of the License, or | ||
| 10 | +# (at your option) any later version. | ||
| 11 | +# | ||
| 12 | +# This program is distributed in the hope that it will be useful, | ||
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | +# GNU General Public License for more details. | ||
| 16 | +# | ||
| 17 | +# You should have received a copy of the GNU General Public License | ||
| 18 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 19 | + | ||
| 20 | +import os | ||
| 21 | +import sys | ||
| 22 | +import logging | ||
| 23 | + | ||
| 24 | +from config import * | ||
| 25 | +from data import * | ||
| 26 | +from evaluation import * | ||
| 27 | +from similarity_measure import * | ||
| 28 | +from recommender import * | ||
| 29 | +from strategy import * | ||
| 30 | +from user import * | ||
| 31 | + | ||
| 32 | +def set_up_recommender(cfg): | ||
| 33 | + if cfg.strategy == "cta": | ||
| 34 | + axi_db = xapian.Database(cfg.axi) | ||
| 35 | + app_rec = Recommender(axi_db) | ||
| 36 | + app_rec.set_strategy(AxiContentBasedStrategy()) | ||
| 37 | + | ||
| 38 | + elif cfg.strategy == "ct": | ||
| 39 | + debtags_db = DebtagsDB(cfg.tags_db) | ||
| 40 | + if not debtags_db.load(): | ||
| 41 | + logging.error("Could not load DebtagsDB from %s." % cfg.tags_db) | ||
| 42 | + sys.exit(1) | ||
| 43 | + debtags_index = DebtagsIndex(os.path.expanduser(cfg.tags_index)) | ||
| 44 | + debtags_index.load(debtags_db,cfg.reindex) | ||
| 45 | + app_rec = Recommender(debtags_index) | ||
| 46 | + app_rec.set_strategy(ContentBasedStrategy()) | ||
| 47 | + | ||
| 48 | + return app_rec | ||
| 49 | + | ||
| 50 | +def cross_validation(recommender): | ||
| 51 | + metrics = [] | ||
| 52 | + metrics.append(Precision()) | ||
| 53 | + metrics.append(Recall()) | ||
| 54 | + validation = CrossValidation(0.1,10,recommender,metrics) | ||
| 55 | + validation.run(user) | ||
| 56 | + | ||
| 57 | +if __name__ == '__main__': | ||
| 58 | + cfg = Config() | ||
| 59 | + rec = set_up_recommender(cfg) | ||
| 60 | + user = LocalSystem() | ||
| 61 | + #result.print_result() | ||
| 62 | + cross_validation(rec) |
src/recommender.py
| @@ -24,15 +24,17 @@ class RecommendationResult: | @@ -24,15 +24,17 @@ class RecommendationResult: | ||
| 24 | self.item_score = item_score | 24 | self.item_score = item_score |
| 25 | self.size = size | 25 | self.size = size |
| 26 | 26 | ||
| 27 | + def __str__(self): | ||
| 28 | + result = self.get_prediction() | ||
| 29 | + str = "\n" | ||
| 30 | + for i in range(len(result)): | ||
| 31 | + str += "%2d: %s\n" % (i,result[i][0]) | ||
| 32 | + return str | ||
| 33 | + | ||
| 27 | def get_prediction(self): | 34 | def get_prediction(self): |
| 28 | sorted_result = sorted(self.item_score.items(), key=itemgetter(1)) | 35 | sorted_result = sorted(self.item_score.items(), key=itemgetter(1)) |
| 29 | return sorted_result[:self.size] | 36 | return sorted_result[:self.size] |
| 30 | 37 | ||
| 31 | - def print_result(self): | ||
| 32 | - result = self.get_prediction() | ||
| 33 | - for i in range(len(result)): | ||
| 34 | - print "%2d: %s" % (i,result[i][0]) | ||
| 35 | - | ||
| 36 | class Recommender: | 38 | class Recommender: |
| 37 | """ """ | 39 | """ """ |
| 38 | def __init__(self,items_repository,users_repository=None, | 40 | def __init__(self,items_repository,users_repository=None, |