if subject.strip() == "": subject = "[no subject]" text_body = args.payload["text"][0] text_in = text_body.split("\n")[0] sender = args.payload["from"][0] print "Sender: %s" % (sender, ) print "Got text: %s" % (text_body, ) print "Using text: %s" % (text_in, ) chain = MarkovChain() f = open("markov_source.json") raw_table = f.read() f.close() chain.parse_table(raw_table) text_words = [word.strip() for word in text_in.split(" ")] text_in = chain.generate_chain(length=len(text_words) + 1, words=text_words) print "New text: %s" % (text_in, ) from_param = from_address if from_name is not None: from_param = (from_address, from_name) message = sendgrid.Message(from_param, subject, text_in) sender_name = None sender_email = sender if sender.find("<") >= 0: sender_name = sender[:sender.find("<") - 1]
text = text.replace('"', "") return text if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('length', type=int, help='Maximum number of words to generate.') parser.add_argument('--lines', type=int, help='Number of lines to generate (default=1).', default=1) args = parser.parse_args() chain = MarkovChain() if not os.path.isfile("table.json"): f = open("source.txt") lines = [christmas_parser(f.read())] f.close() chain.create_table(lines) chain.save_table("table.json") else: f = open("table.json") raw_table = f.read() f.close() chain.parse_table(raw_table) for i in range(0, args.lines): print chain.generate_chain(args.length) + "\n"