def calculate_pagerank_scores(ref_topic_map): from sefaria.pagesheetrank import pagerank_rank_ref_list pr_map = {} pr_seg_map = {} # keys are (topic, seg_tref). used for sheet relevance for topic, ref_list in tqdm(ref_topic_map.items(), desc='calculate pr'): oref_list = [] for tref in ref_list: try: oref_list += [Ref(tref)] except InputError: continue oref_pr_list = pagerank_rank_ref_list(oref_list, normalize=True) for oref, pr in oref_pr_list: pr_map[(topic, oref.normal())] = pr for seg_oref in oref.all_segment_refs(): pr_seg_map[(topic, seg_oref.normal())] = pr return pr_map, pr_seg_map
def calculate_pagerank_scores(ref_topic_map): from sefaria.pagesheetrank import pagerank_rank_ref_list from statistics import mean pr_map = {} pr_seg_map = {} # keys are (topic, seg_tref). used for sheet relevance for topic, ref_list in tqdm(ref_topic_map.items(), desc='calculate pr'): oref_list = [] for tref in ref_list: oref = get_ref_safely(tref) if oref is None: continue oref_list += [oref] seg_ref_map = { r.normal(): [rr.normal() for rr in r.all_segment_refs()] for r in oref_list } oref_pr_list = pagerank_rank_ref_list(oref_list, normalize=True, seg_ref_map=seg_ref_map) for oref, pr in oref_pr_list: pr_map[(topic, oref.normal())] = pr for seg_tref in seg_ref_map[oref.normal()]: pr_seg_map[(topic, seg_tref)] = pr return pr_map, pr_seg_map