Skip to content

Instantly share code, notes, and snippets.

@mkolod
Last active April 28, 2017 16:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkolod/1512a8032d03d8a0fdc0b8baa2f027a1 to your computer and use it in GitHub Desktop.
Save mkolod/1512a8032d03d8a0fdc0b8baa2f027a1 to your computer and use it in GitHub Desktop.
def find_ngrams(input_list, max_n):
return [map(lambda x: list(x), zip(*[input_list[i:] for i in range(n)])) for n in range(1, max_n+1)]
@mkolod
Copy link
Author

mkolod commented Apr 28, 2017

for ngram in find_ngrams("the cat sat on the mat".split(), 4):
... print(ngram)
...
[['the'], ['cat'], ['sat'], ['on'], ['the'], ['mat']]
[['the', 'cat'], ['cat', 'sat'], ['sat', 'on'], ['on', 'the'], ['the', 'mat']]
[['the', 'cat', 'sat'], ['cat', 'sat', 'on'], ['sat', 'on', 'the'], ['on', 'the', 'mat']]
[['the', 'cat', 'sat', 'on'], ['cat', 'sat', 'on', 'the'], ['sat', 'on', 'the', 'mat']]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment