Skip to content

Instantly share code, notes, and snippets.

@bpgergo
Created June 22, 2011 09:40
Show Gist options
  • Save bpgergo/1039770 to your computer and use it in GitHub Desktop.
Save bpgergo/1039770 to your computer and use it in GitHub Desktop.
Ngrams with coroutines
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