wordNgrams function
Returns word n-grams of s with n words per gram.
Words are split on whitespace; empty or n < 1 returns empty list.
Implementation
List<List<String>> wordNgrams(String s, int n) {
if (n < 1) return <List<String>>[];
final List<String> words = s.trim().split(RegExp(r'\s+'));
if (words.isEmpty || words.length < n) return <List<String>>[];
final int count = words.length - n + 1;
return List.generate(count, (int i) => words.sublist(i, i + n));
}