Created
March 3, 2017 08:10
-
-
Save dado3212/97c8f30de08f99f218386c3127f624ee to your computer and use it in GitHub Desktop.
This is a simple script that auto-generates texts using Markov chains based on your Mac iMessage history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = "Alex Beals" | |
import sqlite3, markovify, codecs, warnings, os | |
# Handle errors from running on Python 2.7 (emoji support) | |
warnings.simplefilter("ignore") | |
print("Connecting to chat database...") | |
# Extract all of my texts from the 'db' file | |
conn = sqlite3.connect(os.path.expanduser("~/Library/Messages/chat.db")) | |
c = conn.cursor() | |
c.execute('SELECT text FROM message WHERE text != "" AND is_from_me = 1') | |
texts = c.fetchall() | |
print("Building corpus out of " + "{:,}".format(len(texts)) + " messages...") | |
# Combine all of the messages into one giant string | |
corpus_text = [] | |
for text in [x[0] for x in texts]: | |
corpus_text.append(text) | |
if (text[-1] in "!?."): | |
corpus_text.append(" ") | |
else: | |
corpus_text.append(". ") | |
corpus_text = "".join(corpus_text) | |
print ("Buiding Markov model out of " + "{:,}".format(len(corpus_text)) + " characters...") | |
# Load that text file in, and generate the markov file | |
text_model = markovify.Text(corpus_text) | |
print("Done. Press 'Enter' for a Markov sentence based off of your texts. Type anything else and press 'Enter' to quit.\n") | |
# Print a sentence every time you press 'Enter' | |
while True: | |
line = raw_input() | |
print('\033[2A') | |
if line == "": | |
print text_model.make_sentence() | |
else: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment