Instantly share code, notes, and snippets.

What would you like to do?
a simple method for creating all possible n-grams of a given string
import static;
import static;
private List<String> possibleNgrams(String[] words) {
return rangeClosed(1, words.length)
window ->
rangeClosed(0, words.length - window)
i ->
range(i, i + window)
.mapToObj(x -> words[x])
.collect(Collectors.joining(" "))))
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