Normalizacja ciągów znaków

Czyszczenie danych w Javie

Dennis Lee

Software Engineer

Zestaw danych inwentarza sklepu spożywczego

  • Praca z zestawem danych inwentarza sklepu spożywczego przez cały rozdział
  • Modyfikacja wybranych kolumn, np. nazwy produktu, w celu zilustrowania przekształceń danych

 

Product Name Category Status Date Received Quantity
Eggplant Fruits & Vegetables Discontinued 3/1/25 46
VEGETABLE OIL Oils & Fats Backordered 4/1/25 51
Cheese Dairy Active 6/1/25 78
Fresh (Organic) *Carrots* Fruits & Vegetables Discontinued 5/1/25 51
Bell Pepper Fresh Fruits & Vegetables Active 5/2/25 67
Czyszczenie danych w Javie

Dlaczego normalizacja ciągów znaków jest ważna

  • Nieczyste ciągi znaków: komputer nie rozpoznaje tekstu
  • Przykład: inwentarz sklepu spożywczego
  • Rozwiązanie: normalizacja ciągów znaków

 

String[] messyProducts = {
    "Eggplant ",              // Extra whitespace at the end
    "VEGETABLE OIL",             // Inconsistent case
    "Fresh (Organic) *Carrots*", // Special characters
    "Bell   Pepper    Fresh",    // Extra whitespace between words
};
Czyszczenie danych w Javie

Usuwanie wiodących i końcowych spacji

String[] products = {"Eggplant ", "  Vegetable Oil", " Cheese "};
for (String product : products) {
    String cleaned = product.trim(); // Removes leading/trailing whitespace
    System.out.println(cleaned);
}
Eggplant
Vegetable Oil
Cheese
Czyszczenie danych w Javie

Ujednolicanie wielkości liter

List<String> products = Arrays.asList("Eggplant", "VEGETABLE OIL", "cheese");

products.stream()
.map(String::toLowerCase) // Convert all to lowercase
.forEach(System.out::println); // Print each product
eggplant
vegetable oil
cheese
Czyszczenie danych w Javie

Wzorce regex

  • Wyrażenia regularne (regex): specjalne wzorce dopasowujące tekst$^1$
  • [^a-zA-Z\\s]: Znajduje każdy znak, który nie jest literą ani spacją
// This pattern matches any character that is NOT:
[       // Start a character set
^       // NOT - match anything not in this set
a-z     // any lowercase letter
A-Z     // any uppercase letter
\\s     // any whitespace character (need extra \ for Java to interpret \s)
]       // End character set

1 https://www.datacamp.com/cheat-sheet/regular-expresso
Czyszczenie danych w Javie

Usuwanie znaków specjalnych

String dirtyName = "Fresh (Organic) *Carrots*"; // Special characters: (, ), *

String cleaned = dirtyName.replaceAll("[^a-zA-Z\\s]", ""); // Remove non-letters
System.out.println(cleaned); // Output: "Fresh Organic Carrots"
Fresh Organic Carrots
Czyszczenie danych w Javie

Usuwanie nadmiarowych spacji

import java.util.regex.Pattern;
Pattern pattern = Pattern.compile("\\s+"); // Match multiple spaces

String messyProduct = "Bell Pepper Fresh"; // Contains extra spaces // Replace multiple spaces with single space String cleanedProduct = pattern.matcher(messyProduct).replaceAll(" ");
System.out.println(cleanedProduct);
Bell Pepper Fresh
Czyszczenie danych w Javie

Łączenie wszystkich kroków

List<String> messyProducts = Arrays.asList(
        "Eggplant ", "VEGETABLE OIL",
        "Fresh (Organic) *Carrots*", "Bell   Pepper    Fresh"
); // Product names extracted from our grocery inventory dataset

messyProducts.stream()
        .map(s -> s.trim()                       // Fix outer spaces
                .replaceAll("[^a-zA-Z\\s]", "")  // Remove special chars
                .replaceAll("\\s+", " ")         // Fix inner spaces
                .toLowerCase())                  // Standardize case
        .forEach(System.out::println);
Czyszczenie danych w Javie

Łączenie kroków: wyniki

eggplant
vegetable oil
fresh organic carrots
bell pepper fresh
Czyszczenie danych w Javie

Czas na ćwiczenia!

Czyszczenie danych w Javie

Preparing Video For Download...