@Test public void guessCaseTest() { String[] inputs = {"abc", "Abc", "ABC", "Abc'de", "ABC'DE", "ABC.", "ABC'de", "a", "12", "A", "A1"}; WordAnalysisSurfaceFormatter.CaseType[] expected = { LOWER_CASE, TITLE_CASE, UPPER_CASE, TITLE_CASE, UPPER_CASE, UPPER_CASE, UPPER_CASE_ROOT_LOWER_CASE_ENDING, LOWER_CASE, DEFAULT_CASE, UPPER_CASE, UPPER_CASE, }; WordAnalysisSurfaceFormatter formatter = new WordAnalysisSurfaceFormatter(); int i = 0; for (String input : inputs) { Assert.assertEquals(expected[i], formatter.guessCase(input)); i++; } }
public CaseType guessCase(String input) { boolean firstLetterUpperCase = false; int lowerCaseCount = 0; int upperCaseCount = 0; int letterCount = 0; for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (!Character.isAlphabetic(c)) { continue; } if (i == 0) { firstLetterUpperCase = Character.isUpperCase(c); if (firstLetterUpperCase) { upperCaseCount++; } else { lowerCaseCount++; } } else { if (Character.isUpperCase(c)) { upperCaseCount++; } else if (Character.isLowerCase(c)) { lowerCaseCount++; } } letterCount++; } if (letterCount == 0) { return CaseType.DEFAULT_CASE; } if (letterCount == lowerCaseCount) { return CaseType.LOWER_CASE; } if (letterCount == upperCaseCount) { return CaseType.UPPER_CASE; } if (firstLetterUpperCase && letterCount == lowerCaseCount + 1) { return letterCount == 1 ? CaseType.UPPER_CASE : CaseType.TITLE_CASE; } int apostropheIndex = input.indexOf('\''); if (apostropheIndex > 0 && apostropheIndex < input.length() - 1) { if (guessCase(input.substring(0, apostropheIndex)) == CaseType.UPPER_CASE && guessCase(input.substring(apostropheIndex + 1)) == CaseType.LOWER_CASE) { return CaseType.UPPER_CASE_ROOT_LOWER_CASE_ENDING; } } return CaseType.MIXED_CASE; }
WordAnalysisSurfaceFormatter { public CaseType guessCase(String input) { boolean firstLetterUpperCase = false; int lowerCaseCount = 0; int upperCaseCount = 0; int letterCount = 0; for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (!Character.isAlphabetic(c)) { continue; } if (i == 0) { firstLetterUpperCase = Character.isUpperCase(c); if (firstLetterUpperCase) { upperCaseCount++; } else { lowerCaseCount++; } } else { if (Character.isUpperCase(c)) { upperCaseCount++; } else if (Character.isLowerCase(c)) { lowerCaseCount++; } } letterCount++; } if (letterCount == 0) { return CaseType.DEFAULT_CASE; } if (letterCount == lowerCaseCount) { return CaseType.LOWER_CASE; } if (letterCount == upperCaseCount) { return CaseType.UPPER_CASE; } if (firstLetterUpperCase && letterCount == lowerCaseCount + 1) { return letterCount == 1 ? CaseType.UPPER_CASE : CaseType.TITLE_CASE; } int apostropheIndex = input.indexOf('\''); if (apostropheIndex > 0 && apostropheIndex < input.length() - 1) { if (guessCase(input.substring(0, apostropheIndex)) == CaseType.UPPER_CASE && guessCase(input.substring(apostropheIndex + 1)) == CaseType.LOWER_CASE) { return CaseType.UPPER_CASE_ROOT_LOWER_CASE_ENDING; } } return CaseType.MIXED_CASE; } }
WordAnalysisSurfaceFormatter { public CaseType guessCase(String input) { boolean firstLetterUpperCase = false; int lowerCaseCount = 0; int upperCaseCount = 0; int letterCount = 0; for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (!Character.isAlphabetic(c)) { continue; } if (i == 0) { firstLetterUpperCase = Character.isUpperCase(c); if (firstLetterUpperCase) { upperCaseCount++; } else { lowerCaseCount++; } } else { if (Character.isUpperCase(c)) { upperCaseCount++; } else if (Character.isLowerCase(c)) { lowerCaseCount++; } } letterCount++; } if (letterCount == 0) { return CaseType.DEFAULT_CASE; } if (letterCount == lowerCaseCount) { return CaseType.LOWER_CASE; } if (letterCount == upperCaseCount) { return CaseType.UPPER_CASE; } if (firstLetterUpperCase && letterCount == lowerCaseCount + 1) { return letterCount == 1 ? CaseType.UPPER_CASE : CaseType.TITLE_CASE; } int apostropheIndex = input.indexOf('\''); if (apostropheIndex > 0 && apostropheIndex < input.length() - 1) { if (guessCase(input.substring(0, apostropheIndex)) == CaseType.UPPER_CASE && guessCase(input.substring(apostropheIndex + 1)) == CaseType.LOWER_CASE) { return CaseType.UPPER_CASE_ROOT_LOWER_CASE_ENDING; } } return CaseType.MIXED_CASE; }  }
WordAnalysisSurfaceFormatter { public CaseType guessCase(String input) { boolean firstLetterUpperCase = false; int lowerCaseCount = 0; int upperCaseCount = 0; int letterCount = 0; for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (!Character.isAlphabetic(c)) { continue; } if (i == 0) { firstLetterUpperCase = Character.isUpperCase(c); if (firstLetterUpperCase) { upperCaseCount++; } else { lowerCaseCount++; } } else { if (Character.isUpperCase(c)) { upperCaseCount++; } else if (Character.isLowerCase(c)) { lowerCaseCount++; } } letterCount++; } if (letterCount == 0) { return CaseType.DEFAULT_CASE; } if (letterCount == lowerCaseCount) { return CaseType.LOWER_CASE; } if (letterCount == upperCaseCount) { return CaseType.UPPER_CASE; } if (firstLetterUpperCase && letterCount == lowerCaseCount + 1) { return letterCount == 1 ? CaseType.UPPER_CASE : CaseType.TITLE_CASE; } int apostropheIndex = input.indexOf('\''); if (apostropheIndex > 0 && apostropheIndex < input.length() - 1) { if (guessCase(input.substring(0, apostropheIndex)) == CaseType.UPPER_CASE && guessCase(input.substring(apostropheIndex + 1)) == CaseType.LOWER_CASE) { return CaseType.UPPER_CASE_ROOT_LOWER_CASE_ENDING; } } return CaseType.MIXED_CASE; }  String format(SingleAnalysis analysis, String apostrophe); String format(SingleAnalysis analysis); String formatToCase(SingleAnalysis analysis, CaseType type, String apostrophe); String formatToCase(SingleAnalysis analysis, CaseType type); boolean canBeFormatted(SingleAnalysis analysis, CaseType type); CaseType guessCase(String input); }
WordAnalysisSurfaceFormatter { public CaseType guessCase(String input) { boolean firstLetterUpperCase = false; int lowerCaseCount = 0; int upperCaseCount = 0; int letterCount = 0; for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (!Character.isAlphabetic(c)) { continue; } if (i == 0) { firstLetterUpperCase = Character.isUpperCase(c); if (firstLetterUpperCase) { upperCaseCount++; } else { lowerCaseCount++; } } else { if (Character.isUpperCase(c)) { upperCaseCount++; } else if (Character.isLowerCase(c)) { lowerCaseCount++; } } letterCount++; } if (letterCount == 0) { return CaseType.DEFAULT_CASE; } if (letterCount == lowerCaseCount) { return CaseType.LOWER_CASE; } if (letterCount == upperCaseCount) { return CaseType.UPPER_CASE; } if (firstLetterUpperCase && letterCount == lowerCaseCount + 1) { return letterCount == 1 ? CaseType.UPPER_CASE : CaseType.TITLE_CASE; } int apostropheIndex = input.indexOf('\''); if (apostropheIndex > 0 && apostropheIndex < input.length() - 1) { if (guessCase(input.substring(0, apostropheIndex)) == CaseType.UPPER_CASE && guessCase(input.substring(apostropheIndex + 1)) == CaseType.LOWER_CASE) { return CaseType.UPPER_CASE_ROOT_LOWER_CASE_ENDING; } } return CaseType.MIXED_CASE; }  String format(SingleAnalysis analysis, String apostrophe); String format(SingleAnalysis analysis); String formatToCase(SingleAnalysis analysis, CaseType type, String apostrophe); String formatToCase(SingleAnalysis analysis, CaseType type); boolean canBeFormatted(SingleAnalysis analysis, CaseType type); CaseType guessCase(String input);  }