Skip to content

Instantly share code, notes, and snippets.

@d6e
Created January 18, 2017 07:16
Show Gist options
  • Save d6e/8018c6ea5fc993f2ba89e06883f66bac to your computer and use it in GitHub Desktop.
Save d6e/8018c6ea5fc993f2ba89e06883f66bac to your computer and use it in GitHub Desktop.
import markovify
import os
import json
import re
directory = os.path.join(os.getcwd(), "general")
filenames = os.listdir(directory)
data = []
for filename in filenames:
fpath = os.path.join(directory, filename)
with open(fpath) as f:
contents = json.load(f)
contents = [m['text'] for m in contents] #list of lists
data.extend(contents)
url_pattern = re.compile(r'<.*>')
codeb_pattern = re.compile(r'`.*`')
text = '\n'.join(data)
text = re.sub(url_pattern, '', text)
text = re.sub(codeb_pattern, '', text)
# Build the model.
text_model = markovify.Text(text)
# Print five randomly-generated sentences
#for i in range(5):
# print(text_model.make_sentence())
# Print three randomly-generated sentences of no more than 140 characters
for i in range(30):
text_model.make_short_sentence(140)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment