import numpy as np
import pandas as pd
import gradio as gr
from abc import ABC, abstractmethod
from modules.module_WordExplorer import WordExplorer
from modules.module_BiasExplorer import WordBiasExplorer
class Connector(ABC):
def parse_word(self, word : str):
return word.lower().strip()
def parse_words(self, array_in_string : 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):
if err is None:
return
return "
" + err + "
"
class WordExplorerConnector(Connector):
def __init__(self, **kwargs):
if 'embedding' in kwargs:
embedding = kwargs.get('embedding')
else:
raise KeyError
self.word_explorer = WordExplorer(embedding)
def plot_proyection_2d( self,
wordlist_0,
wordlist_1,
wordlist_2,
wordlist_3,
wordlist_4,
color_wordlist_0,
color_wordlist_1,
color_wordlist_2,
color_wordlist_3,
color_wordlist_4,
n_alpha,
fontsize,
n_neighbors
):
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):
if 'embedding' in kwargs:
embedding = kwargs.get('embedding')
else:
raise KeyError
self.bias_word_explorer = WordBiasExplorer(embedding)
def calculate_bias_2d(self,
wordlist_1,
wordlist_2,
to_diagnose_list
):
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.check_oov(word_lists)
if err:
return None, self.process_error(err)
fig = self.bias_word_explorer.plot_biased_words(to_diagnose_list, wordlist_2, wordlist_1)
return fig, self.process_error(err)
def calculate_bias_4d(self,
wordlist_1,
wordlist_2,
wordlist_3,
wordlist_4,
to_diagnose_list
):
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 = "¡Para graficar con 4 espacios, debe ingresar al menos 1 palabra en todas las listas!"
if err:
return None, self.process_error(err)
err = self.bias_word_explorer.check_oov(wordlists)
if err:
return None, self.process_error(err)
fig = self.bias_word_explorer.plot_biased_words(to_diagnose_list, wordlist_1, wordlist_2, wordlist_3, wordlist_4)
return fig, self.process_error(err)