Skip to content

Instantly share code, notes, and snippets.

@mazieres
Last active November 15, 2015 07:08
Show Gist options
  • Select an option

  • Save mazieres/8955092cad1e99f2f824 to your computer and use it in GitHub Desktop.

Select an option

Save mazieres/8955092cad1e99f2f824 to your computer and use it in GitHub Desktop.
def ngrams(sequence, depth):
seq = '^' + sequence + '*'
res = []
while depth > 0:
i, j = 0, depth
while j <= len(seq):
res.append(seq[i:j])
i += 1
j += 1
depth -= 1
res.remove('^')
res.remove('*')
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment