Spaces:
Running
Running
Update modules/text_analysis/stopwords.py
Browse files
modules/text_analysis/stopwords.py
CHANGED
|
@@ -102,3 +102,87 @@ def get_custom_stopwords(lang_code: str) -> Set[str]:
|
|
| 102 |
# Obtener stopwords del idioma especificado o devolver conjunto vacío si no existe
|
| 103 |
return stopwords_dict.get(lang_code, set())
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
# Obtener stopwords del idioma especificado o devolver conjunto vacío si no existe
|
| 103 |
return stopwords_dict.get(lang_code, set())
|
| 104 |
|
| 105 |
+
def process_text(text: str, lang_code: str, nlp) -> List[str]:
|
| 106 |
+
"""
|
| 107 |
+
Procesa un texto completo, removiendo stopwords, símbolos y números.
|
| 108 |
+
|
| 109 |
+
Args:
|
| 110 |
+
text (str): Texto a procesar
|
| 111 |
+
lang_code (str): Código del idioma ('es', 'en', 'fr')
|
| 112 |
+
nlp: Modelo de spaCy cargado
|
| 113 |
+
|
| 114 |
+
Returns:
|
| 115 |
+
List[str]: Lista de tokens procesados
|
| 116 |
+
"""
|
| 117 |
+
try:
|
| 118 |
+
# Obtener stopwords personalizadas
|
| 119 |
+
custom_stopwords = get_custom_stopwords(lang_code)
|
| 120 |
+
|
| 121 |
+
# Procesar el texto con spaCy
|
| 122 |
+
doc = nlp(text)
|
| 123 |
+
|
| 124 |
+
# Filtrar y procesar tokens
|
| 125 |
+
processed_tokens = []
|
| 126 |
+
for token in doc:
|
| 127 |
+
# Convertir a minúsculas y obtener el lema
|
| 128 |
+
lemma = token.lemma_.lower()
|
| 129 |
+
|
| 130 |
+
# Aplicar filtros
|
| 131 |
+
if (len(lemma) >= 2 and # Longitud mínima
|
| 132 |
+
lemma not in custom_stopwords and # No es stopword
|
| 133 |
+
not token.is_punct and # No es puntuación
|
| 134 |
+
not token.is_space and # No es espacio
|
| 135 |
+
lemma not in SYMBOLS_AND_NUMBERS and # No es símbolo o número
|
| 136 |
+
not any(char in string.punctuation for char in lemma) and # No contiene puntuación
|
| 137 |
+
not any(char.isdigit() for char in lemma)): # No contiene números
|
| 138 |
+
|
| 139 |
+
processed_tokens.append(lemma)
|
| 140 |
+
|
| 141 |
+
return processed_tokens
|
| 142 |
+
|
| 143 |
+
except Exception as e:
|
| 144 |
+
logger.error(f"Error en process_text: {str(e)}")
|
| 145 |
+
return []
|
| 146 |
+
|
| 147 |
+
def clean_text(text: str) -> str:
|
| 148 |
+
"""
|
| 149 |
+
Limpia un texto removiendo caracteres especiales y normalizando espacios.
|
| 150 |
+
|
| 151 |
+
Args:
|
| 152 |
+
text (str): Texto a limpiar
|
| 153 |
+
|
| 154 |
+
Returns:
|
| 155 |
+
str: Texto limpio
|
| 156 |
+
"""
|
| 157 |
+
# Remover caracteres especiales y números
|
| 158 |
+
cleaned = ''.join(char for char in text if char not in SYMBOLS_AND_NUMBERS)
|
| 159 |
+
|
| 160 |
+
# Normalizar espacios
|
| 161 |
+
cleaned = ' '.join(cleaned.split())
|
| 162 |
+
|
| 163 |
+
return cleaned.strip()
|
| 164 |
+
|
| 165 |
+
def get_stopwords_for_spacy(lang_code: str, nlp) -> Set[str]:
|
| 166 |
+
"""
|
| 167 |
+
Combina stopwords personalizadas con las de spaCy.
|
| 168 |
+
|
| 169 |
+
Args:
|
| 170 |
+
lang_code (str): Código del idioma
|
| 171 |
+
nlp: Modelo de spaCy
|
| 172 |
+
|
| 173 |
+
Returns:
|
| 174 |
+
Set[str]: Conjunto combinado de stopwords
|
| 175 |
+
"""
|
| 176 |
+
custom_stops = get_custom_stopwords(lang_code)
|
| 177 |
+
spacy_stops = nlp.Defaults.stop_words if hasattr(nlp.Defaults, 'stop_words') else set()
|
| 178 |
+
|
| 179 |
+
return custom_stops.union(spacy_stops)
|
| 180 |
+
|
| 181 |
+
# Asegúrate de exportar todas las funciones necesarias
|
| 182 |
+
__all__ = [
|
| 183 |
+
'get_custom_stopwords',
|
| 184 |
+
'process_text',
|
| 185 |
+
'clean_text',
|
| 186 |
+
'get_stopwords_for_spacy',
|
| 187 |
+
'SYMBOLS_AND_NUMBERS'
|
| 188 |
+
]
|