-
Notifications
You must be signed in to change notification settings - Fork 0
/
phigen.py
137 lines (133 loc) · 5.16 KB
/
phigen.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import md5
import os.path
import math
def _openlist(path):
io = open(path)
res = []
for line in io:
line = line.rstrip()
if line=='':continue
res.append(line)
return res
class Phigen:
def __init__(self):
self._prepare()
def _prepare(self):
datadir = os.path.join(os.path.dirname(__file__),'data')
philosophers = _openlist(os.path.join(datadir, 'philosopher'))
prefixes = _openlist(os.path.join(datadir, 'prefix'))
suffixes = _openlist(os.path.join(datadir, 'suffix'))
words = _openlist(os.path.join(datadir, 'word'))
enemies = _openlist(os.path.join(datadir, 'enemy'))
attacks = _openlist(os.path.join(datadir, 'attack'))
friends = _openlist(os.path.join(datadir, 'friend'))
joins = _openlist(os.path.join(datadir, 'join'))
config = {}
config['philosophers'] = philosophers
config['prefixes'] = prefixes
config['suffixes'] = suffixes
config['words'] = words
config['enemies'] = enemies
config['attacks'] = attacks
config['friends'] = friends
config['joins'] = joins
self._config = config
def _concept(self):
if self._rand(2)==0:
return self._randget(self._config['words'])
else:
return self._randget(self._config['prefixes']) + self._randget(self._config['suffixes'])
def _phi(self, update=False):
if update:
phi = self._randget(self._config['philosophers'])
self._config['philosophers'].remove(phi)
return phi
return self._randget(self._config['philosophers'])
def _phiandcon(self):
phi1 = self._phi()
con1 = self._concept()
cond = "の" if self._rand(2)==0 else "における"
return phi1 + cond + con1
def _how(self):
atype = self._rand(2)
suffixes = ['点で', '点において', 'その姿勢において']
suffix = self._randget(suffixes)
if atype==1:
enemy = self._randget(self._config['enemies'])
attack = self._randget(self._config['attacks'])
base = enemy + attack
else:
friend = self._randget(self._config['friends'])
join = self._randget(self._config['joins'])
base = friend + join
return "%s%s" % (base, suffix)
def _niteru(self, base0, base1=None):
verbs = ["に似ている", "と本質的にひとしい", "と共通点がある", "と類似している", "と根底に共通するものがある"]
verb = self._randget(verbs)
how = self._how()
if base1 is None:
base1 = self._phiandcon()
return "%sは%s、%s%s。" % (base0, how, base1, verb)
def _eikyo(self, base0, base1=None):
verbs = ["の影響がある", "の影響下にある", "を受け継ぐものである", "を徹底させたものに他ならない"]
verb = self._randget(verbs)
how = self._how()
if base1 is None:
base1 = self._phiandcon()
return "%sは%s、%s%s。" % (base0, how, base1, verb)
def _analyze(self, base0, base1=None):
atype = self._rand(2)
if atype == 1:
return self._niteru(base0, base1)
else:
return self._eikyo(base0, base1)
def _randget(self, l):
return l[ self._rand(len(l)) ]
def _rand(self, upper):
dig = int(math.log10(upper)) + 1
p = 0
for i in range(dig):
if len(self._inputnum)==0:
self._inputnum = self._orginput[0:]
p += int(self._inputnum.pop()) * 10 ** i
return p % upper
def _num(self, s):
m = md5.new()
m.update(s.encode('utf-8'))
return int(m.hexdigest(), 16)
def genprint(self, inputstr):
g = self.gen(inputstr)
print g['title']
print g['story']
def gen(self, inputstr):
self._prepare()
l = list(str(self._num(inputstr)))[2:-2]
l.reverse()
print l
self._inputnum = l
self._orginput = self._inputnum[0:]
num = self._rand(3)
template_id = num
if template_id == 0:
phi = self._phi(True)
con = self._concept()
title = "%sにおける%s" % (phi, con)
story = self._analyze("%sの%s" % (phi,con))
elif template_id == 1:
phi0 = self._phi(True)
phi1 = self._phi()
con0 = self._concept()
con1 = self._concept()
title = "%sと%s" % (phi0, phi1)
story = self._analyze("%sにおける%s" % (phi0,con0), "%sの%s" % (phi1, con1))
elif template_id == 2:
phi0 = self._phi(True)
phi1 = self._phi()
con0 = self._concept()
con1 = self._concept()
title = "%sの%sと%sの%s" % (phi0,con0,phi1,con1)
story = self._analyze("%sにおける%s" % (phi0,con0), "%sの%s" % (phi1, con1))
return dict(title=unicode(title, 'utf_8'),
story=unicode(story, 'utf_8'))