import java.util.HashMap;
import java.util.Map;
public class OptionNormalizer {
/**
* Normalizes command-line options to a consistent format.
*
* @param options A map of command-line options, where keys are option names
* (e.g., "learning_rate") and values are the option's values
* (e.g., "0.01").
* @return A normalized map of options, where keys are normalized option names
* (lowercase and underscores instead of camelCase) and values are
* the original values. Returns an empty map if input is null.
*/
public static Map<String, String> normalizeOptions(Map<String, String> options) {
if (options == null) {
return new HashMap<>(); // Return empty map if input is null
}
Map<String, String> normalizedOptions = new HashMap<>();
for (Map.Entry<String, String> entry : options.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
// Normalize the key: lowercase and replace camelCase with underscores
String normalizedKey = key.toLowerCase().replaceAllecimento, key.toLowerCase().replaceFirst("([A-Z])", " $1_");
normalizedOptions.put(normalizedKey, value);
}
return normalizedOptions;
}
public static void main(String[] args) {
// Example usage
Map<String, String> commandLineOptions = new HashMap<>();
commandLineOptions.put("LearningRate", "0.01");
commandLineOptions.put("BatchSize", "32");
commandLineOptions.put("Epochs", "10");
Map<String, String> normalizedOptions = normalizeOptions(commandLineOptions);
System.out.println(normalizedOptions); // Expected output: {learning_rate=0.01, batch_size=32, epochs=10}
}
}
Add your comment