-
Notifications
You must be signed in to change notification settings - Fork 0
/
Word.py
462 lines (323 loc) · 10.5 KB
/
Word.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
'''Vowel evolution program.
Run with Convention, Prototype, Game_fns, im_game, Agent, Phonology
Last update February 2017 HJMS'''
import Agent, Phonology, Vowel, Segment
max_e1 = Agent.e1_max()
min_e1 = Agent.e1_min()
max_e2 = Agent.e2_max()
min_e2 = Agent.e2_min()
phon_fd = Phonology.get_feature_dict() #consonant compensation methods (feature matrix)
class Word():
'''
Word class
Words exist in Agents' vocabularies.
The "orthography" of Words never changes,
no matter what happens to the pronunciations.
'''
def __init__(self, onset, nucleus, coda, percept = None, pn = 0):
'''
tag is the word ID i.e. String
In phase 2, ID is set by the elder group's pronunciation
e.g. "o:-00" is a word in lexicon which is pronounced with [o:]
'''
tag = "'[{0}][{1}][{2}]'".format(onset, nucleus, coda)
self.id = tag
self.count = 0
self.noise = pn
self.onset = onset
self.nucleus = nucleus
self.coda = coda
self.morphs = True #morphological alternations
if percept:
self.fixed = True #adults' words are fixed
self.vowel_hist = [(percept, 1)]
self.percept = percept
self.vowel = percept
else:
self.fixed = False
self.vowel_hist = []
self.percept = None
self.vowel = None
def __str__(self):
name = ("{0:15}".format(self.id))
return name
def __repr__(self):
return self.id
def set_percept(self, percept):
self.percept = percept
def set_vowel(self, vowel):
self.vowel = vowel
def get_vowel(self):
'''
applies random noise
then assimilation
'''
from random import uniform, randint
fm = phon_fd
P = Phonology.Phonology
onset = self.onset.name
nuc = self.percept
coda = self.coda.name
#apply random noise first
radius = self.noise
if radius > 0:
circle_range = self.circle_range
#radius = perception margin in ERB units
#imitation will be a randomly selected point inside this circle
e1 = nuc.e1
e2 = nuc.e2
l = int(nuc.length)
#find the domain of circle fn to get x-coord (f2 value)
left = e2 + radius
right = e2 - radius
rand_x = uniform(right, left) #generate x
#find the range for y-coord (f1 value at x = rand_x)
floor, c = circle_range(rand_x, e2, e1, radius)
ceiling = min(rand_x, c) #constraint: f1 <= f2
rand_y = uniform(floor, ceiling) #generate y
#constraint: length of imitation needs to be in range [100..300]
#random number in that range and within original length+-50
length_min = max([100, (l - 50)])
length_max = min([length_min+50, 300])
new_e1, new_e2 = rand_y, rand_x
new_length = randint(length_min, length_max)
name = nuc.name #match the name for the incoming signal
#keep the imitations in a range
if new_e1 > max_e1:
new_e1 = max_e1
if new_e2 > max_e2:
new_e2 = max_e2
if new_e1 < min_e1:
new_e1 = min_e1
if new_e2 < min_e2:
new_e2 = min_e2
n_nuc = Vowel.Vowel(new_e1, new_e2, new_length, name)
else:
n_nuc = nuc.cc()
#APPLY COARTICULATION TRANSFORMS
c1 = P(fm[onset])
c2 = P(fm[coda])
nfl = n_nuc.features
ofl = c1.features
cfl = c2.features
#simulated morphological alternations
#chance of a feature being "left off" due to context/use (inflectional/derivational)
#morphable_features = ["stop", "voiced", "fricative", "spread"]
morph_chance = 50
#onset transformations (coart)
for of in ofl:
ofns = c1.articulations
of_nuc = ofns[of]((onset, n_nuc, coda), 0, True)
n_nuc = of_nuc
#nucleus production noise (art)
for nf in nfl:
nf_nuc = ofns[nf]((onset, n_nuc, coda), 1, True)
n_nuc = nf_nuc
#coda transformations (coart)
for cf in cfl:
cfns = c2.articulations
cf_nuc = cfns[cf]( (onset, n_nuc, coda), 2, True)
n_nuc = cf_nuc
return n_nuc
def get_form(self):
from random import randint
#simulated morphological alternations
#chance of a feature being "left off" due to context/use (inflectional/derivational)
#morphable_features = ["stop", "voiced", "fricative", "spread"]
onset = self.onset
nuc = self.nucleus
coda = self.coda
morph_chance = 75 # 75% chance for each feature to be included
ofl = [f for f in onset.features if ((randint(0, 99) < morph_chance) or f is "null")]
cfl = [f for f in coda.features if ((randint(0, 99) < morph_chance) or f is "null")]
onset_cc = Segment.Segment(onset.name, ofl, onset.symbol)
coda_cc = Segment.Segment(coda.name, cfl, coda.symbol)
form = Word(onset_cc, nuc, coda_cc, self.percept, self.noise)
return form
def assimilate(self):
'''
nuc is the Phonology unit (vowel) to be modified.
onset is the preceding syllable unit.
coda is the following syllable unit.
This adjusts the formant values of the vowel
according to ***articulatory constraints***
which we suppose occur as a result of the gestures
involved in transitioning between syllable constituents.
Adjustments are linear functions;
the onset features are applied first.
returns the modified vowel
'''
P = Phonology.Phonology
fm = Phonology.get_feature_dict() #consonant compensation methods (feature matrix)
onset = self.onset
nucleus = self.percept
coda = self.coda
nuc = nucleus.cc()
c1 = P(fm[onset])
c2 = P(fm[coda])
ofl = c1.features
cfl = c2.features
for of in ofl:
ofns = c1.articulations
nucleus = ofns[of]((onset, nuc, coda), 0, True)
for cf in cfl:
cfns = c2.articulations
nucleus = cfns[cf]( (onset, nuc, coda), 2, True)
return nucleus
def get_vowel_random(self):
'''
The vowel is the agent's phonetic representation of a vowel
i.e. their pronunciation of their vowel percept.
generative -> vowel is identical to percept
mod generative -> vowel is noisy production of percept
exemplar -> vowel is average of history (percept is a memory bank)
'''
from random import uniform, randint
p = self.percept
radius = self.noise
if ((not radius) or (not p)):
return p
circle_range = self.circle_range
#radius = perception margin in ERB units
#imitation will be a randomly selected point inside this circle
e1 = p.e1
e2 = p.e2
l = int(p.length)
#find the domain of circle fn to get x-coord (f2 value)
left = e2 + radius
right = e2 - radius
rand_x = uniform(right, left) #generate x
#find the range for y-coord (f1 value at x = rand_x)
floor, c = circle_range(rand_x, e2, e1, radius)
ceiling = min(rand_x, c) #constraint: f1 <= f2
rand_y = uniform(floor, ceiling) #generate y
#constraint: length of imitation needs to be in range [100..300]
#random number in that range and within original length+-50
length_min = max([100, (l - 25)])
length_max = length_min+50
new_e1, new_e2 = rand_y, rand_x
new_length = randint(length_min, length_max)
name = p.name #match the name for the incoming signal
#keep the imitations in a range
if new_e1 > max_e1:
new_e1 = max_e1
if new_e2 > max_e2:
new_e2 = max_e2
if new_e1 < min_e1:
new_e1 = min_e1
if new_e2 < min_e2:
new_e2 = min_e2
new_v = Vowel.Vowel(new_e1, new_e2, new_length, name)
self.set_vowel(new_v)
return new_v
def circle_range(self, x, h, k, r):
a = (x-h)**2
c = r**2.0
ceiling = (c - a)**(.5) + k
floor = 2.0*k - ceiling
return (ceiling, floor)
def add_hist(self, v):
'''
v is the Vowel the agent matched (not the one it heard).
Agent just matched v with this word.
Increase the count for v.
If v is the new lead in frequency, move it to the front;
otherwise, add it to the end of history list.
'''
v_ind = self.vowel_record_i(v)
if (v_ind is None):
self.vowel_hist.append( (v, 1) )
if not self.percept:
self.set_percept(v)
else:
p, c = self.vowel_hist.pop(v_ind)
counts = c+1
if (not self.vowel_hist):
self.vowel_hist.append( (v, counts) )
self.set_percept(v)
elif (counts > self.vowel_hist[0][1] ):
self.vowel_hist.insert(0, (v, counts) )
self.set_percept(v)
#print(v, "assigned to", self.id)
else:
self.vowel_hist.append( (v, counts) )
def has_record(self, v):
'''
v is a Vowel
returns Bool
True if the agent has matched this vowel to word (self) before
False if vowel has no entry in history
'''
for (p, c) in self.vowel_hist:
if p is v:
return True
return False
def vowel_record_i(self, v):
for i in range(len(self.vowel_hist)):
p, c = self.vowel_hist[i]
if v is p:
return i
return None
def merge_absorb(self, wv, sv):
'''
wv is the "weaker vowel" (lower weight)
sv is the "stronger vowel" (higher weight)
one vowel has absorbed the other in agent rep.
Move the words to the stronger vowel.
'''
sv_vci = self.vowel_record_i(sv)
if (sv_vci is not None):
sv_copy, sv_counts = self.vowel_hist.pop(sv_vci)
else:
sv_copy, sv_counts = sv, 0
wv_vci = self.vowel_record_i(wv)
if (wv_vci is not None):
wv_copy, wv_counts = self.vowel_hist.pop(wv_vci)
new_sv_counts = sv_counts + wv_counts
if ( len(self.vowel_hist) is 0 or
new_sv_counts > self.vowel_hist[0][1]):
self.vowel_hist.insert(0, (sv_copy, new_sv_counts))
self.set_percept(sv_copy)
else:
self.vowel_hist.append((sv_copy, new_sv_counts))
else: #something has gone wrong
print("error report:", wv, "not in history:")
for v, c in self.vowel_hist:
print(v, c)
def merge_absorb_rb(self, wv, sv):
'''
one vowel has absorbed the other in agent rep.
Move the words to the stronger vowel.
'''
counts = 0
if ( (wv in self.vowel_hist) and
(sv in self.vowel_hist) and
(wv is not sv)):
counts = self.vowel_hist[wv]
self.vowel_hist[sv] += counts
self.vowel_hist[wv] = 0
def merge_midpoint(self, v1, v2, mv):
'''
v1, v2, mv are Vowels
v1 and v2 have merged into mv
find v1 and v2 in the history,
change them both to mv,
and update mv's weight to ( v1's weight + v2's weight )
'''
v1_vci = self.vowel_record_i(v1) #get v1 frequency count (0 if no entry)
if (v1_vci is not None):
v1_copy, v1_counts = self.vowel_hist.pop(v1_vci)
else:
v1_copy, v1_counts = v1, 0
v2_vci = self.vowel_record_i(v2) #get v2 frequency count (0 if no entry)
if (v2_vci is not None):
v2_copy, v2_counts = self.vowel_hist.pop(v2_vci)
else:
v2_copy, v2_counts = v2, 0
counts = v1_counts + v2_counts
if ( (not self.vowel_hist) or
counts > self.vowel_hist[0][1]):
self.vowel_hist.insert(0, (mv, counts))
self.set_percept(mv)
else:
self.vowel_hist.append( (mv, counts) )