【问题标题】:Java String Fix Capitalization in Abbreviations缩写中的 Java 字符串修复大写
【发布时间】:2016-01-05 09:53:33
【问题描述】:

我需要一种方法来修正在 String 中找到的缩写词的大小写。假设所有缩写的间距正确。

例如,

"Robert a.k.a. Bob A.k.A. dr. Bobby"

变成:

"Robert A.K.A. Bob A.K.A. Dr. Bobby"

将提前知道正确大写的缩写,存储在某种Collection 中。

我在想这样的算法:

private String fix(String s) {
    StringBuilder builder = new StringBuilder();
    for (String word : s.split(" ")) {
        if (collection.contains(word.toUpperCase()) {
            // word = correct abbreviation here
        }
        builder.append(word);
        builder.append(" ");
    }
    return builder.toString().trim();
}

但据我所知,这种方法存在几个问题:

  • 如果缩写有小写字母(Dr.)
  • 如果单词以标点符号(“a.k.a.”)开头或结尾

我觉得这可以通过正则表达式来解决,迭代匹配并替换正确的缩写。但如果不是,我应该如何解决这个问题?

【问题讨论】:

  • 是否要将每个单词的首字母大写?单词由字符串序列定义,后跟句点或空格。这是正确的吗?
  • 我目前有一个我想要更正的所有缩写的列表。所以如果有"o.o."这样无意义的缩写,就不会更正了。
  • 哦,我明白了 - 让我看看我的答案是否涵盖了这个无意义的缩写
  • 你怎么能真正知道如果一个缩写是“无意义的”?将所有可能的缩写列入白名单感觉过于复杂......

标签: java regex


【解决方案1】:

我建议您使用实用程序库,而不是使用正则表达式或滚动您自己的实现。 Apache Commons Lang 中的 WordUtils 非常适合这项工作:

String input = "Robert a.k.a. Bob A.k.A. dr. Bobby";
String capitalized = WordUtils.capitalize(input, '.', ' ');
System.out.println(capitalized);

打印出来

罗伯特 A.K.A.鲍勃 A.K.A.鲍比博士

【讨论】:

    【解决方案2】:

    我就是这样处理的......

    更新

    OP 读取 cmets 后

    打印出来:

    罗伯特 A.K.A.鲍勃 A.K.A. Bobby The o.o. 博士

    import java.util.ArrayList;
    import java.util.List;
    
    public class Fixer {
    
        List<String> collection = new ArrayList<>();
    
        public Fixer() {
            collection.add("Dr.");
            collection.add("A.K.A.");
            collection.add("o.o.");
        }
    
        /* app entry point */
        public static void main(String[] args) throws InterruptedException {
            String testCase = "robert a.k.a. bob A.k.A. dr. bobby the o.o.";
    
            Fixer l = new Fixer();
            String result = l.fix(testCase);
    
            System.out.println(result);
        }
    
        private String fix(String s) {
            StringBuilder builder = new StringBuilder();
            for (String word : s.split(" ")) {
                String abbr = getAbbr(word);
                if (abbr == null) {
                    builder.append(word.substring(0, 1).toUpperCase());
                    builder.append(word.substring(1));
                } else {
                    builder.append(abbr);
                }
                builder.append(" ");
            }
            return builder.toString().trim();
        }
    
        private String getAbbr(String word) {
            for (String abbr : collection) {
                if (abbr.equalsIgnoreCase(word)) {
                    return abbr;
                }
            }
            return null;
        }
    }
    

    【讨论】:

      【解决方案3】:

      您不必使用正则表达式,即。您的解决方案看起来很合理(尽管如果您要处理大量数据,它可能会很慢)。

      对于包含小写字母的缩写,例如。博士,您可以使用a case insensitive string comparison 而不是toUpperCase。实际上,这仅在您自己直接比较字符串时才有用。你真的需要一个不区分大小写的HashMap。也许:

      Map<String, String> collection = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
      

      如果缩写以标点符号开头或结尾,请确保您的集合中的相应键也是如此。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-14
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 2020-09-16
        • 1970-01-01
        • 1970-01-01
        • 2015-07-14
        相关资源
        最近更新 更多