Skip to content

Instantly share code, notes, and snippets.

@efruchter
Last active August 29, 2015 14:12
Show Gist options
  • Save efruchter/6b818811d66716515dac to your computer and use it in GitHub Desktop.
Save efruchter/6b818811d66716515dac to your computer and use it in GitHub Desktop.
MC Ride Trigrams. Enjoy the soothing verse of MC Ride from the comfort of your own computer.
from nltk.util import ngrams
from nltk.model import NgramModel
from nltk.probability import LidstoneProbDist
# Source: should be formatted on multiple lines for each sentence.
SOURCE_TEXT = 'Mystery.txt'
LINE_START = "<line>"
LINE_END = "</line>"
raw_text = open(SOURCE_TEXT).read()
filtered_lines = []
for line in raw_text.splitlines():
if len(line) > 0:
line_fixed = line.lower()
line_fixed = line_fixed.replace(',', ' , ')
line_fixed = ' '.join([LINE_START, line_fixed, LINE_END])
filtered_lines += [line_fixed]
tokens = []
for line in filtered_lines:
tokens += line.split()
grams3 = ngrams(tokens, 3)
estimator = lambda fdist, bins: LidstoneProbDist(fdist, 0.2)
lm = NgramModel(3, grams3, estimator=estimator)
generated_words = lm.generate(200, [LINE_START])
generated_lines = ' '.join(generated_words).split(LINE_START)
for line in generated_lines:
line = line.replace(LINE_END, '')
print line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment