有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从StringArray java中的标记/单词构建句子

我如何将list<String>中的单词连接起来,使其回到句子的真实形式

String sentence = "i get money. i love it. i want to buy. something. ";
String[] arrSent = sentence.split("\\. ");

for(int i=0; i<arrSent.length; i++) {
    String[] words = arrSent[i].split("\\ ");
    for(int j=0; j<words.length; j++ {
        listWord.add(words[j]);
    }
}

输出为:

i
get
money
i
love
it
i
want
to
buy
something

我只是想把它重建成真实的形式(如句子)

更新

我试过像你建议的那样。但我找到了一条新的困难之路

我已经从列表中删除了一个单词“爱”,并将其添加到新列表“listWord2”。当我把它重建成句子的真实形式时,新句子中的.就消失了

代码如下:

String [] arrays2 = listKata2.toArray(new String[listWord2.size()]);
sentence = Arrays.deepToString(arrays2).replaceAll(",", "");
System.out.println("result :  : "+sentence);

输出为:

[i get money i it i want to buy something]

缺少.

我应该再次按空格分割listWord2吗?,请推荐我


共 (3) 个答案

  1. # 1 楼答案

    做:

    String sentence = "i get money. i love it. i want to buy. something. ";
    String[] arrSent = sentence.split("\\. ");
    
    String sentenceRebuild = "";
    for(int i=0; i<arrSent.length; i++){
      String[] words = arrSent[i].split("\\ ");
    
      for(int j=0; j<words.length; j++){
        sentenceRebuild += words[j];
      }
    }
    
  2. # 2 楼答案

    尝试以下内容:

    String sentence = "i get money. i love it. i want to buy. something. ";
    String[] arrSent = sentence.split(" ");
    sentence=Arrays.toString(arrSent).replaceAll(",", "");
    System.out.println(sentence);
    

    输出:

    [i get money. i love it. i want to buy. something.]
    

    上面是如果你想在句子中.,下面是没有.

    String sentence = "i get money. i love it. i want to buy. something. ";
    String[] arrSent = sentence.split("\\. ");
    sentence=Arrays.toString(arrSent).replaceAll(",", "");
    System.out.println(sentence);
    

    输出:

    [i get money i love it i want to buy something]
    
  3. # 3 楼答案

    唯一真正的答案是,一旦你把所有的单词都记下来,失去了句子之间的句号,就没有办法把它们带回来——这些信息将永远丢失,然后就不可能重建原始结构

    你需要弄清楚如何(如果,实际上是)保留这些信息。一种方法是保留句子数组,只需用单词列表而不是句子字符串填充它,类似这样:

    List<List<String>> sentences = new List<List<String>>();
    String[] arrSent = sentence.split("\\. ");
    for (int i = 0; i < arrSent.length; i++)
        sentences.add(Arrays.asList(arrSend[i].split("\\ "));
    

    然后你会得到

    (
       ( "i", "get", "money" ),
       ( "i", "love", "it" ),
       ( "i", "want", "to", "buy" ),
       ( "something" )
    )
    

    这很容易看出如何从中重建原始文本

    另一种选择可能是保持单词列表的扁平化,但为句子终止的位置添加特殊的占位符,例如使用null值。扫描单词的算法应该知道如何在不崩溃的情况下处理这些占位符,而重建句子的算法将使用这些占位符来添加句号:

    String[] arrSent = sentence.split("\\. ");
    
    for(int i=0; i<arrSent.length; i++) {
        String[] words = arrSent[i].split("\\ ");
        for(int j=0; j<words.length; j++ {
            listWord.add(words[j]);
        }
        listWord.add(null);
    }
    
    // Rebuild
    StringBuffer output = new StringBuffer();
    for (Iterator<String> it = listWord.iterator(); it.hasNext(); ) {
         String val = it.next();
         String nextword = (output.length() > 0 ? " " : "") + val;
         output.append(val == null ? "." : nextword);
    }