【问题标题】:How to split sentence in to words and then separate it in to two groups in Java?如何在Java中将句子分成单词然后分成两组?
【发布时间】:2016-04-22 11:34:57
【问题描述】:

我需要将句子分成单词,然后分成两组。一组应该只包含句子中的最后一个单词,其他单词属于第二组作为句子的修饰语。
例如: 最高价格食品信息 {最大、价格、食物} {信息}

我一直这样做,直到拆分单词。但是不能这样分组。我该怎么做?

import java.util.*;
public class Groupword {
    public static void main(String[] args) {

    ArrayList<String> words = new ArrayList<String>();
    HashMap<String, Integer> wordFreqMap = new HashMap<String, Integer>();

    terms.add("Max Price Food Information");

    for(int i=0; i < terms.size(); i++) {
      tempTerm = terms.get(i);
      String[] result = tempTerm.split(" ");
      for (String s : result) {
      System.out.println("word="+s);
    ...................................
    ...................................
    }
}

【问题讨论】:

    标签: java arrays arraylist split hashmap


    【解决方案1】:
        String lastWord = result[result.length - 1];
        String[] modifiers = Arrays.copyOf(result, result.length - 1);
    

    【讨论】:

    • 感谢您的回复。它的输出为 lastWord=Information modifiers=[Ljava.lang.String;@789144
    • 使用System.out.println("lastWord=" + lastWord + " modifiers=" + Arrays.toString(modifiers))
    【解决方案2】:

    使用Arrays.copyOfRange:

    terms.add("Max Price Food Information");
    
    
    for (String s: terms) {
        String[] arr = s.split(" ");
        String[] arr1 = Arrays.copyOfRange(arr, 0, arr.length-1);  // [Max, Price, Food]
        String[] arr2 = Arrays.copyOfRange(arr, arr.length-1, arr.length); // [Information]
    }
    

    【讨论】:

      【解决方案3】:

      我认为这个解决方案可能有效,它效率不高 如果你有这个 pattern : 最高价格食品信息 你可以有两个数组

      String Data[][]=new String[3][terms.size];
      String Information[] = new String[terms.size];
      

      然后通过以下方式读取数据:

      Scanner in = new Scanner(System.in);
      for(int i=0; i < terms.size(); i++){
          Data[0][i] = in.next();
          Data[1][i] = in.next();
          Data[2][i] = in.next();
          Information[i] = in.next();
      }
      

      这种方式是没有拆分方式

      【讨论】:

        【解决方案4】:

        您可以简单地存储拆分单词数组中的最后一个单词,并通过将其复制到长度为 1 来截断数组的最后一个元素。

        类似这样的:

        for(int i=0; i < terms.size(); i++) {
          tempTerm = terms.get(i);
          String[] result = tempTerm.split(" ");
          for (String s : result) {
              System.out.println("word="+s);
          }
          String lastWord = result[result.length-1];
          result = Arrays.copyOf(result, result.length-1)
        }
        

        【讨论】:

          【解决方案5】:
          List<String> firstWordsList = new ArrayList<>();
          List<String> lastWordList = new ArrayList<>();
          for(String tempTerm : terms) {
              int lastSpaceIndex = tempTerm.lastIndexOf(" ");
              if (lastSpaceIndex >= 0) {
                  String firstWords = tempTerm.substring(0, lastSpaceIndex);
                  String lastWord = tempTerm.substring(lastSpaceIndex+1);
                  firstWordsList.add(firstWords);
                  lastWordsList.add(lastWord);
              }
              else {
                  lastWordsList.add(tempTerm);
              }
          }
          

          【讨论】:

          • 我尝试使用 'System.out.println("firstWordsList="+firstWordsList); '但是,它给出了一个错误,'java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1'。我该如何解决?非常感谢@John Scattergood .–
          • 我的回答有错字。现在已经修好了。
          • 我试过了。对不起。它也给出了同样的错误。 '线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:-1'。
          • 你有没有只有一个单词且没有空格的术语?如果是这样,则需要防止 lastIndexOf() 在调用 substring() 之前返回 -1。我会更新答案。
          • 是的,我的数据集只包含一个单词。现在它运作良好。非常感谢。如果我需要将这个程序的第一个词作为一个组而不是最后一个词来更改。例如:Max Price Food Information {Max } {Price, Food,Information} 我可以知道我需要做哪些更改吗?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-02
          • 1970-01-01
          • 1970-01-01
          • 2015-02-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多