1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class OptionNormalizer {
  4. /**
  5. * Normalizes command-line options to a consistent format.
  6. *
  7. * @param options A map of command-line options, where keys are option names
  8. * (e.g., "learning_rate") and values are the option's values
  9. * (e.g., "0.01").
  10. * @return A normalized map of options, where keys are normalized option names
  11. * (lowercase and underscores instead of camelCase) and values are
  12. * the original values. Returns an empty map if input is null.
  13. */
  14. public static Map<String, String> normalizeOptions(Map<String, String> options) {
  15. if (options == null) {
  16. return new HashMap<>(); // Return empty map if input is null
  17. }
  18. Map<String, String> normalizedOptions = new HashMap<>();
  19. for (Map.Entry<String, String> entry : options.entrySet()) {
  20. String key = entry.getKey();
  21. String value = entry.getValue();
  22. // Normalize the key: lowercase and replace camelCase with underscores
  23. String normalizedKey = key.toLowerCase().replaceAllecimento, key.toLowerCase().replaceFirst("([A-Z])", " $1_");
  24. normalizedOptions.put(normalizedKey, value);
  25. }
  26. return normalizedOptions;
  27. }
  28. public static void main(String[] args) {
  29. // Example usage
  30. Map<String, String> commandLineOptions = new HashMap<>();
  31. commandLineOptions.put("LearningRate", "0.01");
  32. commandLineOptions.put("BatchSize", "32");
  33. commandLineOptions.put("Epochs", "10");
  34. Map<String, String> normalizedOptions = normalizeOptions(commandLineOptions);
  35. System.out.println(normalizedOptions); // Expected output: {learning_rate=0.01, batch_size=32, epochs=10}
  36. }
  37. }

Add your comment