【问题标题】:How to add a space at the end of every sentence?如何在每个句子的末尾添加一个空格?
【发布时间】:2018-11-07 21:15:05
【问题描述】:

我在我的 Android 应用程序中处理了几个句子。在每个句子的末尾,我需要添加一个额外的空格。我在下面试过了。

bodyText=body.replaceAll("\\.",". ");

这确实有效,直到我在句子之间找到dots。例如,如果有一个带有十进制数字的句子,那么上面的代码也会在该数字上添加一个空格。检查下面的示例,我在其中应用了上面的代码,但它没有按预期工作。

Last year the overall pass percentage was 90. 95%.  It was 96. 21% in 2016. 

您可以看到小数位是如何用空格分隔的。

如何只在句尾加空格?通常每个句子的结尾都会包含一个句号。

【问题讨论】:

  • 指定一个“句尾”。缩写后也可能有点,例如Mr. SmithDr. Brown
  • 您的所有陈述都与点 (.) 相符?
  • @WiktorStribiżew:句子的结尾。在上面我给出的例子中,第一句是Last year the overall pass percentage was 90. 95%. 第二句是It was 96. 21% in 2016. 。通常以句号结束。
  • @LearningAlways:通常以句号结束。是的。否则无论如何我们都看不到结局。
  • 正则表达式找不到句尾。它可以在字符串中找到一个特殊的模式。例如,在特定字符/字符序列之前/之后的点。为了将点限定为句末,您要检查什么上下文?请提供图案的综合规格。否则,这不是正则表达式的任务,而是 NLP 库的任务。

标签: java android regex string


【解决方案1】:

我不知道这是否正确,但如果是大写字母(大写后者),您可以在点(。)之后检查后者,然后您可以考虑该语句的结尾并添加一个空格。如果您的语句稍后从小写开始,则不能使用它。

但是很难检查第一个字母是否大写。

但你也可以用

 String first = myString.substring(0,1);

myString 应该在 dot(.) 之后,并且它不应该以任何数字开头。

【讨论】:

    【解决方案2】:

    你可以得到你自己的代码的结果,如下所示

    public static String modifySentence(String input) {
    
        StringBuilder sb = new StringBuilder(input);
        // Counter which will increase with every insertion of char in StringBuilder
        int insertCounter = 1;
    
        int index = input.indexOf(".");
    
        // If index is not of last digit of input, or not a digit or not a space.
        // In all above cases we need to skip
        while (index >= 0) {
            if ((index + 1 < input.length())
                    && (!Character.isDigit(input.charAt(index + 1)))
                    && (!Character.isSpaceChar(input.charAt(index + 1)))) {
    
                sb.insert(index + insertCounter, " ");
                insertCounter++;
            }
    
            index = input.indexOf(".", index + 1);
        }
        return sb.toString();
    }
    

    输入就像

    System.out.println(modifySentence("Last year the overall pass percentage was 90.95%.It was 96.21% in 2016."));
            System.out.println(modifySentence("Last year the overall pass percentage was 90.95%.It was 96.21% in 2016. And this is extra . test string"));
    

    输出是

    Last year the overall pass percentage was 90.95%. It was 96.21% in 2016.
    Last year the overall pass percentage was 90.95%. It was 96.21% in 2016. And this is extra . test string
    

    正如wiktor-stribiżew 评论的那样,使用your_string.replaceAll("\\.([^\\d\\s])", ". $1"); 也可以达到同样的效果。或者你可以使用your_string.replaceAll("\\.(?&lt;!\\d\\.\\d)(\\S)", ". $1"),它会处理这种情况,就像数字在点之后开始

    如果您对这些正则表达式有任何困惑,可以直接询问(通过在评论中提及他)wiktor-stribiżew。这些正则表达式功劳归他所有。

    【讨论】:

    • 太冗长了。只需s.replaceAll("\\.([^\\d\\s])", ". $1") 也可以做到这一点
    • @WiktorStribiżew 是的,你是完美的。
    • 不是我,我并不完美:)
    • @WiktorStribiżew :)。我还想知道一件事。如果句子在点之后以数字开头,我们可以处理这种情况吗?比如“test.100 分”。这里的结果将是“测试。100 分。”
    • 我认为总会有边缘情况,只有机器学习/NLP 可以提供帮助。你在说s.replaceAll("\\.(?&lt;!\\d\\.\\d)(\\S)", ". $1")之类的东西
    【解决方案3】:

    如果要在句号后已经有空格的句子中添加额外的空格,可以执行以下操作:

    String sentence = "Last year the overall pass percentage was 90.95%.  It was 96.21% in 2016.";
        sentence = sentence.replaceAll("\\. ",".  ");
    

    但如果您需要在句号后没有用空格分隔的句子中添加空格,请执行以下操作:

    import java.util.regex.*; 
    public class MyClass {
    public static void main(String args[]) {
        String sentence = "Last year the overall pass percentage was 90.95%.It was 96.21% in 2016.example.";
        String[] sentenceArr=sentence.split("\\.");  
        String str = "";
        for(int i = 0; i < sentenceArr.length; i++) {
    
            if(Pattern.matches(".*\\d+",sentenceArr[i]) && Pattern.matches("\\d+.*",sentenceArr[i+1])){
                str=str+sentenceArr[i]+".";
            }
            else{
                str=str+sentenceArr[i]+". ";
            }
    
        }
        System.out.println(str);
    }
    

    }

    输入:去年整体通过率为 90.95%。2016 年为 96.21%。示例

    输出:去年整体通过率为 90.95%。 2016 年为 96.21%。示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      相关资源
      最近更新 更多