Skip to content

Instantly share code, notes, and snippets.

@halfak
Last active April 30, 2016 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halfak/f1c334690e846309fd4d8c272aca12a8 to your computer and use it in GitHub Desktop.
Save halfak/f1c334690e846309fd4d8c272aca12a8 to your computer and use it in GitHub Desktop.
$ python
Python 3.4.3 (default, Jul 28 2015, 18:20:59)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections import deque
>>>
>>> def grammer(tokens, grams=[(0,)]):
... buffer_length = max(max(indexes) for indexes in grams)
... buffer = deque(maxlen=buffer_length+1)
... for token in tokens:
... buffer.append(token)
... for gram in grams:
... if len(buffer) > max(gram):
... # Emit gram
... yield [buffer[i] for i in gram]
...
>>>
>>> print(list(grammer([1,2,3,4,5,6,7,8,9,0], grams=[(0,), (0,2)])))
[[1], [1], [1], [1, 3], [2], [2, 4], [3], [3, 5], [4], [4, 6], [5], [5, 7], [6], [6, 8], [7], [7, 9], [8], [8, 0]]
>>> print(list(grammer([1,2,3,4,5,6,7,8,9,0], grams=[(0,), (0,1), (0,1,2), (0,2)])))
[[1], [1], [1, 2], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [2, 3, 4], [2, 4], [3], [3, 4], [3, 4, 5], [3, 5], [4], [4, 5], [4, 5, 6], [4, 6], [5], [5, 6], [5, 6, 7], [5, 7], [6], [6, 7], [6, 7, 8], [6, 8], [7], [7, 8], [7, 8, 9], [7, 9], [8], [8, 9], [8, 9, 0], [8, 0]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment