-
Notifications
You must be signed in to change notification settings - Fork 1
/
post.py
138 lines (113 loc) · 4.26 KB
/
post.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
__author__ = "Micah Price"
__email__ = "98mprice@gmail.com"
import config
from keras.models import Sequential, load_model
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.utils.data_utils import get_file
from keras.optimizers import RMSprop
import praw
import numpy as np
import random
import sys
import os
import io
def sample(a, temperature=1.0):
# helper function to sample an index from a probability array
a = np.log(a) / temperature
dist = np.exp(a)/np.sum(np.exp(a))
choices = range(len(a))
return np.random.choice(choices, p=dist)
with io.open('data/comments.txt', encoding='utf-8') as f:
comment_text = f.read()
comment_words = set(comment_text.split())
comment_words = sorted(comment_words)
comment_word_indices = dict((c, i) for i, c in enumerate(comment_words))
comment_indices_word = dict((i, c) for i, c in enumerate(comment_words))
maxlen = 30
comment_list_words=comment_text.split()
comment_model = load_model('models/comments.h5')
with io.open('data/titles.txt', encoding='utf-8') as f:
title_text = f.read()
title_words = set(title_text.split())
title_words = sorted(title_words)
title_word_indices = dict((c, i) for i, c in enumerate(title_words))
title_indices_word = dict((i, c) for i, c in enumerate(title_words))
title_list_words=title_text.split()
title_model = load_model('models/titles.h5')
def generate_comments(length):
start_index = random.randint(0, len(comment_list_words) - maxlen - 1)
diversity = 0.8
sentence = comment_list_words[start_index: start_index + maxlen]
generated = ' '.join(sentence)
comments = []
str = ''
for i in range(length):
x = np.zeros((1, maxlen, len(comment_words)))
for t, word in enumerate(sentence):
x[0, t, comment_word_indices[word]] = 1.
preds = comment_model.predict(x, verbose=0)[0]
next_index = sample(preds, diversity)
next_word = comment_indices_word[next_index]
generated += next_word
del sentence[0]
sentence.append(next_word)
if next_word == '<break>':
comments.append(str)
str = ''
else:
str += ' '
str += next_word
return comments
def generate_title():
start_index = random.randint(0, len(title_list_words) - maxlen - 1)
diversity = 1.2
sentence = title_list_words[start_index: start_index + maxlen]
generated = ' '.join(sentence)
str = ''
for i in range(100):
x = np.zeros((1, maxlen, len(title_words)))
for t, word in enumerate(sentence):
x[0, t, title_word_indices[word]] = 1.
preds = title_model.predict(x, verbose=0)[0]
next_index = sample(preds, diversity)
next_word = title_indices_word[next_index]
generated += next_word
del sentence[0]
sentence.append(next_word)
if next_word == '<break>':
if i >= 2:
break
else:
str += ' '
str += next_word
if len(str) >= 300:
str = str[0:300]
return str
reddit = praw.Reddit(client_id=config.client_id,
client_secret=config.client_secret,
user_agent=config.user_agent,
username=config.username,
password=config.password)
def post_comment_chain(submission, comment_chain):
if len(comment_chain[0]) > 0:
comment = submission.reply(comment_chain[0])
if len(comment_chain) > 1:
for comment_str in comment_chain[1:]:
comment = comment.reply(comment_str)
def get_random_submission():
id = reddit.subreddit('totallynotrobots').random()
submission = praw.models.Submission(reddit, id)
return submission.url
def post_to_reddit():
print(reddit.user.me())
submission = reddit.subreddit('totally_humans').submit(title=generate_title(), url=get_random_submission())
comment_chain_count = random.randint(1, 5)
print('comment_chain_count', comment_chain_count)
for i in range(comment_chain_count):
length = random.randint(5, 80)
print('length', length)
comment_chain = generate_comments(length)
if len(comment_chain) > 0:
post_comment_chain(submission, comment_chain)
post_to_reddit()