strategy.py
4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/python
# AppRecommender - A GNU/Linux application recommender
#
# Copyright (C) 2010 Tassia Camoes <tassia@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os, re
import xapian
from data import *
from recommender import *
class ReputationHeuristic:
"""
Abstraction for diferent reputation heuristics.
"""
class BugsHeuristic(ReputationHeuristic):
"""
Reputation heuristic based on quantity of open bugs.
"""
class RCBugsHeuristic(ReputationHeuristic):
"""
Reputation heuristic based on quantity of RC bugs.
"""
class PopularityHeuristic(ReputationHeuristic):
"""
Reputation heuristic based on popularity of packages.
"""
class PkgMatchDecider(xapian.MatchDecider):
"""
Extends xapian.MatchDecider to disconsider installed packages.
"""
def __init__(self, installed_pkgs):
xapian.MatchDecider.__init__(self)
self.installed_pkgs = installed_pkgs
def __call__(self, doc):
return doc.get_data() not in self.installed_pkgs
class RecommendationStrategy:
"""
Abstraction for diferent recommendation strategy.
"""
class ItemReputationStrategy(RecommendationStrategy):
"""
Recommendation strategy based on items reputation.
"""
def run(self,items_list,heuristic):
"""
Perform recommendation strategy.
"""
return RecomendationResult()
class ContentBasedStrategy(RecommendationStrategy):
"""
Content-based recommendation strategy.
"""
def run(self,recommender,user):
"""
Perform recommendation strategy.
"""
profile = user.debtags_tag_profile(recommender.items_repository.debtags_db,50)
qp = xapian.QueryParser()
query = qp.parse_query(profile)
enquire = xapian.Enquire(recommender.items_repository)
enquire.set_query(query)
try:
mset = enquire.get_mset(0, 20, None, PkgMatchDecider(user.items()))
except xapian.DatabaseError as error:
logging.critical(error.get_msg())
exit(1)
item_score = {}
for m in mset:
item_score[m.document.get_data()] = m.rank
return RecommendationResult(item_score,20)
class AxiContentBasedStrategy(RecommendationStrategy):
"""
Content-based recommendation strategy based on Apt-xapian-index.
"""
def run(self,recommender,user):
"""
Perform recommendation strategy.
"""
profile = user.axi_tag_profile(recommender.items_repository,50)
query = xapian.Query(xapian.Query.OP_OR,profile)
enquire = xapian.Enquire(recommender.items_repository)
enquire.set_query(query)
try:
mset = enquire.get_mset(0, 20, None, PkgMatchDecider(user.items()))
except xapian.DatabaseError as error:
logging.critical(error.get_msg())
exit(1)
item_score = {}
for m in mset:
item_score[m.document.get_data()] = m.rank
return RecommendationResult(item_score,20)
class ColaborativeStrategy(RecommendationStrategy):
"""
Colaborative recommendation strategy.
"""
def run(self,user,users_repository,similarity_measure):
"""
Perform recommendation strategy.
"""
return RecomendationResult()
class KnowledgeBasedStrategy(RecommendationStrategy):
"""
Knowledge-based recommendation strategy.
"""
def run(self,user,knowledge_repository):
"""
Perform recommendation strategy.
"""
return RecomendationResult()
class DemographicStrategy(RecommendationStrategy):
"""
Recommendation strategy based on demographic data.
"""
def run(self,user,items_repository):
"""
Perform recommendation strategy.
"""
return RecomendationResult()