-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion_generator.py
46 lines (34 loc) · 1.33 KB
/
question_generator.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
#! /usr/bin/env python
import argparse
import json
import random
PHRASE_START = 'PHRASE_START'
"""This special string represents the starting symbol"""
PHRASE_END = 'PHRASE_END'
"""This special string represents the ending symbol"""
def make_phrase(transition_matrix, word=PHRASE_START):
"""Returns a phrase (string) constructing from following the probabilities
in the transition matrix."""
phrase = ''
while word != PHRASE_END:
previous = word
# Pick a random word using the probabilities.
word = random.choices(
list(transition_matrix[previous].keys()),
list(transition_matrix[previous].values())
)[0]
if word == PHRASE_END:
phrase += '?'
else:
if previous != PHRASE_START:
phrase += ' '
phrase += word
return phrase
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Generate a question from a transition matrix.")
parser.add_argument('input_file', type=argparse.FileType('r', encoding='UTF-8'))
parser.add_argument('--n_questions', type=int, default=1, help='Number of questions to print.')
args = parser.parse_args()
transition_matrix = json.load(args.input_file)
for _ in range(args.n_questions):
print(make_phrase(transition_matrix))