Created
December 14, 2018 03:02
-
-
Save kzinmr/6eed42b5d6eb02ee8237f1dc8a16e120 to your computer and use it in GitHub Desktop.
pure python ngrams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import tee, zip_longest | |
def ngrams(iterable, n=3): | |
""" | |
>>> list(ngrams(range(5), 3)) | |
[(0, 1, 2), (1, 2, 3), (2, 3, 4)] | |
""" | |
ts = tee(iterable, n) | |
for i, t in enumerate(ts[1:]): | |
for _ in range(i+1): | |
next(t, None) | |
return zip(*ts) # zip_longest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment