【问题标题】:Capital letters after punctuation marks, but with some exceptions标点符号后的大写字母,但有一些例外
【发布时间】:2021-11-18 10:31:44
【问题描述】:

我的例外是:

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

我的代码如下:

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(".", ". ")); 我在检查更多标点符号之前就做了这些,现在我有更多它不起作用

【问题讨论】:

  • 可能是因为标点符号后面有空格(例如空格是大写的)?
  • 什么意思?对于这个例子:“reply-to address” 我需要显示这样的文本:“Reply-to address”,只有“R”大写字母因为是句子的开头,但“to”需要保持小写,因为它是一个复合词。例如“step 1 - take your car”,“S”必须是大写字母,因为它是句子的开头,“T”必须是大写字母,因为它位于标点符号和空格之后

标签: java string java-8 apache-commons-text


【解决方案1】:

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

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

  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;
}

【讨论】:

  • 感谢您的回答。您将如何区分“-”和由标点符号组成的单词? (看看我的例子)也许检查'-'之后是否有空格?
  • @Despotars '-' 似乎与其他标点符号不同,不是吗?从“规范化空间”中排除“-”怎么样?
  • 我同意@DavidLukas,连字符需要特殊处理。我用一个最小的例子更新了我的答案。
  • 那么,在示例“第 1 步 - 开车”中我应该如何进行?我需要在“-”之后大写“T”
  • @Despotars 我没有注意到 java-8 标签;我相应地更新了答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多