(sequence, n)
| 20 | # Implementation from nltk source |
| 21 | # https://www.nltk.org/_modules/nltk/util.html |
| 22 | def form_ngrams(sequence, n): |
| 23 | history = [] |
| 24 | while n > 1: |
| 25 | # PEP 479, prevent RuntimeError from being raised when StopIteration bubbles out of generator |
| 26 | try: |
| 27 | next_item = next(sequence) |
| 28 | except StopIteration: |
| 29 | # no more data, terminate the generator |
| 30 | return |
| 31 | history.append(next_item) |
| 32 | n -= 1 |
| 33 | for item in sequence: |
| 34 | history.append(item) |
| 35 | yield tuple(history) |
| 36 | del history[0] |
| 37 | |
| 38 | |
| 39 | def word_ngrams(s, n): |
no outgoing calls
no test coverage detected