1
1
from collections import Counter
2
- from typing import List , Optional
2
+ from typing import Optional
3
3
4
4
import numpy as np
5
5
6
- from eli5 .base import TargetExplanation , WeightedSpans , DocWeightedSpans
6
+ from eli5 .base import TargetExplanation , DocWeightedSpans
7
7
from eli5 .base_utils import attrs
8
8
from eli5 .utils import max_or_0
9
9
10
10
11
- def get_char_weights (doc_weighted_spans , preserve_density = None ):
12
- # type: (DocWeightedSpans, Optional[bool]) -> np.ndarray
11
+ def get_char_weights (
12
+ doc_weighted_spans : DocWeightedSpans , preserve_density : Optional [bool ] = None ,
13
+ ) -> np .ndarray :
13
14
""" Return character weights for a text document with highlighted features.
14
15
If preserve_density is True, then color for longer fragments will be
15
16
less intensive than for shorter fragments, so that "sum" of intensities
@@ -35,11 +36,10 @@ def get_char_weights(doc_weighted_spans, preserve_density=None):
35
36
@attrs
36
37
class PreparedWeightedSpans (object ):
37
38
def __init__ (self ,
38
- doc_weighted_spans , # type : DocWeightedSpans
39
- char_weights , # type : np.ndarray
40
- weight_range , # type : float
39
+ doc_weighted_spans : DocWeightedSpans ,
40
+ char_weights : np .ndarray ,
41
+ weight_range : float ,
41
42
):
42
- # type: (...) -> None
43
43
self .doc_weighted_spans = doc_weighted_spans
44
44
self .char_weights = char_weights
45
45
self .weight_range = weight_range
@@ -55,25 +55,24 @@ def __eq__(self, other):
55
55
return False
56
56
57
57
58
- def prepare_weighted_spans (targets , # type: List[TargetExplanation]
59
- preserve_density = None , # type: Optional[bool]
60
- ):
61
- # type: (...) -> List[Optional[List[PreparedWeightedSpans]]]
58
+ def prepare_weighted_spans (targets : list [TargetExplanation ],
59
+ preserve_density : Optional [bool ] = None ,
60
+ ) -> list [Optional [list [PreparedWeightedSpans ]]]:
62
61
""" Return weighted spans prepared for rendering.
63
62
Calculate a separate weight range for each different weighted
64
63
span (for each different index): each target has the same number
65
64
of weighted spans.
66
65
"""
67
- targets_char_weights = [
66
+ targets_char_weights : list [ Optional [ list [ np . ndarray ]]] = [
68
67
[get_char_weights (ws , preserve_density = preserve_density )
69
68
for ws in t .weighted_spans .docs_weighted_spans ]
70
69
if t .weighted_spans else None
71
- for t in targets ] # type: List[Optional[List[np.ndarray]]]
70
+ for t in targets ]
72
71
max_idx = max_or_0 (len (ch_w or []) for ch_w in targets_char_weights )
73
72
74
- targets_char_weights_not_None = [
73
+ targets_char_weights_not_None : list [ list [ np . ndarray ]] = [
75
74
cw for cw in targets_char_weights
76
- if cw is not None ] # type: List[List[np.ndarray]]
75
+ if cw is not None ]
77
76
78
77
spans_weight_ranges = [
79
78
max_or_0 (
0 commit comments