Skip to content

Instantly share code, notes, and snippets.

@4e1e0603
Last active February 11, 2020 08:49
Show Gist options
  • Save 4e1e0603/08519eafe25319e44cabeb085d184c61 to your computer and use it in GitHub Desktop.
Save 4e1e0603/08519eafe25319e44cabeb085d184c61 to your computer and use it in GitHub Desktop.
Compute N-Gram
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Jednoduchá realizace n-gramu.
"""
def ngrams(words, n):
return zip(*[words[i:] for i in range(n)])
def bigrams(words):
return ngrams(words, 2)
def trigrams(words):
return ngrams(words, 3)
def fourgrams(words):
return ngrams(words, 4)
def fivegrams(words):
return ngrams(words, 5)
if __name__ == '__main__':
words = "The quick brown fox jumps over the lazy dog".split(" ")
print("bigrams")
for item in list(bigrams(words)): print(item)
print("trigrams")
for item in list(trigrams(words)): print(item)
print("fourgrams")
for item in list(fourgrams(words)): print(item)
print("fivegrams")
for item in list(fivegrams(words)): print(item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment