Create a gist now

Instantly share code, notes, and snippets.

Embed
a simple method for creating all possible n-grams of a given string
import static java.util.stream.IntStream.range;
import static java.util.stream.IntStream.rangeClosed;
private List<String> possibleNgrams(String[] words) {
return rangeClosed(1, words.length)
.mapToObj(
window ->
rangeClosed(0, words.length - window)
.mapToObj(
i ->
range(i, i + window)
.mapToObj(x -> words[x])
.collect(Collectors.joining(" "))))
.flatMap(Function.identity())
.collect(Collectors.toList());
}
System.out.println(possibleNgrams("pale ale beer".split("\\s+"))
> ["pale ale beer", "pale ale", "ale beer", "pale", "ale", "beer"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment