-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig.py
More file actions
70 lines (55 loc) · 2.11 KB
/
Copy pathconfig.py
File metadata and controls
70 lines (55 loc) · 2.11 KB
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
import configparser as parser
import random
class config:
# load the configuration file
def __init__(self, config_filename):
self.load_config(config_filename)
def load_config(self, config_filename):
# create a config parser
config = parser.ConfigParser()
config.optionxform = str
# read the file
config.read(config_filename)
# read the values
dictionary = {}
for section in config.sections():
print('Found section: ' + section)
dictionary[section] = {}
for option in config.options(section):
dictionary[section][option] = config.get(section, option).splitlines()
self.phrases = dictionary['phrases']
if 'defaults' in dictionary and 'subjects' in dictionary:
self.has_subjects = True
self.defaults = dictionary['defaults']
self.subjects = dictionary['subjects']
for subject in self.subjects:
self.subjects[subject] = self.subjects[subject][0].split(',')
print('loaded defaults and subjects')
else:
self.has_subjects = False
def create_subjects(self, number = 0):
if number == 0:
number = int(self.defaults['num_subjects'][0])
if self.has_subjects:
first_subject = random.choice(list(self.subjects))
subjects = [first_subject]
for i in range(1,number):
subjects.append(self.get_adjacent_subject(subjects[i-1]))
self.current_subjects = subjects
else:
pass
def get_adjacent_subject(self, subject):
node = self.subjects[subject]
return random.choice(node)
def get_subject(self):
return random.choice(self.current_subjects)
def get_phrase(self, key):
try:
string_to_return = random.choice(self.phrases[key])
if string_to_return == 'none':
return ''
else:
return string_to_return
except:
print('Could not find phrases with key ' + key)
return ''