1

我的例外是:

  • 句首或标点符号后的大写字母
  • 标点符号后加空格
  • 缩写后,不要使用大写字母
  • 在“-”(带空格)之后,使用大写字母
  • 在“-”(无空格)之后,不要大写

我的代码如下,但有例外:

private static char[] PUNCTUATION_MARKS = { '?', '!', ';', '.', '-' };

applyCorrectCase("step 1 - take your car");
applyCorrectCase("reply-to address");
applyCorrectCase("req. start");
applyCorrectCase("alt. del. date [alternate detection date]");
applyCorrectCase("you are.important for us? to continue?here! yes");

String applyCorrectCase(String value) {
    String lowerCaseValue = value.toLowerCase();
    if (value.contains(". ")) {
        lowerCaseValue = value.replace(". ", ".");
    }
    lowerCaseValue = WordUtils.capitalize(lowerCaseValue, PUNCTUATION_MARKS );
    System.out.println(lowerCaseValue.replace(".", ". "));
}

这些是我的结果:

Step 1 - take your car <--- The 't' after the '-' need to be uppercase
Reply-To address <--- The 't' after the '-' need to be lowercase
Req. Start <--- The 's' after the '.' need to be lowercase because it is an abbreviation
Alt. Del. Date [alternate detection date] <--- Both 'd' after the '.' need to be lowercase because it is an abbreviation
You are. Important for us? to continue?Here! yes <--- The 't' after the '?' need to be capital, we need to add an space between '?' and 'H', the 'y' after the '!' need to be uppercase

这些是我的期望:

Step 1 - Take your car
Reply-to address
Req. start
Alt. del. date [alternate detection date]
You are. Important for us? To continue? Here! Yes

有什么想法可以修复我的代码吗?

更新

关于代码if (value.contains(". ")) {System.out.println(lowerCaseValue.replace(".", ". "));我在检查更多标点符号之前就做了这些,现在我有更多它不起作用

4

1 回答 1

3

我会以不同的步骤来解决这个问题。

重新排序规范,您有三个不同的类别

  1. 规范化空格:如果缺少标点符号,则在标点符号后添加一个空格
  2. 大写字母:在句首,标点符号后,连字符后跟空格
  3. 无大写字母:缩写后或连字符后不带空格

为此,您需要定义您的缩写词(因为否则如何区分缩写词和句子的结尾?)。

然后按顺序执行以下操作

1. 规范化空间

查找后面没有空格的标点符号,并根据需要添加空格

2. 大写字母

找到标点符号(全部)和所有后跟空格的连字符。在所有这些点中,将第一个字母大写。使字符串的第一个字母(第一句的开头)也大写。

3. 没有大写字母

参考定义的缩写,如果您在字符串中找到一个缩写模式,使其后跟一个句点,则空格后面的以下字母必须是小写的。

如果您发现连字符后紧跟一个大写字母,则该字母必须变为小写。

编辑

我认为这应该对提供的测试用例有用。我确信它可以进行微调,但足以开始:

private static final String[] PUNCTUATION_MARKS = { "\\?", "\\!", ";", "\\." };

private static final String[] ABBREVIATIONS = {
        "Req", "req",
        "Alt", "alt",
        "Del", "del",
};

public static void main(String[] args) {
    applyCorrectCase("step 1 - take your car");
    applyCorrectCase("reply-to address");
    applyCorrectCase("req. start");
    applyCorrectCase("alt. del. date [alternate detection date]");
    applyCorrectCase("you are.important for us? to continue?here! yes");

}

static String applyCorrectCase(String value) {
    String lower = value.toLowerCase();
    // have only one space where there are multiple
    lower.replaceAll("\\s+", " ");
    for (String p : PUNCTUATION_MARKS) {
        // add a space after a punctuation mark that doesn't have one
        lower = lower.replaceAll(p, p + " ")
                .replaceAll("\\s+", " ");
    }
    char[] chars = lower.toCharArray();
    chars[0] = Character.toUpperCase(chars[0]);
    for (int i = 0; i < chars.length; i++) {
        // capitalize the first letter after the space that follows a punctuation mark or a hyphen
        for (char p : new char[]{ '?', '!', ';', '.', '-' }) {
            if (chars[i] == p && i < chars.length - 2 && chars[i + 1] == ' ') {
                chars[i + 2] = Character.toUpperCase(chars[i + 2]);
            }
        }
    }
    // search for abbreviations
    String tmp = new String(chars);
    List<Pattern> patterns = new ArrayList<>();
    for (String a : ABBREVIATIONS) {
        patterns.add(Pattern.compile("(" + a + "\\. )([A-Z])"));
    }
    for (Pattern p : patterns) {
        Matcher m = p.matcher(tmp);
        while (m.find()) {
            tmp = tmp.replaceAll(m.group(), m.group(1) + m.group(2).toLowerCase());
        }
    }
    System.out.println(tmp);
    return tmp;
}
于 2021-11-18T11:06:22.367 回答