-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathlanguage.py
202 lines (180 loc) · 6.64 KB
/
language.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# coding: utf-8
# In[74]:
import random
import re
from collections import defaultdict
# In[75]:
def choose(lst, exponent=2):
x = random.random() ** exponent
return lst[int(x * len(lst))]
# In[98]:
class Language(object):
def __init__(self, phonemes, syll='CVC', ortho={}, wordlength=(1,4), restricts=[]):
self.phonemes = {}
for k, v in phonemes.iteritems():
v = list(v)
random.shuffle(v)
self.phonemes[k] = v
self.syll = syll
self.ortho = ortho
self.wordlength = wordlength
self.morphemes = defaultdict(list)
self.allmorphemes = set()
self.words = defaultdict(list)
self.restricts = restricts
self.genitive = self.morpheme('of', 3)
self.definite = self.morpheme('the', 3)
self.joiner = random.choice(' -')
self.minlength = 6
self.used = []
self.last_n = []
def syllable(self):
while True:
phones = []
for s in self.syll:
if s == '?':
if random.random() > 0.5:
phones = phones[:-1]
else:
p = choose(self.phonemes[s], 1.5)
phones.append(p)
syll = ''.join(phones)
for r in self.restricts:
if re.search(r, syll):
break
else:
return syll
def orthosyll(self):
s = self.syllable()
o = u""
for c in s:
o += self.ortho.get(c, c.lower())
return o
def morpheme(self, key=None, maxlength=None):
morphemes = self.morphemes[key]
n = random.randrange(len(morphemes) + (10 if key is None else 1))
if n < len(morphemes):
return morphemes[n]
for _ in xrange(100):
s = self.orthosyll()
if maxlength and len(s) > maxlength:
continue
if s not in self.allmorphemes:
break
morphemes.append(s)
self.allmorphemes.add(s)
return s
def word(self, key=None):
ws = self.words[key]
while True:
n = random.randrange(len(ws) + (3 if key is None else 2))
if n < len(ws):
if ws[n] in self.last_n:
continue
self.last_n.append(ws[n])
self.last_n = self.last_n[-3:]
return ws[n]
l = random.randrange(*self.wordlength)
keys = [key] + [None for _ in xrange(l-1)]
random.shuffle(keys)
w = ''.join(self.morpheme(k) for k in keys)
ws.append(w)
self.last_n.append(w)
self.last_n = self.last_n[-3:]
return w
def name(self, key=None, genitive=0.5, definite=0.1, minlength=5,
maxlength=12):
while True:
if genitive > random.random():
x = random.random()
w1 = self.word(key if random.random() < 0.6
else None).capitalize()
w2 = self.word(key if random.random() < 0.6
else None).capitalize()
if w1 == w2: continue
if random.random() > 0.5:
p = self.joiner.join([w1, self.genitive, w2])
else:
p = self.joiner.join([w1, w2])
else:
p = self.word(key).capitalize()
if random.random() < definite:
p = self.joiner.join([self.definite, p])
if not hasattr(self, "used"):
self.used = []
for p2 in self.used:
if p in p2 or p2 in p:
break
else:
if minlength <= len(p) <= maxlength:
self.used.append(p)
return p
# In[101]:
vsets = ["AIU", "AEIOU", "AEIOUaei", "AEIOUu", "AIUai", "EOU", "AEIOU@0u"]
csets = ["PTKMNSL", "PTKBDGMNLRSsZzc", "PTKMNH", "HKLMNPW'",
"PTKQVSGRMNnLJ", "TKSsDBQgxMNLRWY", "TKDGMNSs",
"PTKBDGMNzSZcHJW"]
lsets = ["RL", "R", "L", "WY", "RLWY"]
ssets = ["S", "Ss", "SsF"]
fsets = ["MN", "SK", "MNn", 'SsZz']
syllsets = ["CVV?C", "CVC", "CVVC?", "CVC?", "CV", "VC", "CVF", "C?VC", "CVF?",
"CL?VC", "CL?VF", "S?CVC", "S?CVF", "S?CVC?",
"C?VF", "C?VC?", "C?VF?", "C?L?VC", "VC",
"CVL?C?", "C?VL?C", "C?VLC?"
]
vorthos=[{'a': u'á', 'e': u'é', 'i': u'í', 'u': u'ü', '@': u'ä', '0': u'ö'},
{'a': u'au', 'e': u'ei', 'i': u'ie', 'u': u'oo', '@': u'ea', '0': u'ou'},
{'a': u'â', 'e': u'ê', 'i': u'y', 'u': u'w', '@': u'à', '0': u'ô'},
{'a': u'aa', 'e': u'ee', 'i': u'ii', 'u': u'uu', '@': u'ai', '0': u'oo'}]
corthos = [{'n': 'ng', 'x': 'kh', 's': 'sh', 'g': 'gh', 'z': 'zh', 'c': 'ch'},
{'n': u'ñ', 'x': 'x', 's': u'š', 'g': u'gh', 'z': u'ž', 'c': u'č'},
{'n': u'ng', 'x': 'ch', 's': u'sch', 'g': u'gh', 'z': u'ts', 'c': u'tsch'},
{'n': u'ng', 'x': 'c', 's': u'ch', 'g': u'gh', 'z': u'j', 'c': u'tch'},
{'n': u'ng', 'x': 'c', 's': u'x', 'g': u'g', 'z': u'zh', 'c': u'q'}]
restricts = ['Ss', 'sS', 'LR', 'RL', "FS", "Fs", "SS", "ss", r"(.)\1"]
# In[102]:
def get_language():
while True:
cset = choose(csets)
vset = choose(vsets)
syll = choose(syllsets, 1)
if len(cset) ** syll.count("C") * len(vset) * syll.count("V") > 30:
break
fset = choose([cset, random.choice(fsets), cset + random.choice(fsets)])
lset = choose(lsets)
sset = choose(ssets)
ortho = {"'": u"`"}
ortho.update(choose(vorthos))
ortho.update(choose(corthos))
minlength = random.choice([1,2])
if len(syll) < 3:
minlength += 1
maxlength = random.randrange(minlength+1, 7)
l = Language(phonemes={'V': vset,
'C': cset,
'L': lset,
'F': fset,
'S': sset},
syll=syll,
ortho=ortho,
restricts=restricts,
wordlength=(minlength, maxlength))
return l
def show_language(l):
print l.phonemes['V'], l.phonemes['C']
if 'F' in l.syll: print l.phonemes['F'],
if 'L' in l.syll: print l.phonemes['L'],
if 'S' in l.syll: print l.phonemes['S'],
print l.syll
ps = set()
while len(ps) < 10:
ps.add(l.name("city"))
print u', '.join(ps)
ps = set()
while len(ps) < 10:
ps.add(l.name("sea"))
print u', '.join(ps)
print "* * *"
if __name__ == '__main__':
for _ in xrange(20):
show_language(get_language())