I am trying tokenize strings into ngrams. Strangely in the documentation for the NGramTokenizer I do not see a method that will return the individual ngrams that were tokeni
package ngramalgoimpl;
import java.util.*;
public class ngr {
public static List n_grams(int n, String str) {
List n_grams = new ArrayList();
String[] words = str.split(" ");
for (int i = 0; i < words.length - n + 1; i++)
n_grams.add(concatination(words, i, i+n));
return n_grams;
}
/*stringBuilder is used to cancatinate mutable sequence of characters*/
public static String concatination(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) {
for (int n = 1; n <= 3; n++) {
for (String ngram : n_grams(n, "This is my car."))
System.out.println(ngram);
System.out.println();
}
}
}