(
rows: List[Dict[str, Any]],
label_rows: List[Dict[str, Any]],
*,
top_k: int,
roles_file: Path,
input_dir: Path,
)
| 648 | |
| 649 | |
| 650 | def rerank_episodes( |
| 651 | rows: List[Dict[str, Any]], |
| 652 | label_rows: List[Dict[str, Any]], |
| 653 | *, |
| 654 | top_k: int, |
| 655 | roles_file: Path, |
| 656 | input_dir: Path, |
| 657 | ) -> Tuple[List[Dict[str, Any]], Dict[str, Any]]: |
| 658 | roles = load_roles(roles_file) |
| 659 | user_metadata = load_user_metadata(input_dir) |
| 660 | states = build_user_states(user_metadata, roles, rows) |
| 661 | label_map = build_label_map(label_rows) |
| 662 | |
| 663 | output_rows: List[Dict[str, Any]] = [] |
| 664 | grouped = group_by_episode(rows) |
| 665 | method_stats = { |
| 666 | "method_key": METHOD_KEY, |
| 667 | "method_name": METHOD_NAME, |
| 668 | "episodes": 0, |
| 669 | "top_k": top_k, |
| 670 | "uses_reading_reports": False, |
| 671 | "users": {}, |
| 672 | } |
| 673 | |
| 674 | for episode_id, episode_rows in sorted(grouped.items(), key=episode_sort_key): |
| 675 | if not episode_rows: |
| 676 | continue |
| 677 | user_id = str(episode_rows[0].get("user_id") or "").strip() |
| 678 | if user_id not in states: |
| 679 | states[user_id] = UserDiscourseState(profile_text="") |
| 680 | state = states[user_id] |
| 681 | |
| 682 | facet_texts: List[str] = [] |
| 683 | for row in episode_rows: |
| 684 | facet_texts.extend(extract_discourse_facets(row).values()) |
| 685 | idf = build_idf_from_texts([state.profile_text, *state.feedback_texts(), *facet_texts]) |
| 686 | state.prepare_vectors(idf) |
| 687 | |
| 688 | scored_rows: List[Tuple[float, str, Dict[str, Any], Dict[str, Any]]] = [] |
| 689 | for row in episode_rows: |
| 690 | identity = paper_identity(row) |
| 691 | score_payload = score_row(row, idf, state) |
| 692 | scored_rows.append((score_payload["system_score"], identity, row, score_payload)) |
| 693 | |
| 694 | scored_rows.sort( |
| 695 | key=lambda item: ( |
| 696 | -float(item[0]), |
| 697 | str(item[2].get("date") or ""), |
| 698 | str(item[2].get("title") or ""), |
| 699 | hashlib.sha1(item[1].encode("utf-8")).hexdigest(), |
| 700 | ) |
| 701 | ) |
| 702 | |
| 703 | for rank, (_, _, row, score_payload) in enumerate(scored_rows, start=1): |
| 704 | label = label_map.get(label_key(row), {}) |
| 705 | output_rows.append(clone_output_row(row, label, score_payload, rank, top_k)) |
| 706 | |
| 707 | update_feedback_state(state, episode_rows, label_map) |
no test coverage detected