recommender.py
3.29 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
#!/usr/bin/env python
"""
recommender - python module for classes related to recommenders.
"""
__author__ = "Tassia Camoes Araujo <tassia@gmail.com>"
__copyright__ = "Copyright (C) 2011 Tassia Camoes Araujo"
__license__ = """
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/>.
"""
from operator import itemgetter
from data import *
from strategy import *
from error import Error
class RecommendationResult:
"""
Class designed to describe a recommendation result: items and scores.
"""
def __init__(self,item_score):
"""
Set initial parameters.
"""
self.item_score = item_score
def __str__(self):
"""
String representation of the object.
"""
result = self.get_prediction()
str = "\n"
for i in range(len(result)):
str += "%2d: %s\n" % (i,result[i][0])
return str
def get_prediction(self,size=20):
"""
Return prediction based on recommendation size (number of items).
"""
sorted_result = sorted(self.item_score.items(), key=itemgetter(1))
return reversed(sorted_result[:size])
class Recommender:
"""
Class designed to play the role of recommender.
"""
def __init__(self,cfg):
"""
Set initial parameters.
"""
try:
strategy = "self."+cfg.strategy+"(cfg)"
exec(strategy)
except (NameError, AttributeError, SyntaxError) as err:
print err
logging.critical("Could not perform recommendation strategy '%s'" %
cfg.strategy)
raise Error
def ct(self,cfg):
"""
Set recommender attributes to perform content-based recommendation
using tags index as source data.
"""
self.items_repository = TagsXapianIndex(cfg)
self.strategy = ContentBasedStrategy()
def cta(self,cfg):
"""
Set recommender attributes to perform content-based recommendation
using apt-xapian-index as source data.
"""
self.items_repository = xapian.Database(cfg.axi)
self.strategy = AxiContentBasedStrategy()
def col(self,cfg):
"""
Set recommender attributes to perform collaborative recommendation
using popcon-xapian-index as source data.
"""
self.users_repository = PopconXapianIndex(cfg)
self.strategy = CollaborativeStrategy()
def set_strategy(self,strategy):
"""
Set the recommendation strategy.
"""
self.strategy = strategy
def get_recommendation(self,user):
"""
Produces recommendation using previously loaded strategy.
"""
return self.strategy.run(self,user)