-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
35 lines (28 loc) · 925 Bytes
/
common.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
# -*- coding:utf-8 -*-
import dotenv
import random
import yaml
from os.path import join, dirname, abspath
def denv(envkey):
return dotenv.get_key(join(abspath(dirname(__file__)), '.env'), envkey)
def markov():
f = open(join(abspath(dirname(__file__)), 'table.yml'), 'r')
table = yaml.load(f)
f.close()
if table:
period = [u'。', u'!', u'?']
punc = [u'・', u'、', u'「', u'」']
current_wd = random.choice(table.keys())
while current_wd in period or current_wd in punc:
current_wd = random.choice(table.keys())
result = [current_wd]
while current_wd not in period:
next_wd = random.choice(table.get(current_wd))
if not next_wd:
break
result.append(next_wd)
current_wd = next_wd
sentence = ''.join(result)
return sentence
else:
return None