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 = "
" + err + "
"
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)