打印字符串数组中每个单词的bi和trigrams

时间:2016-02-29 02:56:23

标签: java arrays combinatorics

我需要为从fwords.txt转换的数组tempsArray中的每个单词打印所有可能的bi和trigrams。这是我复制并粘贴在一起的代码。

import java.util.*;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.*;

public class Ngram {

public static List<String> ngrams(int n, String str) {
    List<String> ngrams = new ArrayList<String>();
    String[] words = str.split(" ");
    for (int i = 0; i < words.length - n + 1; i++)
        ngrams.add(concat(words, i, i+n));
    return ngrams;
}

public static String concat(String[] words, int start, int end) {
    StringBuilder sb = new StringBuilder();
    for (int i = start; i < end; i++)
        sb.append((i > start ? " " : "") + words[i]);
    return sb.toString();
}

public static void main(String[] args) throws IOException {
String token1 = "";
// create Scanner inFile1
Scanner inFile1 = new Scanner(new File("fwords.txt")).useDelimiter(",\\s*");
 List<String> temps = new ArrayList<String>();
// while loop
while (inFile1.hasNext()) {
  // find next line
  token1 = inFile1.next();
  temps.add(token1);
}
inFile1.close();
String[] tempsArray = temps.toArray(new String[0]);




    for (int n = 1; n <= 3; n++) {
        for (String ngram : ngrams(n, tempsArray[1] ))
            System.out.println(ngram);
        System.out.println();
    }
}
}

for(String ngram:ngrams(n,tempsArray [1]))将打印数组中第二个单词的bi和trigrams。我怎么能为数组中的每个单词做这个?

1 个答案:

答案 0 :(得分:0)

要迭代数组中的所有单词,请使用两个循环。最外层将遍历tempsArray的每个元素。其余部分来自您的代码(数字1除了索引j)。

for (int j =0 j < tempsArray.length; j ++ ) { 
  for (int n = 1; n <= 3; n++) {
    for (String ngram : ngrams(n, tempsArray[j] ))
      System.out.println(ngram);
    System.out.println();
  }
}

for (String t: tempsArray ) { 
  for (int n = 1; n <= 3; n++) {
    for (String ngram : ngrams(n, t))
      System.out.println(ngram);
    System.out.println();
  }
}