Created
June 22, 2011 09:40
-
-
Save bpgergo/1039770 to your computer and use it in GitHub Desktop.
Ngrams with coroutines
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
def coroutine(func): | |
""" A decorator function that takes care | |
of starting a coroutine automatically on call """ | |
def start(*args,**kwargs): | |
coro = func(*args,**kwargs) | |
coro.next() | |
return coro | |
return start | |
@coroutine | |
def ngrams(n, target): | |
""" A coroutine to generate ngrams. | |
Accepts one char at a time """ | |
chars = collections.deque() | |
while True: | |
chars.append((yield)) | |
if len(chars) == n: | |
target.send(chars) | |
chars.popleft() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment