This repository has been archived by the owner on May 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_type_helpers.py
211 lines (164 loc) · 5.02 KB
/
lib_type_helpers.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
"""
These functions are useful to facilitate programming using immutability.
"""
from lib_types import (
Consonant,
Phonet,
VocalFolds,
Vowel,
VowelLength,
Place,
Manner,
Airstream,
SecondaryArticulation,
)
def is_consonant(phone: Phonet) -> bool:
"""Whether a phone is a consonant"""
return isinstance(phone, Consonant)
def is_vowel(phone: Phonet) -> bool:
"""Whether a phone is a vowel"""
return isinstance(phone, Vowel)
def with_vocal_folds(vocal_folds: VocalFolds, phone: Phonet) -> Phonet:
"""
Change the voicing of a phone to a particular voicing.
"""
if is_consonant(phone):
return Consonant(
vocal_folds,
phone.place,
phone.manner,
phone.airstream,
phone.secondary_articulation,
)
return Vowel(
phone.height, phone.backness, phone.rounding, vocal_folds, phone.vowel_length
)
def with_place(place: Place, phone: Phonet) -> Phonet:
"""
Change the place of articulation of a consonant to a particular place.
Ignore vowels.
"""
# Do not change vowels, since they do not have a place of articulation.
if is_vowel(phone):
return phone
# change the place of articulation of consonants.
return Consonant(
phone.vocal_folds,
place,
phone.manner,
phone.airstream,
phone.secondary_articulation,
)
def with_manner(manner: Manner, phone: Phonet) -> Phonet:
"""
Change the manner of articulation of a consonant to a particular manner.
Ignore vowels.
"""
# Do not change vowels, since they do not have a manner of articulation.
if is_vowel(phone):
return phone
# change the manner of articulation of consonants.
return Consonant(
phone.vocal_folds,
phone.place,
manner,
phone.airstream,
phone.secondary_articulation,
)
def with_airstream(airstream: Airstream, phone: Phonet) -> Phonet:
"""
Change the airstream mechanism of a consonant to a particular one.
Ignore vowels.
"""
# Do not change vowels.
if is_vowel(phone):
return phone
# change the airstream mechanism of consonants.
return Consonant(
phone.vocal_folds,
phone.place,
phone.manner,
airstream,
phone.secondary_articulation,
)
def with_vowel_length(vowel_length: VowelLength, phone: Phonet) -> Phonet:
"""
Change the vowel length of a vowel to a particular voicing.
Ignore consonants.
"""
# Do not change consonants, since they do not have a vowel length.
if is_consonant(phone):
return phone
# Make the vowel extra-short.
return Vowel(
phone.height, phone.backness, phone.rounding, phone.vocal_folds, vowel_length
)
def with_secondary_articulation(
secondary_articulation: SecondaryArticulation, phone: Phonet
) -> Phonet:
"""
Change the secondary articulation of a consonant to a particular one.
Ignore vowels.
"""
# Do not change vowels.
if is_vowel(phone):
return phone
# change the secondary articulation of consonants.
return Consonant(
phone.vocal_folds,
phone.place,
phone.manner,
phone.airstream,
secondary_articulation,
)
def to_extra_short(phone: Phonet) -> Phonet:
"""
Change the length of a vowel to
extra short. Ignore consonants.
"""
return with_vowel_length(VowelLength.EXTRA_SHORT, phone)
def to_normal_length(phone: Phonet) -> Phonet:
"""
Change the length of a vowel to
normal. Ignore consonants.
"""
return with_vowel_length(VowelLength.NORMAL, phone)
def to_long(phone: Phonet) -> Phonet:
"""
Change the length of a vowel to long.
Ignore consonants.
"""
return with_vowel_length(VowelLength.LONG, phone)
def to_half_long(phone: Phonet) -> Phonet:
"""
Change the length of a vowel to half-long.
Ignore consonants.
"""
return with_vowel_length(VowelLength.HALF_LONG, phone)
def to_alveolar(phone: Phonet) -> Phonet:
""" Change the place of articulation to alveolar if phone is a consonant."""
return with_place(Place.ALVEOLAR, phone)
def to_voiced(phone: Phonet) -> Phonet:
"""Change the vocal folds to voiced."""
return with_vocal_folds(VocalFolds.VOICED, phone)
def to_voiceless(phone: Phonet) -> Phonet:
"""Change the vocal folds to voiceless."""
return with_vocal_folds(VocalFolds.VOICELESS, phone)
def to_voiceless_aspirated(phone: Phonet) -> Phonet:
"""Change the vocal folds to voiceless aspirated."""
return with_vocal_folds(VocalFolds.VOICELESS_ASPIRATED, phone)
def to_voiced_aspirated(phone: Phonet) -> Phonet:
"""Change the vocal folds to voiced aspirated."""
return with_vocal_folds(VocalFolds.VOICED_ASPIRATED, phone)
def to_labialized(phone: Phonet) -> Phonet:
"""Change the secondary articulation to labialized."""
return with_secondary_articulation(SecondaryArticulation.LABIALIZED, phone)
def to_palatalized(phone: Phonet) -> Phonet:
"""Change the secondary articulation to palatalized."""
return with_secondary_articulation(SecondaryArticulation.PALATALIZED, phone)
def to_pharyngealized(phone: Phonet) -> Phonet:
"""Change the secondary articulation to pharyngealized."""
return with_secondary_articulation(SecondaryArticulation.PHARYNGEALIZED, phone)
def to_velarized(phone: Phonet) -> Phonet:
"""Change the secondary articulation to velarized."""
return with_secondary_articulation(SecondaryArticulation.VELARIZED, phone)