From cd07dc71991836f97d8dab0f8802bfee29392b8d Mon Sep 17 00:00:00 2001 From: Luciano Prestes Cavalcanti Date: Sun, 7 Aug 2016 19:20:21 -0300 Subject: [PATCH] Fix bug on train without pkgs on user profile --- apprecommender/main/cli.py | 9 ++++++++- apprecommender/strategy.py | 14 +++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/apprecommender/main/cli.py b/apprecommender/main/cli.py index 7f57557..76d9aaf 100644 --- a/apprecommender/main/cli.py +++ b/apprecommender/main/cli.py @@ -6,7 +6,8 @@ from apprecommender.config import Config from apprecommender.initialize import Initialize from apprecommender.strategy import (MachineLearning, MachineLearningBVA, - MachineLearningBOW) + MachineLearningBOW, + MachineLearningTrainError) from apprecommender.main import collect_user_data from apprecommender.main.options import get_parser @@ -14,6 +15,7 @@ ERROR_INIT = 1 ERROR_TRAIN = 2 PERMISSION_DENIED = 3 +ERROR_INIT_TRAIN = 4 def parse_options(args, config): @@ -64,6 +66,8 @@ def run(args): MachineLearning.train(MachineLearningBOW) except IOError: return PERMISSION_DENIED + except MachineLearningTrainError: + return ERROR_INIT_TRAIN return SUCCESS elif args['contribute']: @@ -91,6 +95,9 @@ def main(): print "Run: apprec.py --train" elif result is PERMISSION_DENIED: print "Please, run this command as sudo" + elif result is ERROR_INIT_TRAIN: + print 'Error: You need install more packages to use machine' \ + ' learning recommendations' if __name__ == '__main__': main() diff --git a/apprecommender/strategy.py b/apprecommender/strategy.py index 91e7ba3..ebe66eb 100644 --- a/apprecommender/strategy.py +++ b/apprecommender/strategy.py @@ -161,6 +161,15 @@ def run(self, rec, user, rec_size): return result +class MachineLearningTrainError(Exception): + + def __init__(self, value=''): + self.value = value + + def __str__(self): + return repr(self.value) + + class MachineLearning(ContentBased): __metaclass__ = ABCMeta @@ -284,7 +293,10 @@ def train(cls): try: MachineLearning.PKGS_CLASSIFICATIONS = ml_data.create_data( labels) - cls.run_train(MachineLearning.PKGS_CLASSIFICATIONS) + if len(MachineLearning.PKGS_CLASSIFICATIONS) > 0: + cls.run_train(MachineLearning.PKGS_CLASSIFICATIONS) + else: + raise MachineLearningTrainError() except IOError: raise