Categorische standaardisatie

Data opschonen in Java

Dennis Lee

Software Engineer

Waarom categorieën standaardiseren?

  • Inconsistente categorienamen: groeperen lukt niet
  • Verschillende schrijfwijzen voor "Fruits & Vegetables"
  • Oplossing: categorieën standaardiseren

 

 

Rauwe categorie Aantal
Fruits & Vegetables 10
fruits & veg 20
Fruits&Veg 30
Data opschonen in Java

Stappen voor standaardisatie

  1. Definieer standaardcategorieën
    FRUITS_AND_VEGETABLES
    DAIRY
    UNCATEGORIZED
    
  2. Koppel varianten aan standaardcategorieën
    Fruits & Vegetables -> FRUITS_AND_VEGETABLES
    fruits & veg -> FRUITS_AND_VEGETABLES
    Fruits&Veg -> FRUITS_AND_VEGETABLES
    
  3. Voer analyse uit, bijv. tel FRUITS_AND_VEGETABLES
Data opschonen in Java

Standaardcategorieën definiëren met enum

// Define standard categories
public enum ProductCategory {
    FRUITS_AND_VEGETABLES,
    DAIRY,
    UNCATEGORIZED;
}

// Example category
System.out.println(ProductCategory.FRUITS_AND_VEGETABLES);
FRUITS_AND_VEGETABLES
Data opschonen in Java

Rauwe categorieën mappen naar standaardcategorieën

import java.util.HashMap;
import java.util.Map;
// Define map of raw category variations to standard categories
Map<String, ProductCategory> rawCategories = new HashMap<>();
// Variation 1: Fruits & Vegetables
rawCategories.put("Fruits & Vegetables", ProductCategory.FRUITS_AND_VEGETABLES);
// Variation 2: fruits & veg
rawCategories.put("fruits & veg", ProductCategory.FRUITS_AND_VEGETABLES);
// Variation 3: Fruits&Veg
rawCategories.put("Fruits&Veg", ProductCategory.FRUITS_AND_VEGETABLES);
Data opschonen in Java

Varianten mappen: output

System.out.println(rawCategories.get("fruits & veg")); // Get standard category
FRUITS_AND_VEGETABLES

 

rawCategories: alleen ter illustratie; kolommen zijn key-valueparen in de Map

Rauwe categorie Standaardcategorie
Fruits & Vegetables FRUITS_AND_VEGETABLES
fruits & veg FRUITS_AND_VEGETABLES
Fruits&Veg FRUITS_AND_VEGETABLES
Data opschonen in Java

Onbekende categorieën afhandelen

String unknownCategory = "Mystery Category";

// Return UNCATEGORIZED if category not found in rawCategories
ProductCategory category = rawCategories.getOrDefault(
        unknownCategory, ProductCategory.UNCATEGORIZED);

System.out.println("Unknown: " + category);
Mystery Category: UNCATEGORIZED
Rauwe categorie Standaardcategorie
Mystery Category UNCATEGORIZED
Data opschonen in Java

De mapping onveranderlijk maken

import java.util.Collections;
Map<String, ProductCategory> categories =
        Collections.unmodifiableMap(rawCategories); // Create immutable view

try { categories.put("new", ProductCategory.DAIRY); // Will throw exception } catch (UnsupportedOperationException e) { System.out.println("Cannot modify immutable map"); }
Cannot modify immutable map
Data opschonen in Java

Categorieën uit de dataset halen

// Extract category and quantity from our grocery inventory dataset
Map<String, Integer> rawData = 
        Map.of("Fruits & Vegetables", 10, 
               "fruits & veg", 20,
               "Fruits&Veg", 30);

rawData: mapping uit onze supermarktvoorraaddata

Rauwe categorie Aantal
Fruits & Vegetables 10
fruits & veg 20
Fruits&Veg 30
Data opschonen in Java

Standaardcategorie opzoeken

Map<ProductCategory, Integer> stockByCategory =
        new EnumMap<ProductCategory, Integer>(ProductCategory.class);

rawData.forEach((raw, quantity) -> // Lookup standard category in categories and sum quantities stockByCategory.merge(categories.get(raw), quantity, Integer::sum));

categories: mapping om standaardcategorie op te zoeken

Rauwe categorie Standaardcategorie
Fruits & Vegetables FRUITS_AND_VEGETABLES
fruits & veg FRUITS_AND_VEGETABLES
Fruits&Veg FRUITS_AND_VEGETABLES
Data opschonen in Java

Optellen per standaardcategorie

System.out.println(stockByCategory);
{FRUITS_AND_VEGETABLES=60}

 

stockByCategory: mapping na het opzoeken van rawData in categoryMap

Standaardcategorie Aantal
FRUITS_AND_VEGETABLES 60
Data opschonen in Java

Groeperen op standaardcategorie: samenvatting

1) Haal rawData (categorie/aantal) uit de dataset (snippet hieronder)

Rauwe categorie Aantal
Fruits & Vegetables 10

2) Maak categories om standaardcategorieën op te zoeken (snippet hieronder)

Rauwe categorie Standaardcategorie
Fruits & Vegetables FRUITS_AND_VEGETABLES

3) Bereken stockByCategory door rawData in categories op te zoeken (snippet hieronder)

Standaardcategorie Aantal
FRUITS_AND_VEGETABLES 60
Data opschonen in Java

Alles samenbrengen

Belangrijke imports

import java.util.Map;
import java.util.HashMap;
import java.util.Collections;
  • Maak een enum voor standaardcategorieën
  • Bouw een categoriemapping met HashMap
  • Maak een mapping onveranderlijk met Collections.unmodifiableMap
  • Handel onbekende categorieën af met .getOrDefault()
  • Groepeer op standaardcategorie met .merge()
Data opschonen in Java

Laten we oefenen!

Data opschonen in Java

Preparing Video For Download...