-
Notifications
You must be signed in to change notification settings - Fork 0
/
Phonology.py
866 lines (550 loc) · 15.2 KB
/
Phonology.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
'''
Vowel Evolution program Phonology class.
Run in PYTHON 3(!)
with Vowel, Game_fns, Prototype, Word, Agent, Convention, graphics files in same directory
import time, random, re from Python library
Last update November 2017 HJMS
SYLLABLES
Using a grammary organization to represent syllables
Dynamic vowels may be overreaching for this project,
so we've ditched those for now.
We've also made consonant features more fluid.
Syllable => onset + nucleus + coda
Onset => (consonant | null)
Nucleus => vowel
Coda => (consonant | null)
consonant => (coronal | labial | dorsal | laryngeal) + (voiced | voiceless)
vowel => (high | mid | low) + (front | central | back) + (long | short)
We are forbidding vowels in onsets or codas.
* In future we hope to have
Nucleus => (vowel + vowel) | (vowel + null) | (null + vowel)
To accommodate diphthongs
COARTICULATION/FEATURE EFFECTS
If you're wondering how we got these precise values for the formant effects,
the official answer is "we stayed at a Holiday Inn Express last night,
more succinctly FIIK;WMTU."
'''
import Agent, Convention, Vowel
from time import ctime
from graphics import *
import re
from random import sample, choice, uniform, randint
max_e1 = Agent.e1_max()
min_e1 = Agent.e1_min()
max_e2 = Agent.e2_max()
min_e2 = Agent.e2_min()
extra = 1.0
def edge_adj(edge, f):
bound_buff = abs(edge - f) #decrease effect close to boundary
mult = min([1, bound_buff]) #goes to 0 if peripheral, up to 1
return mult
class Phonology:
def __init__(self, features = None):
self.set_features(features)
def set_features(self, fl=None):
'''
Based on Flynn 2012.
Each key needs to be mapped to a function.
Functions can be reused, but keys can't.
'''
self.articulations = {"null": self.null,
"stop": self.stop,
"nasal": self.nasal,
"rhotic": self.rhotic,
"labial": self.labial,
"rounded": self.rounded, #round is a reserved word in Python
"coronal": self.coronal,
"lateral": self.lateral,
"strident": self.strident,
"laryngeal": self.laryngeal,
"fricative": self.fricative,
"posterior": self.posterior,
"laminal": self.laminal,
"dorsal": self.dorsal,
"voiced": self.voiced,
"voiceless": self.voiceless,
"retracted": self.retracted,
"spread": self.spread_glottis, #spread==aspirated
"constricted": self.constricted_glottis,
"palatal": self.palatal, #unlisted in Flynn 12 (not phonological),
#VOWELS
"high": self.high,
"mid": self.mid,
"low": self.low,
"front": self.front,
"central": self.central,
"back": self.back
}
arts = self.articulations
self.features = fl
################################################################################
# FEATURES
################################################################################
def laryngeal(self, syll, pos, assim):
'''
Does nothing
--increase-e1,-decrease-e2--
'''
#unpack the syllable
o, n, c = syll #o <- onset, n <- nucleus, c <- coda
#if assim:
# n = self.lower_v(n)
# n = self.back_v(n)
#else:
# n = self.raise_v(n)
# n = self.front_v(n)
return n
def labial(self, syll, pos, assim):
'''
decrease e1, decrease e2
'''
o, n, c = syll
mi = .25
ma = 1.0
if assim:
n = self.raise_v(n, mi, ma)
n = self.back_v(n, mi, ma)
else:
n = self.lower_v(n, mi, ma)
n = self.front_v(n, mi, ma)
return n
def coronal(self, syll, pos, assim):
'''
increase e1 of preceding vowels
'''
o, n, c = syll
mi = .1
ma = .5
if pos is 2: #coda
if assim:
n = self.raise_v(n, mi, ma)
n = self.front_v(n, mi, ma)
else:
n = self.lower_v(n, mi, ma)
n = self.back_v(n, mi, ma)
return n
def posterior(self, syll, pos, assim):
o, n, c = syll
return n
def laminal(self, syll, pos, assim):
o, n, c = syll
return n
def dorsal(self, syll, pos, assim):
'''
decrease e2
'''
o, n, c = syll
mi = .1
ma = .75
if assim:
n = self.front_v(n, mi, ma)
else:
n = self.back_v(n, mi, ma)
return n
def front(self, syll, pos, assim):
o, n, c = syll
#
amount = uniform(.5, 1.0)
if pos == 1:
if assim:
self.front_v(n, mi, ma)
else:
self.back_v(n, mi, ma)
return n
def high(self, syll, pos, assim):
o, n, c = syll
amount = uniform(.75, 1.25)
if pos == 1:
if assim:
self.lower_v(n, mi, ma)
else:
self.raise_v(n, mi, ma)
return n
def low(self, syll, pos, assim):
o, n, c = syll
mi = 1.25
ma = 2.25
if pos == 1:
if assim:
self.lower_v(n, mi, ma)
else:
self.raise_v(n, mi, ma)
return n
def mid(self, syll, pos, assim):
o, n, c = syll
mi = .75
ma = 2.25
if pos == 1:
e1 = n.e1
e1_0 = e1 - ma
e1_1 = e1 + ma
ne1 = uniform(e1_0, e1_1)
if ne1 > max_e1:
ne1 = max_e1
if ne1 < min_e1:
ne1 = min_e1
n.e1 = ne1
return n
def central(self, syll, pos, assim):
o, n, c = syll
ma = .5
if pos == 1:
e2 = n.e2
e2_0 = e2 - ma
e2_1 = e2 + ma
ne2 = uniform(e2_0, e2_1)
if ne2 > max_e2:
ne2 = max_e2
if ne2 < min_e2:
ne2 = min_e2
n.e2 = ne2
return n
def back(self, syll, pos, assim):
o, n, c = syll
mi = .25
ma = .5
if pos == 1:
if assim:
self.back_v(n, mi, ma)
else:
self.front_v(n, mi, ma)
return n
def retracted(self, syll, pos, assim):
o, n, c = syll
return n
def voiced(self, syll, pos, assim):
o, n, c = syll
#lengthen preceding vowels
if pos is 2:
amount = randint(50, 75)
if assim:
if (n.length + amount) < 300:
n.length += amount
else:
if (n.length - amount) > 75:
n.length -= amount
return n
def voiceless(self, syll, pos, assim):
o, n, c = syll
if pos is 2: #coda
amount = randint(50, 75)
if (assim and ( (n.length - amount) >= 75)):
n.length -= amount
elif (n.length + amount <= 300):
n.length += amount
return n
def spread_glottis(self, syll, pos, assim):
'''
decrease length
'''
o, n, c = syll
l_amount = randint(0, 50) #up to 50 ms
if assim:
if (n.length - l_amount) > 75: #aspirated onset
n.length -= l_amount #reduce length)
else:
if (n.length + l_amount) < 300:
n.length += l_amount
return n
def constricted_glottis(self, syll, pos, assim):
'''
increase e1, decrease e2 of adjacent vowels
'''
o, n, c = syll
if assim:
n = self.lower_v(n)
n = self.back_v(n)
else:
n = self.raise_v(n)
n = self.front_v(n)
return n
def null(self, syll, pos, assim):
o, n, c = syll
return n
def rounded(self, syll, pos, assim):
o, n, c = syll
e1_mi = .25
e1_ma = 1.0
if assim:
n = self.raise_v(n, e1_mi, e1_ma)
else:
n = self.lower_v(n, e1_mi, e1_ma)
e2_mi = .5
e2_ma = 1.0
if assim:
n = self.back_v(n, e2_mi, e2_ma)
else:
n = self.front_v(n, e2_mi, e2_ma)
return n
def palatal(self, syll, pos, assim):
#not a phonological feature, but has a phonetic effect
o, n, c = syll
mi = .1
ma = .75
if assim:
n = self.front_v(n, mi, ma)
else:
n = self.back_v(n, mi, ma)
return n
def lateral(self, syll, pos, assim):
o, n, c = syll
mi = .5
ma = 1.5
if pos == 2:
mi += .25
ma += .25
if assim:
n = self.back_v(n, mi, ma)
else:
n = self.front_v(n, mi, ma)
return n
def rhotic(self, syll, pos, assim):
#hahaha yeah, right.
o, n, c = syll
return n
def fricative(self, syll, pos, assim):
o, n, c = syll
return n
def stop(self, syll, pos, assim):
'''
shorten a preceding vowel's duration
'''
#similar to rounding?
# different if it's guttural?
o, n, c = syll
amount = randint(0, 50) #up to 50 ms
if pos is 2: #stop is in the coda
if (assim and ( (n.length - amount) > 50) ):
n.length -= amount #reduce length
elif ( (n.length + amount) < 300):
n.length += amount
return n
def strident(self, syll, pos, assim):
o, n, c = syll
return n
def nasal(self, syll, pos, assim):
'''
decrease e1
effect increased if nasal is in the coda
'''
o, n, c = syll
mi = .1
ma = .5
if pos is 2:
if assim:
n = self.lower_v(n, mi, ma)
else:
n = self.raise_v(n, mi, ma)
return n
################################################################################
#TRANSFORMATION METHODS#########################################################
def raise_v(self, v, mi = .025, ma = .75):
'''
decrease v.e1 by [mi, ma]
if ma is greater than 0, it will be random from [mi, ma]
if no ma amount is specified, it will use mi
'''
e1_adj = edge_adj(min_e1, v.e1)
if ma:
amount = uniform(mi, ma) * e1_adj * extra #goes to 0 at edges
else:
amount = mi * e1_adj * extra
ne1 = v.e1 - amount
if ne1 > min_e1:
v.e1 = ne1
return v
def lower_v(self, v, mi = .025, ma = .75):
'''
increase v.e1 by [mi, ma]
if ma is greater than 0, it will be random from [mi, ma]
if no ma amount is specified, it will use mi
'''
e1_adj = edge_adj(max_e1, v.e1)
if ma:
amount = uniform(mi, ma) * e1_adj * extra
else:
amount = mi * e1_adj * extra
ne1 = v.e1 + amount
if (ne1 < max_e1 and ne1 < v.e2):
v.e1 = ne1
return v
def front_v(self, v, mi = .025, ma = .75):
'''
increase v.e2 by [mi, ma]
if ma is greater than 0, adjustment will be random from [mi, ma]
if no ma amount is specified, it will use mi
'''
e2_adj = edge_adj(max_e2, v.e2)
if ma:
amount = uniform(mi, ma) * e2_adj * extra
else:
amount = mi * e2_adj * extra
ne2 = v.e2 + amount
if ne2 < max_e2:
v.e2 = ne2
return v
def back_v(self, v, mi = .1, ma = .75):
'''
decrease v.e2 by [mi, ma]
if ma is greater than 0, adjustment will be random from [mi, ma]
if no ma amount is specified, it will use mi
'''
e2_adj = edge_adj(min_e2, v.e2)
if ma:
amount = uniform(mi, ma) * e2_adj * extra
else:
amount = mi * e2_adj * extra
ne2 = v.e2 - amount
if (ne2 > min_e2 and ne2 > v.e1):
v.e2 = ne2
return v
def perceive(agent, convention):
for word in convention:
res = agent.call_matchers(word) #use a different matching function which calls assimilate and de-assimilate
return
def articulations():
p = Phonology()
arts = [print(a) for a in p.articulations.keys()]
def get_feature_dict(language = "english"):
lang = language.lower()
languages = {"english": english}
if lang not in languages:
print(language, "undefined in Phonology.")
return
return languages[lang]()
def english():
'''
Refer to Phonology.articulations for full list
articulations() will print the list
TIPS FOR CHANGING CONSONANTS
1. No duplicate keys, but common substrings are OK
2. Duplicate values are allowed
3. All articulations in the value lists have to be defined in
Phonology.articulations()
AND have to have a method (even if it's just an identity function)
4. There are no limits on how many articulatory methods can be in a value list
5. There are no constraints to ensure coherency of the methods in a value list
e.g. you can have any combination
6. You can change the names of the features,
*but you have to make sure everything gets updated appropriately*
'''
##########################################################
# CHANGE CONSONANTS HERE #
##########################################################
matrix = {"t": ["coronal", "voiceless", "stop", "spread"],
"p": ["labial", "voiceless", "stop", "spread"],
"g": ["dorsal", "voiced", "stop"],
"h": ["laryngeal", "voiceless", "fricative"],
"s": ["coronal", "voiceless", "fricative", "strident"],
"d": ["coronal", "voiced", "stop"], #
"b": ["labial", "voiced", "stop"],
"k": ["dorsal", "voiceless", "spread", "stop"],
"m": ["nasal", "labial"],
"n": ["nasal", "voiced", "coronal"],
"eng": ["dorsal", "nasal"],
"f": ["labial", "fricative", "voiceless"],
"v": ["labial", "fricative", "voiced"],
"theta": ["coronal", "voiceless", "fricative"],
"eth": ["coronal", "voiced", "fricative"],
"z": ["coronal", "voiceless", "fricative", "strident"],
"esh": ["voiceless", "fricative", "strident", "palatal"],
"ezh": ["voiced", "fricative", "strident", "palatal"],
"l": ["coronal", "voiced", "lateral"],
"r": ["coronal", "rhotic", "voiced"], #^identical to l?
"w": ["rounded", "voiced"],
"-": ["null"]
}
return matrix
def dum_v():
from Vowel import Vowel
e1 = uniform(min_e1, max_e1)
e2 = uniform(min_e2, max_e2)
length = randint(100, 300)
v = Vowel(e1, e2, length, "")
name = []
if is_high(v):
name.append("high")
elif is_low(v):
name.append("low")
else:
name.append("mid")
if is_back(v):
name.append("back")
elif is_front(v):
name.append("front")
else:
name.append("central")
if length > 200:
name.append("long")
else:
name.append("short")
v.name = "-".join(name)
return v
def test_symbol(symbol_name, assimilate = True, vowel = None):
'''
Apply the features of the symbol to the vowel.
Assimilate indicates whether the changes are applied (coarticulated)
or 'undone' / deassimilated
If no vowel is provided, a random one will be created.
'''
if not vowel:
vowel = dum_v()
fm = get_feature_dict()
if symbol_name not in fm:
print(symbol_name, "undefined in Phonology.Articulations")
return
symbol = Segment.Segment(symbol_name, fm[symbol_name])
#w = Word.Word(symbol, vowel, "-", vowel)
w = None
child = make_child()
child.chosen = True
print("Original vowel:", vowel, "in syllable", w)
if assimilate:
pv = w.get_vowel()
print("Applying features", fm[symbol])
print("Assimilated vowel:", pv)
else:
pv = child.get_vowel(w)
print("Correcting features", fm[symbol])
print("De-assimilated vowel", pv)
def get_consonant_symbols():
'''print the consonant keys'''
return [c for c in get_feature_set(None).keys()]
def view_coarts(v):
lex = get_rand_lex(v)
def get_rand_lex(v):
#draw the articulation and dearticulation perimeters
#center is v
#borders are defined by coart functions + phone/vowel noise
#from Word import Word
conv = make_convention(v)
c_words = conv.lexicon.values()
cbpd = conv.base_proto_dict
#for cw in c_words:
conv.draw_def_margin()
def is_high(v):
'''True when v occupies the vertical top third of the space'''
min_high = max_e1 - ((max_e1 - min_e1)/3) #above the 2/3 portion
if v.e1 >= min_high:
return True
return False
def is_low(v):
'''True when v occupies the vertical bottom third of the space'''
max_low = min_e1 + ((max_e1 - min_e1)/3) #below the 2/3 portion
if v.e1 <= max_low:
return True
return False
def is_front(v):
'''True when v is in the left-most third of the space'''
min_front = max_e2 - ((max_e2 - min_e2)/3) #front of the space (left)
if v.e2 >= min_front:
return True
else:
return False
def is_back(v):
'''True when v is in the right-most third of the space'''
max_back = min_e2 + ((max_e2 - min_e2)/3) #back of the space (right)
if v.e2 <= max_back:
return True
else:
return False