@@ -49,7 +49,8 @@ def score_results(results):
4949 flat_len = len(flat_res)
5050 engines_len = len(results)
5151 results = []
52− # deduplication + scoring
52+
53+ # pass 1: deduplication + scoring
5354 for i, res in enumerate(flat_res):
5455
5556 res['parsed_url'] = urlparse(res['url'])
@@ -90,7 +91,42 @@ def score_results(results):
9091 else:
9192 res['score'] = score
9293 results.append(res)
93− return sorted(results, key=itemgetter('score'), reverse=True)
94+ results = sorted(results, key=itemgetter('score'), reverse=True)
95+
96+ # pass 2 : group results by category and template
97+ gresults = []
98+ categoryPositions = {}
99+
100+ for i, res in enumerate(results):
101+ # FIXME : handle more than one category per engine
102+ category = engines[res['engine']].categories[0] + ':' + '' if 'template' not in res else res['template']
103+
104+ current = None if category not in categoryPositions else categoryPositions[category]
105+
106+ # group with previous results using the same category if the group can accept more result and is not too far from the current position
107+ if current != None and (current['count'] > 0) and (len(gresults) - current['index'] < 20):
108+ # group with the previous results using the same category with this one
109+ index = current['index']
110+ gresults.insert(index, res)
111+
112+ # update every index after the current one (including the current one)
113+ for k in categoryPositions:
114+ v = categoryPositions[k]['index']
115+ if v >= index:
116+ categoryPositions[k]['index'] = v+1
117+
118+ # update this category
119+ current['count'] -= 1
120+
121+ else:
122+ # same category
123+ gresults.append(res)
124+
125+ # update categoryIndex
126+ categoryPositions[category] = { 'index' : len(gresults), 'count' : 8 }
127+
128+ # return gresults
129+ return gresults
94130
95131
96132class Search(object):