(
    candidates: List[Candidate],
    term_weights: Dict[str, float],
    window_size: int,
    n_toks: int,
)
return norm_edge_weights
Compute weights between candidates that occur within a sliding window(s) of
    each other, then combine with statistical ``term_weights`` and normalize
    by the total number of outgoing edge weights.
Compute weights between candidates that occur within a sliding window(s) of
    each other, then combine with statistical ``term_weights`` and normalize
    by the total number of outgoing edge weights.
Compute weights between candidates that occur within a sliding window ( s ) of each other then combine with statistical term_weights and normalize by the total number of outgoing edge weights .
def _compute_edge_weights(
    candidates: List[Candidate],
    term_weights: Dict[str, float],
    window_size: int,
    n_toks: int,
) -> List[Tuple[str, str, Dict[str, float]]]:
    """
    Compute weights between candidates that occur within a sliding window(s) of
    each other, then combine with statistical ``term_weights`` and normalize
    by the total number of outgoing edge weights.
    """
    n_coocs = collections.defaultdict(lambda: collections.defaultdict(int))
    sum_logdists = collections.defaultdict(lambda: collections.defaultdict(float))
    # iterate over windows
    log_ = math.log  # localize this, for performance
    for start_idx in range(n_toks):
        end_idx = start_idx + window_size
        window_cands = (cand for cand in candidates if start_idx <= cand.idx < end_idx)
        # get all token combinations within window
        for c1, c2 in itertools.combinations(window_cands, 2):
            n_coocs[c1.text][c2.text] += 1
            try:
                sum_logdists[c1.text][c2.text] += log_(
                    window_size / abs(c1.idx - c2.idx)
                )
            except ZeroDivisionError:
                sum_logdists[c1.text][c2.text] += log_(window_size)
        if end_idx >= n_toks:
            break
    # compute edge weights between co-occurring terms (nodes)
    edge_weights = collections.defaultdict(lambda: collections.defaultdict(float))
    for c1, c2_dict in sum_logdists.items():
        for c2, sum_logdist in c2_dict.items():
            edge_weights[c1][c2] = (
                ((1.0 + sum_logdist) / n_coocs[c1][c2])
                * term_weights[c1]
                * term_weights[c2]
            )
    # normalize edge weights by sum of outgoing edge weights per term (node)
    norm_edge_weights: List[Tuple[str, str, Dict[str, float]]] = []
    for c1, c2s in edge_weights.items():
        sum_edge_weights = sum(c2s.values())
        norm_edge_weights.extend(
            (c1, c2, {"weight": weight / sum_edge_weights}) for c2, weight in c2s.items()
        )
    return norm_edge_weights
def _compute_edge_weights ( candidates : List [ Candidate ] , term_weights : Dict [ str , float ] , window_size : int , n_toks : int , ) -> List [ Tuple [ str , str , Dict [ str , float ] ] ] : n_coocs = collections . defaultdict ( lambda : collections . defaultdict ( int ) ) sum_logdists = collections . defaultdict ( lambda : collections . defaultdict ( float ) ) # iterate over windows log_ = math . log # localize this, for performance for start_idx in range ( n_toks ) : end_idx = start_idx + window_size window_cands = ( cand for cand in candidates if start_idx <= cand . idx < end_idx ) # get all token combinations within window for c1 , c2 in itertools . combinations ( window_cands , 2 ) : n_coocs [ c1 . text ] [ c2 . text ] += 1 try : sum_logdists [ c1 . text ] [ c2 . text ] += log_ ( window_size / abs ( c1 . idx - c2 . idx ) ) except ZeroDivisionError : sum_logdists [ c1 . text ] [ c2 . text ] += log_ ( window_size ) if end_idx >= n_toks : break # compute edge weights between co-occurring terms (nodes) edge_weights = collections . defaultdict ( lambda : collections . defaultdict ( float ) ) for c1 , c2_dict in sum_logdists . items ( ) : for c2 , sum_logdist in c2_dict . items ( ) : edge_weights [ c1 ] [ c2 ] = ( ( ( 1.0 + sum_logdist ) / n_coocs [ c1 ] [ c2 ] ) * term_weights [ c1 ] * term_weights [ c2 ] ) # normalize edge weights by sum of outgoing edge weights per term (node) norm_edge_weights : List [ Tuple [ str , str , Dict [ str , float ] ] ] = [ ] for c1 , c2s in edge_weights . items ( ) : sum_edge_weights = sum ( c2s . values ( ) ) norm_edge_weights . extend ( ( c1 , c2 , { "weight" : weight / sum_edge_weights } ) for c2 , weight in c2s . items ( ) ) return norm_edge_weights