def generate_all(strings, max_length=75, descr_depth=2): """ :param strings: (all_titles, all_authors, all_descr) :return: (new_title, new_author, new_descr) Generate the random title/author/description using the Markov chains """ m1 = Markov(prob=True, level=1) m2 = Markov(prob=True, level=1) m3 = Markov(prob=True, level=descr_depth) m1.parse(strings[0]) m2.parse(strings[1]) m3.parse(strings[2]) t_string = kill_chars(generate_title(m1)) a_string = kill_chars(generate_author(m2)) d_string = kill_chars(generate_description(m3, max_length)) #t_string = generate_title(m1) #a_string = generate_author(m2) #d_string = generate_description(m3) t_list = t_string.split() while t_list[-1] in stop_words: t_list = t_list[0:-1] t_string = " ".join(t_list) return (t_string,a_string,d_string)
def handle(self, *args, **options): """Create fake users and statuses for Mowdie.""" from faker import Faker import random from django.conf import settings from PyMarkovTextGenerator import Markov fake = Faker() textgen = Markov(prob=True, level=3) with open(settings.BASE_DIR + "/../john_carter.txt") as file: textgen.parse(file.read()) def update_text(): return textgen.generate( startf=lambda db: random.choice([x for x in db if x[0][0].isupper()]), endf=lambda s: len(s) > 120) Favorite.objects.all().delete() Update.objects.all().delete() User.objects.all().delete() users = [] for _ in range(20): user = User(username=fake.user_name(), email=fake.email()) user.set_password("password") user.save() Profile(user=user).save() users.append(user) user = User(username="******", email="*****@*****.**", is_staff=True, is_superuser=True) user.set_password("password") user.save() Profile(user=user).save() updates = [] for _ in range(100): update = Update(text=update_text(), posted_at=make_aware(fake.date_time_this_year()), user=random.choice(users)) update.save() updates.append(update) combos = random.sample([(user, update) for user in users for update in updates], 200) for user, update in combos: favorite = Favorite(user=user, update=update) favorite.save()
def handle(self, *args, **options): """Create fake users and statuses for Mowdie.""" from faker import Faker import random from django.conf import settings from PyMarkovTextGenerator import Markov fake = Faker() textgen = Markov(prob=True, level=3) with open(settings.BASE_DIR + "/../john_carter.txt") as file: textgen.parse(file.read()) def update_text(): return textgen.generate( startf=lambda db: random.choice([x for x in db if x[0][0].isupper()]), endf=lambda s: len(s) > 120 ) Favorite.objects.all().delete() Update.objects.all().delete() User.objects.all().delete() users = [] for _ in range(20): user = User(username=fake.user_name(), email=fake.email()) user.set_password("password") user.save() Profile(user=user).save() users.append(user) user = User(username="******", email="*****@*****.**", is_staff=True, is_superuser=True) user.set_password("password") user.save() Profile(user=user).save() updates = [] for _ in range(100): update = Update( text=update_text(), posted_at=make_aware(fake.date_time_this_year()), user=random.choice(users) ) update.save() updates.append(update) combos = random.sample([(user, update) for user in users for update in updates], 200) for user, update in combos: favorite = Favorite(user=user, update=update) favorite.save()
class TweetBot(object): def __init__(self, text_file): self.markov_dictionary = Markov() with open(text_file) as file: self.markov_dictionary.parse(file.read()) def _end(self, sentance): last_word = sentance.split()[-1:] if (sentance[-1:] in punctuation and len(sentance.split()) > 10 and last_word not in ("Mr.", "St.")): return True else: return False def cleanup_tweet(self, tweet): """ Does a variety of things to beautify the markov-ed text. """ # Fix quotes # Remove two quotes separated by a space. tweet = tweet.replace('" "', ' ') # If the first quote has a space after, put a quote at the beginning of the line. if search(r'^[^"]+" ', tweet): tweet = '"' + tweet # If the last quote on the line has a space before it put a quote at the end of the line. if search(r' "[^"]+$', tweet): tweet = tweet + '"' return tweet def generate_tweet(self): tweet = "" while tweet == "" or len(tweet) > 135: tweet = self.markov_dictionary.generate(endf=self._end) tweet = self.cleanup_tweet(tweet) return tweet
def __init__(self, text_file): self.markov_dictionary = Markov() with open(text_file) as file: self.markov_dictionary.parse(file.read())
from PyMarkovTextGenerator import Markov def end(s): interpunction = (".", "?", "!") if s[len(s)-1] in interpunction and len(s.split()) > 10: return True else: return False m = Markov(prob=True, level=2) with open("text-test") as file: m.parse(file.read()) print m.generate(endf=end)
def generate_all(strings, max_length=75, descr_depth=2): """ :param strings: (all_titles, all_authors, all_descr) :return: (new_title, new_author, new_descr) Generate the random title/author/description using the Markov chains """ m1 = Markov(prob=True, level=1) m2 = Markov(prob=True, level=1) m3 = Markov(prob=True, level=descr_depth) m1.parse(strings[0]) m2.parse(strings[1]) m3.parse(strings[2]) t_string = kill_chars(generate_title(m1)) a_string = kill_chars(generate_author(m2)) d_string = kill_chars(generate_description(m3, max_length)) #t_string = generate_title(m1) #a_string = generate_author(m2) #d_string = generate_description(m3) t_list = t_string.split() while t_list[-1] in stop_words: t_list = t_list[0:-1] t_string = " ".join(t_list) return (t_string, a_string, d_string)