Last active
April 30, 2016 15:22
-
-
Save halfak/f1c334690e846309fd4d8c272aca12a8 to your computer and use it in GitHub Desktop.
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
$ 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