简体   繁体   English

如何在Java中将句子拆分为单词,然后将其分为两组?

[英]How to split sentence in to words and then separate it in to two groups in Java?

I need to split sentence in to words and then in to two groups. 我需要将句子分成单词,然后分成两组。 One group should contain only last word in the sentence and other words belongs to second group as modifiers of the sentence. 一组应仅包含句子中的最后一个单词,而其他单词则属于第二组,作为句子的修饰语。
Eg: Max Price Food Information {Max, Price, Food} {Information} 例如:最高价格食物信息{最高价格,食物} {信息}

I did it until splitting the words. 我做到了,直到分开了两个字。 But cound't group like that.How can I do it? 但是不能那样组,我该怎么办?

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);
    ...................................
    ...................................
    }
}
    String lastWord = result[result.length - 1];
    String[] modifiers = Arrays.copyOf(result, result.length - 1);

Use Arrays.copyOfRange : 使用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]
}

i think this solution might work , it's not efficient if you have this pattern : Max Price Food Information you could have two array : 我认为此解决方案可能有效,如果您采用以下模式 ,则效率不高 :最高价格食物信息可以包含两个数组

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

then reading data in following way : 然后按以下方式读取数据:

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

this way is without split method 这种方式没有拆分方法

You can simply store the last word from the split words array and truncate the array's last element by copying it until length-1. 您可以简单地存储拆分词数组中的最后一个单词,并通过将其复制到length-1来截断该数组的最后一个元素。

Something like this: 像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM