Data Cleaning
Clean and normalize messy data.
Example 074 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.
How it is built
The cell DataCleaning has no face and no memory. Handlers: run (line 17).
The source
074_data_cleaning.cell
// ============================================================================
// 074 — Data Cleaning
// ============================================================================
// Clean and normalize messy data.
//
// Run: soma run 074_data_cleaning.cell run
// Expect:
// Raw: 5 records
// Cleaned:
// alice -> [email protected]
// bob -> [email protected]
// carol -> [email protected]
// Removed 2 invalid records
cell DataCleaning {
on run() {
// Messy data with inconsistent formatting
let raw = list(
map("name", " Alice ", "email", " [email protected] "),
map("name", "BOB", "email", "[email protected]"),
map("name", "", "email", "[email protected]"),
map("name", "Carol", "email", "[email protected]"),
map("name", "Dave", "email", "")
)
let raw_count = raw.length
print("Raw: {raw_count} records")
// Clean and validate
let cleaned = list()
let removed = 0
for row in raw {
let name = lowercase(trim(row.name))
let email = lowercase(trim(row.email))
// Skip if name or email is empty
if name == "" {
removed = removed + 1
} else {
if email == "" {
removed = removed + 1
} else {
let clean_row = map("name", name, "email", email)
cleaned = list(cleaned, clean_row)
}
}
}
print("Cleaned:")
for row in cleaned {
print(" {row.name} -> {row.email}")
}
print("Removed {removed} invalid records")
return "done"
}
}What the toolchain says
$ soma check 074_data_cleaning.cell
✓ All checks passed.$ soma run 074_data_cleaning.cell run
Raw: 5 records
Cleaned:
alice -> [email protected]
bob -> [email protected]
carol -> [email protected]
Removed 2 invalid records
done