-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate.py
53 lines (43 loc) · 1.53 KB
/
generate.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
import markovify, random, sys
def cleanup_sentence(sentence):
is_a_index = sentence.find(' is a ')
sentence = sentence[:is_a_index].title() + sentence[is_a_index:]
return sentence
def is_good_sentence(sentence):
word_count = len(sentence.split())
is_a_index = sentence.find(' is a ')
# Make sure the format is: {Title} is a {description}
if is_a_index == -1:
return False
# Make sure it's sufficiently long
if word_count < 5:
return False
# Only keep titles that are under a few words
title_length = len(sentence[:is_a_index].split())
if title_length > 5:
return False
return True
def generate(num_sentences):
with open('description.txt') as f:
text = f.read()
text_model = markovify.Text(text)
best_sentences = []
sentence_length = 140
while True:
sentence = text_model.make_short_sentence(sentence_length).capitalize()
if is_good_sentence(sentence):
sentence = cleanup_sentence(sentence)
word_count = len(sentence.split())
# For some short sentences, add another sentence
if word_count < 11 and random.random() > 0.5:
second_sentence = text_model.make_short_sentence(sentence_length - len(sentence)).capitalize()
if second_sentence and second_sentence.find(' is a ') == -1:
sentence += " " + second_sentence
best_sentences.append(sentence)
if len(best_sentences) == num_sentences:
break;
return best_sentences
def amazon_lambda_handler(event, context):
return generate(5)
if __name__ == '__main__':
print generate(1)[0]