Introducción a Microsoft Semantic Kernel
Introducción
La calidad de los datos es crucial para sistemas de IA. En este tutorial aprenderás técnicas de normalización y preprocesamiento que mejoran significativamente los resultados de tus modelos.
Normalización de Texto
public class TextNormalizer
{
public string Normalize(string text)
{
if (string.IsNullOrWhiteSpace(text))
return string.Empty;
// 1. Lowercase
text = text.ToLowerInvariant();
// 2. Remover acentos
text = RemoveAccents(text);
// 3. Remover caracteres especiales
text = RemoveSpecialCharacters(text);
// 4. Normalizar espacios
text = NormalizeWhitespace(text);
return text.Trim();
}
private string RemoveAccents(string text)
{
var normalizedString = text.Normalize(NormalizationForm.FormD);
var stringBuilder = new StringBuilder();
foreach (var c in normalizedString)
{
var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
if (unicodeCategory != UnicodeCategory.NonSpacingMark)
{
stringBuilder.Append(c);
}
}
return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
}
}
Tokenización
public class Tokenizer
{
private readonly HashSet<string> _stopWords = new()
{
"el", "la", "de", "que", "y", "a", "en", "un", "ser", "se", "no"
};
public List<string> Tokenize(string text, bool removeStopWords = true)
{
var tokens = text
.Split(new[] { ' ', '\t', '\n', '\r', ',', '.', ';', ':', '!', '?' },
StringSplitOptions.RemoveEmptyEntries)
.Select(t => t.ToLowerInvariant())
.Where(t => t.Length > 2);
if (removeStopWords)
{
tokens = tokens.Where(t => !_stopWords.Contains(t));
}
return tokens.ToList();
}
}
Limpieza de Datos
public class DataCleaner
{
public string CleanBusinessHours(string hours)
{
// Normalizar horarios de negocio
// "Lunes a Viernes: 9-14h" → "L-V: 09:00-14:00"
return hours;
}
public string CleanPhoneNumber(string phone)
{
// Normalizar números de teléfono
// "+34 666 123 456" → "666123456"
return Regex.Replace(phone, @"[^\d]", "");
}
}
Mejores Prácticas
- Normalizar antes de generar embeddings
- Mantener consistencia en todo el pipeline
- Documentar transformaciones aplicadas
- Validar datos antes y después de normalizar
Palabras clave: data normalization, text preprocessing, tokenization, data cleaning, NLP
Share this content: