Skip to content

Commit

Permalink
Refactor apprecommender options
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmoura committed Jun 27, 2016
1 parent c34f1d4 commit 008f416
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 61 deletions.
5 changes: 4 additions & 1 deletion apprecommender/app_recommender.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
2 changes: 2 additions & 0 deletions apprecommender/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 20 additions & 43 deletions apprecommender/load_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"
Expand Down
12 changes: 10 additions & 2 deletions apprecommender/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down
21 changes: 6 additions & 15 deletions bin/apprec.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,18 @@
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


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
Expand All @@ -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)
Expand Down

0 comments on commit 008f416

Please sign in to comment.