-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_markov.py
66 lines (50 loc) · 2.06 KB
/
basic_markov.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
import markovify
import json
# Create markov chain model from text in filename
def generate_model(filename, output_filename=None):
with open(filename) as f:
text = f.read()
model = markovify.Text(text)
if output_filename is not None:
model_json = model.to_json()
with open(output_filename, 'w') as op:
op.write(model_json)
return model
# will attempt 100 times to make an original sentance, but could return "None" if this can't be done.
def generate_sentences_from_model(text_model, count = 1):
ret = []
for i in range(count):
# override to allow generated sentances to match up to 50% of the original
sent = text_model.make_sentence(tries = 100, max_overlap_ratio = 0.5)
ret.append(sent)
return ret
# Get saved model or create a new one, and return `count` sentances generated from that.
# This is the main function to use.
def get_sentences(response_filename='responses.txt', output_filename=None, count=1):
if response_filename is None and output_filename is None:
text_model = generate_model_lambda()
else:
if output_filename is None:
text_model = generate_model(response_filename)
else:
try:
with open(output_filename, 'r') as f:
text_model = markovify.Text.from_json(f.read())
except:
text_model = generate_model(response_filename, output_filename)
return generate_sentences_from_model(text_model, count=count)
def lambda_handler(event, context):
with open("mc_responses.json", 'r') as model_file:
text_model = markovify.Text.from_json(model_file.read())
sentences = generate_sentences_from_model(text_model, 100)
return {
"statusCode": 200,
"body": json.dumps({"result": sentences})
}
# Generate a model without using a file - CURSED
if __name__ == "__main__":
input_filename = "responses.txt"
output_filename = "mc_responses.json"
sent = get_sentences(count = 20)
for s in sent:
print(s)