Created
July 6, 2012 14:50
-
-
Save hellertime/3060604 to your computer and use it in GitHub Desktop.
Python n-gram generator using itertools
This file contains 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
import itertools | |
def ngrams(xs, f = None, n = 3): | |
ts = itertools.tee(xs, n) | |
for i, t in enumerate(ts[1:]): | |
for _ in xrange(i + 1): | |
next(t, None) | |
return map(f, itertools.izip(*ts)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment