Commit 7f4319d5721a50872ed571fff821997666664a7c

Authored by Tássia Camões Araújo
1 parent cf91ea7c
Exists in master and in 1 other branch add_vagrant

Updated set_strategy method with new strategies and fixed recommender

initialization.
Showing 1 changed file with 74 additions and 25 deletions   Show diff stats
src/recommender.py
... ... @@ -25,6 +25,7 @@ import xapian
25 25 import operator
26 26 import data
27 27 import strategy
  28 +import apt
28 29  
29 30 class RecommendationResult:
30 31 """
... ... @@ -43,9 +44,13 @@ class RecommendationResult:
43 44 """
44 45 String representation of the object.
45 46 """
  47 + #[FIXME] try alternative way to get pkgs summarys (efficiency)
  48 + #cache = apt.Cache()
46 49 result = self.get_prediction()
47 50 str = "\n"
48 51 for i in range(len((list(result)))):
  52 + #summary = cache[result[i][0]].candidate.summary
  53 + #str += "%2d: %s\t\t- %s\n" % (i,result[i][0],summary)
49 54 str += "%2d: %s\n" % (i,result[i][0])
50 55 return str
51 56  
... ... @@ -69,40 +74,84 @@ class Recommender:
69 74 Set initial parameters.
70 75 """
71 76 self.cfg = cfg
72   - self.items_repository = xapian.Database(cfg.axi)
73   - self.set_strategy(cfg.strategy)
  77 + # Load xapian indexes
  78 + self.axi_programs = xapian.Database(cfg.axi_programs)
  79 + self.axi_desktopapps = xapian.Database(cfg.axi_desktopapps)
  80 + if cfg.popcon:
  81 + self.popcon_programs = xapian.Database(cfg.popcon_programs)
  82 + self.popcon_desktopapps = xapian.Database(cfg.popcon_desktopapps)
  83 + # Load valid programs, desktopapps and tags
  84 + # format: one package or tag name per line
  85 + self.valid_programs = []
  86 + self.valid_desktopapps = []
  87 + self.valid_tags = []
  88 + logging.info("Loading recommender filters")
  89 + with open(os.path.join(cfg.filters_dir,"programs")) as pkgs:
  90 + self.valid_programs = [line.strip() for line in pkgs
  91 + if not line.startswith("#")]
  92 + with open(os.path.join(cfg.filters_dir,"desktopapps")) as pkgs:
  93 + self.valid_desktopapps = [line.strip() for line in pkgs
  94 + if not line.startswith("#")]
  95 + with open(os.path.join(cfg.filters_dir,"debtags")) as tags:
  96 + self.valid_tags = [line.strip() for line in tags
  97 + if not line.startswith("#")]
  98 + # Set xapian index weighting scheme
74 99 if cfg.weight == "bm25":
75   - self.weight = xapian.BM25Weight()
  100 + self.weight = xapian.BM25Weight(cfg.bm25_k1, cfg.bm25_k2,
  101 + cfg.bm25_k3, cfg.bm25_b,
  102 + cfg.bm25_nl)
76 103 else:
77 104 self.weight = xapian.TradWeight()
78   - self.valid_pkgs = []
79   - # file format: one pkg_name per line
80   - with open(os.path.join(cfg.filters,cfg.pkgs_filter)) as valid_pkgs:
81   - self.valid_pkgs = [line.strip() for line in valid_pkgs
82   - if not line.startswith("#")]
  105 + self.set_strategy(cfg.strategy)
83 106  
84 107 def set_strategy(self,strategy_str):
85 108 """
86 109 Set the recommendation strategy.
87 110 """
88 111 logging.info("Setting recommender strategy to \'%s\'" % strategy_str)
89   - self.items_repository = xapian.Database(self.cfg.axi)
90   - if "desktop" in strategy_str:
91   - self.items_repository = xapian.Database("/root/.app-recommender/axi_desktop")
92   - self.cfg.popcon_index = "/root/.app-recommender/popcon-index_desktop_1000"
93   -
94   - if strategy_str == "cb" or strategy_str == "cb_desktop":
95   - self.strategy = strategy.ContentBasedStrategy("full",
96   - self.cfg.profile_size)
97   - if strategy_str == "cbt" or strategy_str == "cbt_desktop":
98   - self.strategy = strategy.ContentBasedStrategy("tag",
99   - self.cfg.profile_size)
100   - if strategy_str == "cbd" or strategy_str == "cbd_desktop":
101   - self.strategy = strategy.ContentBasedStrategy("desc",
102   - self.cfg.profile_size)
103   - if "col" in strategy_str:
104   - self.users_repository = data.PopconXapianIndex(self.cfg)
105   - self.strategy = strategy.CollaborativeStrategy(self.cfg.k_neighbors)
  112 + self.items_repository = self.axi_programs
  113 + self.valid_pkgs = self.valid_programs
  114 + # Check if collaborative strategies can be instanciated
  115 + if ("col" in strategy_str) or ("knn" in strategy_str):
  116 + if not self.cfg.popcon:
  117 + logging.info("Cannot perform collaborative strategy")
  118 + return 1
  119 + else:
  120 + self.users_repository = self.popcon_programs
  121 + # Set strategy based on strategy_str
  122 + if strategy_str == "cb":
  123 + self.strategy = strategy.ContentBased("mix",self.cfg.profile_size)
  124 + elif strategy_str == "cbt":
  125 + self.strategy = strategy.ContentBased("tag",self.cfg.profile_size)
  126 + elif strategy_str == "cbd":
  127 + self.strategy = strategy.ContentBased("desc",self.cfg.profile_size)
  128 + elif strategy_str == "cbh":
  129 + self.strategy = strategy.ContentBased("half",self.cfg.profile_size)
  130 + if strategy_str == "cb_eset":
  131 + self.strategy = strategy.ContentBased("mix_eset",self.cfg.profile_size)
  132 + elif strategy_str == "cbt_eset":
  133 + self.strategy = strategy.ContentBased("tag_eset",self.cfg.profile_size)
  134 + elif strategy_str == "cbd_eset":
  135 + self.strategy = strategy.ContentBased("desc_eset",self.cfg.profile_size)
  136 + elif strategy_str == "cbh_eset":
  137 + self.strategy = strategy.ContentBased("half_eset",self.cfg.profile_size)
  138 + #elif strategy_str == "col":
  139 + # self.strategy = strategy.CollaborativeEset()
  140 + elif strategy_str == "knn":
  141 + self.strategy = strategy.Knn(self.cfg.k_neighbors)
  142 + elif strategy_str == "knn_plus":
  143 + self.strategy = strategy.KnnPlus(self.cfg.k_neighbors)
  144 + elif strategy_str == "knn_eset":
  145 + self.strategy = strategy.KnnEset(self.cfg.k_neighbors)
  146 + elif strategy_str == "knnco":
  147 + self.strategy = strategy.KnnContent(self.cfg.k_neighbors)
  148 + elif strategy_str == "knnco_eset":
  149 + self.strategy = strategy.KnnContentEset(self.cfg.k_neighbors)
  150 + elif strategy_str.startswith("demo"):
  151 + self.strategy = strategy.Demographic(strategy_str)
  152 + else:
  153 + logging.info("Strategy not defined.")
  154 + return
106 155  
107 156 def get_recommendation(self,user,result_size=100):
108 157 """
... ...