File size: 5,425 Bytes
ced76fc
a779273
ced76fc
e7eeec5
ced76fc
 
a779273
 
ced76fc
 
 
 
 
a779273
 
ced76fc
 
 
 
 
a779273
 
 
ced76fc
 
 
 
 
a779273
 
ced76fc
 
 
 
 
 
 
 
a779273
 
 
ced76fc
 
 
 
a779273
0fbc7e6
 
a779273
ced76fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a779273
 
 
 
 
 
 
 
 
 
 
 
ced76fc
 
 
 
a779273
 
 
ced76fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a779273
 
 
 
ced76fc
 
 
 
 
0fbc7e6
 
a779273
 
e7eeec5
 
 
 
ced76fc
 
 
 
 
 
 
 
 
 
a779273
 
 
 
 
 
ced76fc
 
a779273
 
 
 
e7eeec5
a779273
 
 
e7eeec5
ced76fc
e7eeec5
 
ced76fc
a779273
 
 
ced76fc
 
 
 
 
 
 
 
 
a779273
 
 
 
 
 
 
 
ced76fc
 
e7eeec5
a779273
 
 
e7eeec5
a779273
 
 
e7eeec5
ced76fc
 
 
 
 
 
e7eeec5
a779273
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from abc import ABC

from modules.module_WordExplorer import WordExplorer
from modules.module_BiasExplorer import WordBiasExplorer, WEBiasExplorer2Spaces, WEBiasExplorer4Spaces
from typing import List, Tuple


class Connector(ABC):
    def parse_word(
        self, 
        word: str
    ) -> str:
        
        return word.lower().strip()

    def parse_words(
        self, 
        array_in_string: str
    ) -> List[str]:

        words = array_in_string.strip()
        if not words:
            return []

        words = [
            self.parse_word(word) 
            for word in words.split(',') if word.strip() != ''
        ]
        return words

    def process_error(
        self, 
        err: str
    ) -> str:

        if err:
            err = "<center><h3>" + err + "</h3></center>"
        return err
    

class WordExplorerConnector(Connector):
    def __init__(
        self, 
        **kwargs
    ) -> None:

        embedding = kwargs.get('embedding', None)
        if embedding is None:
            raise KeyError
        
        self.word_explorer = WordExplorer(
            embedding=embedding
        )

    def plot_proyection_2d( 
        self,
        wordlist_0: str,
        wordlist_1: str,
        wordlist_2: str,
        wordlist_3: str,
        wordlist_4: str,
        color_wordlist_0: str,
        color_wordlist_1: str,
        color_wordlist_2: str,
        color_wordlist_3: str,
        color_wordlist_4: str,
        n_alpha: float,
        fontsize: int,
        n_neighbors: int
    ) -> Tuple:

        err = ""
        neighbors_method = 'sklearn'
        wordlist_0 = self.parse_words(wordlist_0)
        wordlist_1 = self.parse_words(wordlist_1)
        wordlist_2 = self.parse_words(wordlist_2)
        wordlist_3 = self.parse_words(wordlist_3)
        wordlist_4 = self.parse_words(wordlist_4)

        if not (wordlist_0 or wordlist_1 or wordlist_2 or wordlist_1 or wordlist_4):
            err = self.process_error("Ingresa al menos 1 palabras para continuar")
            return None, err
        
        err = self.word_explorer.check_oov(
            [wordlist_0, wordlist_1, wordlist_2, wordlist_3, wordlist_4]
        )

        if err:
            return None, self.process_error(err)

        fig = self.word_explorer.plot_projections_2d(
            wordlist_0,
            wordlist_1,
            wordlist_2,
            wordlist_3,
            wordlist_4,
            color_wordlist_0=color_wordlist_0,
            color_wordlist_1=color_wordlist_1,
            color_wordlist_2=color_wordlist_2,
            color_wordlist_3=color_wordlist_3,
            color_wordlist_4=color_wordlist_4,
            n_alpha=n_alpha,
            fontsize=fontsize,
            n_neighbors=n_neighbors,
            nn_method = neighbors_method
        )

        return fig, self.process_error(err)

class BiasWordExplorerConnector(Connector):

    def __init__(
        self, 
        **kwargs
    ) -> None:

        embedding = kwargs.get('embedding', None)
        if embedding is None:
            raise KeyError

        self.bias_word_explorer_2_spaces = WEBiasExplorer2Spaces(
            embedding=embedding
        )
        self.bias_word_explorer_4_spaces = WEBiasExplorer4Spaces(
            embedding=embedding
        )

    def calculate_bias_2d(
        self,
        wordlist_1: str,
        wordlist_2: str,
        to_diagnose_list: str
    ) -> Tuple:

        err = ""
        wordlist_1 = self.parse_words(wordlist_1)
        wordlist_2 = self.parse_words(wordlist_2)
        to_diagnose_list = self.parse_words(to_diagnose_list)

        word_lists = [wordlist_1, wordlist_2, to_diagnose_list]
        for _list in word_lists:
            if not _list:
                err = "Debe ingresar al menos 1 palabra en las lista de palabras a diagnosticar, sesgo 1 y sesgo 2"
        if err:
            return None, self.process_error(err)

        err = self.bias_word_explorer_2_spaces.check_oov(word_lists)
        if err:
            return None, self.process_error(err)

        fig = self.bias_word_explorer_2_spaces.calculate_bias(
            to_diagnose_list, 
            wordlist_1, 
            wordlist_2
        )

        return fig, self.process_error(err)

    def calculate_bias_4d(
        self,
        wordlist_1: str,
        wordlist_2: str,
        wordlist_3: str,
        wordlist_4: str,
        to_diagnose_list: str
    ) -> Tuple:

        err = ""
        wordlist_1 = self.parse_words(wordlist_1)
        wordlist_2 = self.parse_words(wordlist_2)
        wordlist_3 = self.parse_words(wordlist_3)
        wordlist_4 = self.parse_words(wordlist_4)
        to_diagnose_list = self.parse_words(to_diagnose_list)

        wordlists = [wordlist_1, wordlist_2, wordlist_3, wordlist_4, to_diagnose_list]
        for _list in wordlists:
            if not _list:
                err = "Debe ingresar al menos 1 palabra en todas las listas para graficar en 4 espacios"
        if err:
            return None, self.process_error(err)

        err = self.bias_word_explorer_4_spaces.check_oov(wordlists)
        if err:
            return None, self.process_error(err)

        fig = self.bias_word_explorer_4_spaces.calculate_bias(
            to_diagnose_list, 
            wordlist_1, 
            wordlist_2, 
            wordlist_3, 
            wordlist_4
        )
        
        return fig, self.process_error(err)