from __future__ import annotations import numpy as np from app.core.schemas import TopEdge def compute_outgoing_importance(matrix: np.ndarray) -> list[float]: sentence_count = matrix.shape[0] scores: list[float] = [] for source_idx in range(sentence_count): column = matrix[source_idx + 1 :, source_idx] scores.append(float(column.mean()) if column.size else 0.0) return scores def compute_incoming_importance(matrix: np.ndarray) -> list[float]: sentence_count = matrix.shape[0] scores: list[float] = [] for target_idx in range(sentence_count): row = matrix[target_idx, :target_idx] scores.append(float(row.mean()) if row.size else 0.0) return scores def compute_top_edges(matrix: np.ndarray, top_k: int = 10) -> list[TopEdge]: sentence_count = matrix.shape[0] candidates: list[TopEdge] = [] for target_idx in range(sentence_count): for source_idx in range(target_idx): candidates.append( TopEdge( source_sentence_idx=source_idx, target_sentence_idx=target_idx, score=float(matrix[target_idx, source_idx]), ) ) candidates.sort(key=lambda edge: edge.score, reverse=True) return candidates[:top_k]