(
rows: List[Dict[str, Any]],
label_rows: List[Dict[str, Any]],
*,
top_k: int,
roles_file: Path,
input_dir: Path,
)
| 731 | |
| 732 | |
| 733 | def rerank_episodes( |
| 734 | rows: List[Dict[str, Any]], |
| 735 | label_rows: List[Dict[str, Any]], |
| 736 | *, |
| 737 | top_k: int, |
| 738 | roles_file: Path, |
| 739 | input_dir: Path, |
| 740 | ) -> Tuple[List[Dict[str, Any]], Dict[str, Any]]: |
| 741 | roles = load_roles(roles_file) |
| 742 | user_metadata = load_user_metadata(input_dir) |
| 743 | states = build_user_states(user_metadata, roles, rows) |
| 744 | label_map = build_label_map(label_rows) |
| 745 | |
| 746 | output_rows: List[Dict[str, Any]] = [] |
| 747 | grouped = group_by_episode(rows) |
| 748 | method_stats = { |
| 749 | "method_key": METHOD_KEY, |
| 750 | "method_name": METHOD_NAME, |
| 751 | "episodes": 0, |
| 752 | "top_k": top_k, |
| 753 | "uses_dynamic_profile": False, |
| 754 | "uses_reading_reports": False, |
| 755 | "uses_live_entity_api": False, |
| 756 | "users": {}, |
| 757 | } |
| 758 | |
| 759 | for episode_id, episode_rows in sorted(grouped.items(), key=episode_sort_key): |
| 760 | if not episode_rows: |
| 761 | continue |
| 762 | user_id = str(episode_rows[0].get("user_id") or "").strip() |
| 763 | if user_id not in states: |
| 764 | states[user_id] = UserKnowledgeEntityState(profile_text="", profile_entities=[]) |
| 765 | state = states[user_id] |
| 766 | |
| 767 | entity_texts = [entities_text(extract_entities(row)) for row in episode_rows] |
| 768 | day_texts = [paper_text(row) for row in episode_rows] |
| 769 | idf = build_idf_from_texts([ |
| 770 | state.profile_text, |
| 771 | entities_text(state.profile_entities), |
| 772 | *state.feedback_texts(), |
| 773 | *entity_texts, |
| 774 | *day_texts, |
| 775 | ]) |
| 776 | state.prepare_vectors(idf) |
| 777 | max_log_citations = max_log_citations_for_day(episode_rows) |
| 778 | |
| 779 | scored_rows: List[Tuple[float, str, Dict[str, Any], Dict[str, Any]]] = [] |
| 780 | for row in episode_rows: |
| 781 | identity = paper_identity(row) |
| 782 | rep = build_paper_representation(row, idf) |
| 783 | score_payload = score_row( |
| 784 | row, |
| 785 | rep, |
| 786 | state, |
| 787 | max_log_citations=max_log_citations, |
| 788 | ) |
| 789 | scored_rows.append((score_payload["system_score"], identity, row, score_payload)) |
| 790 |
no test coverage detected