Skip to content

Instantly share code, notes, and snippets.

@bpgergo
Created June 22, 2011 09:42
Show Gist options
  • Save bpgergo/1039774 to your computer and use it in GitHub Desktop.
Save bpgergo/1039774 to your computer and use it in GitHub Desktop.
Ngrams with coroutines 2
@coroutine
def filter_chars(accepted_chars,target):
""" A coroutine to filter out unaccepted chars.
Accepts one char at a time """
while True:
c = (yield)
if c.lower() in accepted_chars:
target.send(c.lower())
@coroutine
def counter(matrix):
""" A counter sink """
while True:
a, b = (yield)
matrix[pos[a]][pos[b]] += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment