From 008f416261adb0e95b676e1d39b05060ec1cdbbd Mon Sep 17 00:00:00 2001 From: Lucas Moura Date: Mon, 27 Jun 2016 16:24:45 -0300 Subject: [PATCH] Refactor apprecommender options --- apprecommender/app_recommender.py | 5 ++- apprecommender/config.py | 2 + apprecommender/load_options.py | 63 ++++++++++--------------------- apprecommender/strategy.py | 12 +++++- bin/apprec.py | 21 +++-------- 5 files changed, 42 insertions(+), 61 deletions(-) diff --git a/apprecommender/app_recommender.py b/apprecommender/app_recommender.py index 2c058b2..34ff381 100644 --- a/apprecommender/app_recommender.py +++ b/apprecommender/app_recommender.py @@ -5,18 +5,21 @@ from apprecommender.recommender import Recommender from apprecommender.user import LocalSystem +from apprecommender.config import Config class AppRecommender: def __init__(self): self.recommender = Recommender() + self.config = Config() - def make_recommendation(self, recommendation_size): + def make_recommendation(self): begin_time = datetime.datetime.now() logging.info("Computation started at %s" % begin_time) # user = RandomPopcon(cfg.popcon_dir,os.path.join(cfg.filters_dir, # "desktopapps")) user = LocalSystem() + recommendation_size = Config().num_recommendations user_reccomendation = (self.recommender.get_recommendation( user, recommendation_size)) diff --git a/apprecommender/config.py b/apprecommender/config.py index a5455a0..3b326a6 100644 --- a/apprecommender/config.py +++ b/apprecommender/config.py @@ -96,6 +96,8 @@ def __init__(self): self.bm25_nl = 0.5 # user content profile size self.profile_size = 10 + self.num_recommendations = 8 + self.because = False # neighborhood size self.k_neighbors = 50 # popcon profiling method: full, voted diff --git a/apprecommender/load_options.py b/apprecommender/load_options.py index 6244082..732906f 100644 --- a/apprecommender/load_options.py +++ b/apprecommender/load_options.py @@ -15,67 +15,40 @@ def __init__(self): def load(self): config = Config() - short_options = "hdvo:f:b:a:e:p:m:u:l:c:x:w:s:z:r:n:idvo:tdvo" - long_options = ["help", "debug", "verbose", "output=", "filtersdir=", - "pkgsfilter=", "axi=", "dde=", "popconindex=", - "popcondir=", "indexmode=", "clustersdir=", - "kmedoids=", "maxpopcon=", "weight=", "strategy=", - "profile_size=", "profiling=", "neighbors=", "init", - "train"] + short_options = 'hdvo:f:b:a:e:p:m:u:l:c:x:w:s:z:r:n:idvo:tdvo' + long_options = ['help', 'debug', 'verbose', 'kmedoids=', 'maxpopcon=', + 'weight=', 'strategy=', 'profile_size=', 'init', + 'train', 'because', 'nrecommendation'] try: opts, args = getopt.getopt(sys.argv[1:], short_options, long_options) self.options = opts except getopt.GetoptError as error: config.set_logger() - logging.error("Bad syntax: %s" % str(error)) + logging.error('Bad syntax: {}'.format(str(error))) self.usage() sys.exit() for o, p in opts: - if o in ("-h", "--help"): + if o in ('-h', '--help'): self.usage() sys.exit() - elif o in ("-d", "--debug"): + elif o in ('-d', '--debug'): config.debug = 1 - elif o in ("-v", "--verbose"): + elif o in ('-v', '--verbose'): config.verbose = 1 - elif o in ("-o", "--output"): - config.output = p - elif o in ("-f", "--filtersdir"): - config.filters_dir = p - elif o in ("-b", "--pkgsfilter"): - config.pkgs_filter = p - elif o in ("-a", "--axi"): - config.axi = p - elif o in ("-e", "--dde"): - config.dde_url = p - elif o in ("-p", "--popconindex"): - config.popcon_index = p - elif o in ("-m", "--popcondir"): - config.popcon_dir = p - elif o in ("-u", "--index_mode"): - config.index_mode = p - elif o in ("-l", "--clustersdir"): - config.clusters_dir = p - elif o in ("-c", "--kmedoids"): - config.k_medoids = int(p) - elif o in ("-x", "--max_popcon"): - config.max_popcon = int(p) - elif o in ("-w", "--weight"): - config.weight = p - elif o in ("-s", "--strategy"): + elif o in ('-s', '--strategy'): config.strategy = p - elif o in ("-z", "--profile_size"): + elif o in ('-z', '--profile_size'): config.profile_size = int(p) - elif o in ("-z", "--profiling"): - config.profiling = p - elif o in ("-n", "--neighbors"): - config.k_neighbors = int(p) - elif o in ("-i", "--init"): + elif o in ('-i', '--init'): continue - elif o in ("-t", "--train"): + elif o in ('-t', '--train'): continue + elif o in ('-b', '--because'): + config.because = True + elif o in ('-n', '--num-recommendations'): + config.num_recommendations = int(p) else: assert False, "unhandled option" @@ -89,6 +62,10 @@ def usage(self): print " -i, --init Initialize AppRecommender data" print " -t, --train Make training of AppRecommender" \ " machine learning" + print " -n, --num-recommendations Set the number of packages that" \ + " will be recommended" + print " -b, --because Display user packages that" \ + " generated a given recommendation" print " -d, --debug Set logging level to debug" print " -v, --verbose Set logging level to verbose" print " -o, --output=PATH Path to file to save output" diff --git a/apprecommender/strategy.py b/apprecommender/strategy.py index 1ec891a..6ea1979 100644 --- a/apprecommender/strategy.py +++ b/apprecommender/strategy.py @@ -76,6 +76,7 @@ def get_sugestion_from_profile(self, rec, user, profile, enquire = xapian.Enquire(rec.items_repository) enquire.set_weighting_scheme(rec.weight) enquire.set_query(query) + user_profile = None # Retrieve matching packages try: mset = enquire.get_mset(0, recommendation_size, None, @@ -90,7 +91,8 @@ def get_sugestion_from_profile(self, rec, user, profile, item_score[m.document.get_data()] = m.weight ranking.append(m.document.get_data()) - user_profile = user.pkg_profile if because else None + if because and Config().because: + user_profile = user.pkg_profile result = recommender.RecommendationResult( item_score, ranking, user_profile=user_profile) @@ -504,6 +506,8 @@ def run_train(cls, pkgs_classifications): raise NotImplementedError("Method not implemented.") def run(self, rec, user, rec_size): + user_profile = None + terms_name, debtags_name = self.load_terms_and_debtags() pkgs, pkgs_score = self.get_pkgs_and_scores(rec, user) @@ -512,8 +516,12 @@ def run(self, rec, user, rec_size): debtags_name) item_score = self.get_item_score(pkgs_score, pkgs_classifications) + + if Config().because: + user_profile = user.pkg_profile + return recommender.RecommendationResult( - item_score, limit=rec_size, user_profile=user.pkg_profile) + item_score, limit=rec_size, user_profile=user_profile) class MachineLearningBVA(MachineLearning): diff --git a/bin/apprec.py b/bin/apprec.py index 005dd52..45adb5f 100755 --- a/bin/apprec.py +++ b/bin/apprec.py @@ -35,9 +35,9 @@ ERROR_TRAIN = 2 -def call_initialize(options): +def check_for_flag(options, short_flag, long_flag): for option, _ in options: - if option in ("-i", "--init"): + if option in (short_flag, long_flag): return True return False @@ -45,10 +45,8 @@ def call_initialize(options): def run_apprecommender(options): try: - recommendation_size = 8 - app_recommender = AppRecommender() - app_recommender.make_recommendation(recommendation_size) + app_recommender.make_recommendation() return SUCCESS except xapian.DatabaseOpeningError: return ERROR_INIT @@ -57,24 +55,17 @@ def run_apprecommender(options): return ERROR_TRAIN -def call_training(options): - for option, _ in options: - if option in ("-t", "--train"): - return True - - return False - - def run(): load_options = LoadOptions() load_options.load() + options = load_options.options - if call_initialize(load_options.options): + if check_for_flag(options, '-i', '--init'): print "Initializing AppRecommender" initialize = Initialize() initialize.prepare_data() return SUCCESS - elif call_training(load_options.options): + elif check_for_flag(options, '-t', '--train'): print "Training machine learning" MachineLearning.train(MachineLearningBVA) MachineLearning.train(MachineLearningBOW)