Definition
Normalizing cases refers to the process of converting all text data to a uniform case, such as lower case or upper case. This process is common when dealing with text data, especially user-generated text data, to ensure consistency and remove case-sensitivity from comparisons.
Example of normalizing cases using JavaScript
Here's a simple data object in JSON:
const data = [
{ Name: "John Doe", Email: "John.Doe@Email.com", Address: "123 Main St" },
{ Name: "Jane Doe", Email: "Jane.Doe@email.COM", Address: "456 Elm St" },
{ Name: "Bob Smith", Email: "Bob.Smith@email.com", Address: "789 Oak Ave" },
];
This JavaScript function normalizes the case of all the string fields in the data to lower case:
function normalizeCases(data) {
return data.map((item) => {
for (let field in item) {
if (typeof item[field] === "string") {
item[field] = item[field].toLowerCase();
}
}
return item;
});
}
This function uses the .toLowerCase()
method, which converts a string to lower case. It iterates over each field in each item of the data. If the field's value is a string, it converts that value to lower case.
Before
Name | Address | |
---|---|---|
John Doe | John.Doe@Email.com | 123 Main St |
Jane Doe | Jane.Doe@email.COM | 456 Elm St |
Bob Smith | Bob.Smith@email.com | 789 Oak Ave |
After
Name | Address | |
---|---|---|
john doe | john.doe@email.com | 123 main st |
jane doe | jane.doe@email.com | 456 elm st |
bob smith | bob.smith@email.com | 789 oak ave |
In the "Before" table, the case of the data is mixed. After standardization, all text is converted to lower case.
Lowercasing tends to be common in text analysis because it helps to reduce the dimensionality of the data and collates words that are the same but have different casing (e.g., "Email", "email", and "EMAIL") into a single representative form ("email").