Beispiel #1
0
def memes_post(user, forum):
    topics = Topic.query.filter(Topic.forum_id == forum.id).all()
    topic = random.choice(topics)
    text_model = utils.load_model(model_fname())
    rand_val = random.random()
    title = topic.title

    if not title.endswith('meme'):
        title += "meme"
    print(title)

    if rand_val < 0.2:
        utils.save_post(forum, user, topic, text_model)
    elif rand_val >= 0.12 and rand_val <= 0.92:
        if random.random() > 0.5:
            gan_path_prefix = "{}/{}/memes_gan/".format(PROJECT_DIR, DATA_DIR)
            meme_files = [
                gan_path_prefix + fname
                for fname in os.listdir(gan_path_prefix)
            ]
            meme_file = random.choice(meme_files)
            url = utils.upload_image(meme_file).link
        else:
            url = google_scraper.run(100, topic.title)
        utils.save_image_post_markov(forum, user, topic, text_model, url)
    else:
        url = google_scraper.run(100, topic.title)
        utils.save_image_post_caption(forum, user, topic, text_model, url)
Beispiel #2
0
def generate_title(subreddit):
    text_model = utils.load_model(model_fname(subreddit, 'title'))
    title = text_model.make_sentence(tries=100)
    if title.startswith("Rex Tillerson Brushes Off Drama"):
        return None

    return title
Beispiel #3
0
def save_thread(user, forum, subreddit):

    thread_name = generate_title(subreddit)

    text_model = utils.load_model(model_fname(subreddit, 'post'))
    post_content = utils.generate_body(text_model)

    post = Post(content=post_content)
    thread = Topic(title=thread_name)
    thread.save(user=user, forum=forum, post=post)
Beispiel #4
0
def load_thread_model(thread_id):

    if os.path.exists(thread_fname(thread_id)):
        return utils.load_model(thread_fname(thread_id))
    else:
        data_objects = RawData.query.filter(
            RawData.thread_id == thread_id).all()
        data_str = get_data_from_threads(data_objects)
        print("new one created!", thread_fname(thread_id))
        return utils.create_model_from_text(data_str, thread_fname(thread_id))
Beispiel #5
0
def get_thread_model(forum, thread_id):
    thread_corpus_size = RawData.query.filter(
        RawData.thread_id == thread_id).count()
    base_model_url = get_base_model_url(forum)

    if thread_corpus_size > THREAD_CORPUS_MIN:
        thread_model = get_combined_model_for_thread(base_model_url, thread_id)
    else:
        thread_model = utils.load_model(base_model_url)

    return thread_model
Beispiel #6
0
def reddit_post(user, forum):
    rand_val = random.random()

    subreddits = ["politics", "The_Donald"]
    subreddit = random.choice(subreddits)

    if rand_val > THREAD_TO_POST_RATIO:
        save_thread(user, forum, subreddit)
    else:
        topics = Topic.query.filter(Topic.forum_id == forum.id).all()
        topic = random.choice(topics)
        text_model = utils.load_model(model_fname(subreddit, 'post'))
        utils.post_or_image(forum, user, topic, text_model)
Beispiel #7
0
def seed_topics():
    f = open("{}/{}/meme_threads.txt".format(PROJECT_DIR, DATA_DIR), 'r')
    memes = f.read().split('\n')

    forum = Forum.query.filter(Forum.id == MEME_FORUM_ID).all()[0]
    text_model = utils.load_model(model_fname())
    users = User.query.all()

    for meme in memes[48:]:
        user = random.choice(users)
        if len(meme) > 1:
            title = meme
            url = google_scraper.run(100, title + " meme")
            post_content = "![]({})".format(url)
            post_content += "\n"
            post_content += "\n"
            post_content += text_model.make_sentence()
            rand_val = random.random()
            if rand_val > 0.6:
                title += " meme"

            post = Post(content=post_content)
            topic = Topic(title=title)
            topic.save(user=user, forum=forum, post=post)
Beispiel #8
0
def get_combined_model_for_thread(base_model_url, thread_id):
    model_a = utils.load_model(base_model_url)
    model_b = load_thread_model(thread_id)
    print('combining models')
    return markovify.combine([model_a, model_b], [1, 2])