-
Notifications
You must be signed in to change notification settings - Fork 9
<feat/recommendation> Implement a simple ranking model #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
a585cd9
80d0d29
21ae6d7
88f0db8
bcb671a
12e9966
a934450
991e26d
21a25ee
9a7046f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,66 @@ | ||||||
from copy import deepcopy | ||||||
from typing import List, Dict, Tuple | ||||||
|
||||||
from recommendation_system.ranking_layer.base import BaseRankingModel | ||||||
|
||||||
class SimpleRankingModel(BaseRankingModel): | ||||||
def __init__(self) -> None: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thx for annotating mypy typing! |
||||||
super().__init__() | ||||||
|
||||||
def rank(self, user_features: Dict, candidates: List[List[Dict]], top_k: int) -> List[Dict]: | ||||||
candidates = self.process_data(candidates=candidates) | ||||||
rankings = self.calc_candidate_score(candidates=candidates) | ||||||
Comment on lines
+10
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice, very clear! |
||||||
|
||||||
new_candidates = [] | ||||||
for item, score in rankings: | ||||||
new_item = self.get_item(item) | ||||||
new_item['score'] = score | ||||||
Comment on lines
+16
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you'd calculated score in |
||||||
new_candidates.append(new_item) | ||||||
|
||||||
return new_candidates[:top_k] | ||||||
|
||||||
def get_item(self, item): | ||||||
return deepcopy(self._items_mapping[item]) | ||||||
|
||||||
def calc_candidate_score(self, candidates): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
candidate_ranking = {} | ||||||
|
||||||
for item in self._items_mapping: | ||||||
candidate_ranking.setdefault(item, []) | ||||||
for ranker in candidates: | ||||||
if item in ranker: | ||||||
k, score = ranker.get(item) | ||||||
candidate_ranking[item].append(score/k) | ||||||
|
||||||
for item, scores in candidate_ranking.items(): | ||||||
candidate_ranking[item] = sum(scores) | ||||||
|
||||||
return sorted(candidate_ranking.items(), key=lambda d: d[1], reverse=True) | ||||||
|
||||||
def process_data(self, candidates: List[List[Dict]]) -> List[List[Tuple]]: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
items_mapping = {} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also, would you add |
||||||
new_candidates = [] | ||||||
|
||||||
for ranker in candidates: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why |
||||||
new_candidate_per_ranker = {} | ||||||
for i, candidate in enumerate(ranker): | ||||||
items_mapping.setdefault(candidate['title'], { | ||||||
'title': candidate['title'], | ||||||
'subtitle': candidate['subtitle'], | ||||||
'image_url': candidate['image_url'], | ||||||
'date': candidate['date'], | ||||||
'url': candidate['url'] | ||||||
}) | ||||||
new_candidate_per_ranker[candidate['title']] = (i + 1, candidate.get('score', 1)) # (rank, score) | ||||||
|
||||||
new_candidates.append(new_candidate_per_ranker) | ||||||
|
||||||
self._items_mapping = items_mapping | ||||||
|
||||||
return new_candidates | ||||||
|
||||||
def build_user_embedding(self, user_features): | ||||||
pass | ||||||
|
||||||
def calc_similarity(self, user_features): | ||||||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice! we have ranking right now~