File size: 3,172 Bytes
e8aad19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e9956d
 
 
 
 
e8aad19
8e9956d
e8aad19
 
8e9956d
e8aad19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from typing import List, Dict

class CustomPllLabel:
    def __init__(
        self
    ) -> None:

        self.html_head = """
        <html>
            <head>
                <meta charset="utf-8">
                <meta name="viewport" content="width=device-width, initial-scale=1">
                <style>
                    progress {
                        -webkit-appearance: none;
                    }
                    progress::-webkit-progress-bar {
                        background-color: #666;
                        border-radius: 7px;
                    }
                    #myturn span {
                        position: absolute;
                        display: inline-block;
                        color: #fff;
                        text-align: right;
                        font-size:15px
                    }
                    #myturn {
                        display: block;
                        position: relative;
                        margin: auto;
                        width: 90%;
                        padding: 2px;
                    }
                    progress {
                        width:100%;
                        height:20px;
                        border-radius: 7px;
                    }
                </style>
            </head>
            <body>
        """

        self.html_footer ="</body></html>"
    
    def __progressbar(
        self, 
        percentage: int, 
        sent: str, 
        ratio: float, 
        score: float, 
        size: int=15
    ) -> str:

        html = f"""
        <div id="myturn">
            <span data-value="{percentage/2}" style="width:{percentage/2}%;">
                <strong>x{round(ratio,3)}</strong>
            </span>
            <progress value="{percentage}" max="100"></progress>
            <p style='font-size:22px; padding:2px;'>{sent}</p>
        </div>
        """
        return html

    def __render(
        self, 
        sents: List[str],
        scores: List[float], 
        ratios: List[float]
    ) -> str:

        max_ratio = max(ratios)
        ratio2percentage = lambda ratio: int(ratio*100/max_ratio)

        html = ""
        for sent, ratio, score in zip(sents, ratios, scores):
            html += self.__progressbar(
                percentage=ratio2percentage(ratio), 
                sent=sent,
                ratio=ratio, 
                score=score
            )

        return self.html_head + html + self.html_footer
    
    def __getProportions(
        self, 
        scores: List[float], 
    ) -> List[float]:
    
        min_score = min(scores)
        return [min_score/s for s in scores]

    def compute(
        self, 
        pll_dict: Dict[str, float]
    ) -> str:

        sorted_pll_dict = sorted(pll_dict.items(), key=lambda x: x[1], reverse=True)

        sents = [k for k,_ in sorted_pll_dict]
        scores  = [v for _,v in sorted_pll_dict]

        # Scape < and > marks from hightlight word/s
        sents = [s.replace("<","&#60;").replace(">","&#62;") for s in sents]

        ratios = self.__getProportions(scores)
        
        return self.__render(sents, scores, ratios)