phonematcher 0.0.0a2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,329 @@
1
+ {
2
+ "source": "Changes to the feature values built from PanPhon, Hayes (2009) and the IPA vowel chart, each with the source that decides it.",
3
+ "overrides": [
4
+ {
5
+ "phone": "ɹ",
6
+ "feature": "hi",
7
+ "panphon": "+",
8
+ "value": "0",
9
+ "source": "Hayes (2009), Introductory Phonology, as converted to CSV by Gorman (github.com/kylebgorman/hayes2009, dataprep/Features.csv)",
10
+ "reason": "Hayes gives the alveolar approximant no tongue-body value; PanPhon's [+high] describes the English bunched r, not the symbol."
11
+ },
12
+ {
13
+ "phone": "ɹ",
14
+ "feature": "round",
15
+ "panphon": "+",
16
+ "value": "-",
17
+ "source": "Hayes (2009), Introductory Phonology, as converted to CSV by Gorman (github.com/kylebgorman/hayes2009, dataprep/Features.csv)",
18
+ "reason": "Hayes gives [ɹ] [-round]; the IPA chart symbol carries no lip rounding."
19
+ }
20
+ ],
21
+ "diacritics": [
22
+ {
23
+ "marker": "̈",
24
+ "name": "Centralized",
25
+ "vowels_only": true,
26
+ "content": {
27
+ "front": "-",
28
+ "back": "-"
29
+ },
30
+ "source": "International Phonetic Association, The IPA Chart (revised to 2015), internationalphoneticassociation.org: the diacritic is named 'Centralized'",
31
+ "reason": "This table reads 'centralized' as a move to the central column of the chart, which is [-front, -back]. PanPhon's rule for it changes no feature."
32
+ },
33
+ {
34
+ "marker": "̘",
35
+ "name": "Advanced tongue root",
36
+ "vowels_only": false,
37
+ "restore": [
38
+ "tense"
39
+ ],
40
+ "content": {
41
+ "atr": "+"
42
+ },
43
+ "source": "Casali (2007), 'Some observations on the phonetics of [ATR] contrasts', lecture, University of Education, Winneba (research.canil.ca/ATRLecture/Phonetics of ATR.pdf), section 2: [ATR] is an expansion or contraction of the pharyngeal cavity, after Lindau (1979), 'The feature expanded', Journal of Phonetics 7",
44
+ "reason": "PanPhon writes ATR as [+tense] and RTR as [-tense]. Here [tense] has no part in vowel height, and ATR is a contrast in the size of the pharyngeal cavity, so the mark sets [atr] and keeps [tense] as the base has it."
45
+ },
46
+ {
47
+ "marker": "̙",
48
+ "name": "Retracted tongue root",
49
+ "vowels_only": false,
50
+ "restore": [
51
+ "tense"
52
+ ],
53
+ "content": {
54
+ "atr": "-"
55
+ },
56
+ "source": "Casali (2007), 'Some observations on the phonetics of [ATR] contrasts', lecture, University of Education, Winneba (research.canil.ca/ATRLecture/Phonetics of ATR.pdf), section 2: [ATR] is an expansion or contraction of the pharyngeal cavity, after Lindau (1979), 'The feature expanded', Journal of Phonetics 7",
57
+ "reason": "PanPhon writes ATR as [+tense] and RTR as [-tense]. Here [tense] has no part in vowel height, and ATR is a contrast in the size of the pharyngeal cavity, so the mark sets [atr] and keeps [tense] as the base has it."
58
+ },
59
+ {
60
+ "marker": "ʲ",
61
+ "name": "Palatalized",
62
+ "vowels_only": false,
63
+ "restore": [
64
+ "hi",
65
+ "back"
66
+ ],
67
+ "content": {
68
+ "palatalized": "+"
69
+ },
70
+ "source": "Clements and Hume (1995), 'The internal organization of speech sounds', in Goldsmith (ed.), The Handbook of Phonological Theory, Blackwell, ch. 7, sections 3.5 and 3.5.1: a secondary articulation is a minor articulation linked under the C-place node beside the primary one",
71
+ "reason": "PanPhon writes the mark with the primary place features ['hi', 'back'], so it overwrote the base's place: in PanPhon kʲ and qʲ share a vector. The secondary articulation adds to the primary place and does not replace it, so the mark sets a feature of its own and the base keeps its place. The feature names are this table's."
72
+ },
73
+ {
74
+ "marker": "ᶣ",
75
+ "name": "Labio-palatalized",
76
+ "vowels_only": false,
77
+ "restore": [
78
+ "hi",
79
+ "back",
80
+ "round"
81
+ ],
82
+ "content": {
83
+ "palatalized": "+",
84
+ "labialized": "+"
85
+ },
86
+ "source": "Clements and Hume (1995), 'The internal organization of speech sounds', in Goldsmith (ed.), The Handbook of Phonological Theory, Blackwell, ch. 7, sections 3.5 and 3.5.1: a secondary articulation is a minor articulation linked under the C-place node beside the primary one",
87
+ "reason": "PanPhon writes the mark with the primary place features ['hi', 'back', 'round'], so it overwrote the base's place: in PanPhon kʲ and qʲ share a vector. The secondary articulation adds to the primary place and does not replace it, so the mark sets a feature of its own and the base keeps its place. The feature names are this table's."
88
+ },
89
+ {
90
+ "marker": "ʷ",
91
+ "name": "Labialized",
92
+ "vowels_only": false,
93
+ "restore": [
94
+ "hi",
95
+ "back",
96
+ "round"
97
+ ],
98
+ "content": {
99
+ "labialized": "+"
100
+ },
101
+ "source": "Clements and Hume (1995), 'The internal organization of speech sounds', in Goldsmith (ed.), The Handbook of Phonological Theory, Blackwell, ch. 7, sections 3.5 and 3.5.1: a secondary articulation is a minor articulation linked under the C-place node beside the primary one",
102
+ "reason": "PanPhon writes the mark with the primary place features ['hi', 'back', 'round'], so it overwrote the base's place: in PanPhon kʲ and qʲ share a vector. The secondary articulation adds to the primary place and does not replace it, so the mark sets a feature of its own and the base keeps its place. The feature names are this table's."
103
+ },
104
+ {
105
+ "marker": "ˠ",
106
+ "name": "Velarized",
107
+ "vowels_only": false,
108
+ "restore": [
109
+ "hi",
110
+ "back"
111
+ ],
112
+ "content": {
113
+ "velarized": "+"
114
+ },
115
+ "source": "Clements and Hume (1995), 'The internal organization of speech sounds', in Goldsmith (ed.), The Handbook of Phonological Theory, Blackwell, ch. 7, sections 3.5 and 3.5.1: a secondary articulation is a minor articulation linked under the C-place node beside the primary one",
116
+ "reason": "PanPhon writes the mark with the primary place features ['hi', 'back'], so it overwrote the base's place: in PanPhon kʲ and qʲ share a vector. The secondary articulation adds to the primary place and does not replace it, so the mark sets a feature of its own and the base keeps its place. The feature names are this table's."
117
+ },
118
+ {
119
+ "marker": "̴",
120
+ "name": "Velarized or pharyngealized",
121
+ "vowels_only": false,
122
+ "restore": [
123
+ "hi",
124
+ "back"
125
+ ],
126
+ "content": {
127
+ "velarized": "0",
128
+ "pharyngealized": "0"
129
+ },
130
+ "source": "International Phonetic Association, The IPA Chart (revised to 2015), internationalphoneticassociation.org: the diacritic is named 'Velarized or pharyngealized'",
131
+ "reason": "The chart leaves the place open, so the mark sets both [velarized] and [pharyngealized] to 0: one of the two, not known which. The base keeps its place."
132
+ },
133
+ {
134
+ "marker": "ˤ",
135
+ "name": "Pharyngealized",
136
+ "vowels_only": false,
137
+ "restore": [
138
+ "lo",
139
+ "back"
140
+ ],
141
+ "content": {
142
+ "pharyngealized": "+",
143
+ "atr": "-"
144
+ },
145
+ "source": "McCarthy (1994), 'The phonetics and phonology of Semitic pharyngeals', in Keating (ed.), Phonological Structure and Phonetic Form: Papers in Laboratory Phonology III, Cambridge University Press, pp. 218-219: the emphatics have 'a constriction in the upper pharynx' and 'should really be called uvularized'; Hermes, Barlaz, Shosted, Liang and Sutton (2017), Interspeech 2017, pp. 201-205; Al-Solami (2017), Canadian Acoustics 45(1), pp. 25-35: emphatics show 'tongue root and tongue dorsum retraction with inconsistent tongue root retraction'; Clements and Hume (1995), section 3.5: 'pharyngealization, involving the retraction of the tongue root'",
146
+ "reason": "PanPhon writes the mark as [+low, +back], which overwrote the base's place. Pharyngealized (emphatic) sounds constrict the upper pharynx with the tongue dorsum, at a different place from the pharyngeals (Hermes et al. 2017), and retract the tongue root inconsistently (Al-Solami 2017). The mark sets its own feature, [+pharyngealized], plus [-atr] for the tongue-root part, and keeps the base's place."
147
+ },
148
+ {
149
+ "marker": "˞",
150
+ "name": "Rhoticity",
151
+ "vowels_only": false,
152
+ "restore": [
153
+ "hi",
154
+ "round"
155
+ ],
156
+ "content": {},
157
+ "source": "International Phonetic Association, The IPA Chart (revised to 2015), internationalphoneticassociation.org: the diacritic is named 'Rhoticity'",
158
+ "reason": "PanPhon's rule gives [+high, +round, -anterior], so [ɚ] came out high and rounded. The chart names the mark only. This table reads it as a colouring that keeps the vowel's height and rounding, and keeps PanPhon's [-anterior]."
159
+ },
160
+ {
161
+ "marker": "̥",
162
+ "name": "Voiceless",
163
+ "vowels_only": false,
164
+ "content": {
165
+ "voi": "-"
166
+ },
167
+ "source": "International Phonetic Association, The IPA Chart (revised to 2015), internationalphoneticassociation.org: the diacritic is named 'Voiceless'",
168
+ "reason": "PanPhon applies the mark only to voiced sonorants, so [d̥] could not be read. It devoices any base.",
169
+ "conditions": []
170
+ },
171
+ {
172
+ "marker": "̬",
173
+ "name": "Voiced",
174
+ "vowels_only": false,
175
+ "content": {
176
+ "voi": "+"
177
+ },
178
+ "source": "International Phonetic Association, The IPA Chart (revised to 2015), internationalphoneticassociation.org: the diacritic is named 'Voiced'",
179
+ "reason": "PanPhon has no rule for the mark. It voices any base.",
180
+ "conditions": []
181
+ },
182
+ {
183
+ "marker": "̝",
184
+ "name": "Raised",
185
+ "vowels_only": false,
186
+ "content": {},
187
+ "source": "International Phonetic Association, The IPA Chart (revised to 2015), internationalphoneticassociation.org: the diacritic is named 'Raised'",
188
+ "reason": "PanPhon's rule changes no feature. This table reads the mark as one chart row closer.",
189
+ "shift": {
190
+ "height": -1
191
+ }
192
+ },
193
+ {
194
+ "marker": "̞",
195
+ "name": "Lowered",
196
+ "vowels_only": false,
197
+ "content": {},
198
+ "source": "International Phonetic Association, The IPA Chart (revised to 2015), internationalphoneticassociation.org: the diacritic is named 'Lowered'",
199
+ "reason": "PanPhon's rule changes no feature. This table reads the mark as one chart row more open.",
200
+ "shift": {
201
+ "height": 1
202
+ }
203
+ },
204
+ {
205
+ "marker": "̟",
206
+ "name": "Advanced",
207
+ "vowels_only": false,
208
+ "content": {},
209
+ "source": "International Phonetic Association, The IPA Chart (revised to 2015), internationalphoneticassociation.org: the diacritic is named 'Advanced'",
210
+ "reason": "PanPhon's rule changes no feature. On a vowel this table reads the mark as one chart column further front.",
211
+ "shift": {
212
+ "column": -1
213
+ }
214
+ },
215
+ {
216
+ "marker": "̠",
217
+ "name": "Retracted",
218
+ "vowels_only": false,
219
+ "content": {},
220
+ "source": "International Phonetic Association, The IPA Chart (revised to 2015), internationalphoneticassociation.org: the diacritic is named 'Retracted'",
221
+ "reason": "PanPhon's rule changes no feature. On a vowel this table reads the mark as one chart column further back.",
222
+ "shift": {
223
+ "column": 1
224
+ }
225
+ },
226
+ {
227
+ "marker": "̹",
228
+ "name": "More rounded",
229
+ "vowels_only": false,
230
+ "content": {},
231
+ "source": "International Phonetic Association, The IPA Chart (revised to 2015), internationalphoneticassociation.org: the diacritic is named 'More rounded'",
232
+ "reason": "PanPhon has no rule for the mark. On a vowel this table reads it as one step up the rounding scale -, 0, +.",
233
+ "shift": {
234
+ "round": 1
235
+ },
236
+ "conditions": []
237
+ },
238
+ {
239
+ "marker": "̜",
240
+ "name": "Less rounded",
241
+ "vowels_only": false,
242
+ "content": {},
243
+ "source": "International Phonetic Association, The IPA Chart (revised to 2015), internationalphoneticassociation.org: the diacritic is named 'Less rounded'",
244
+ "reason": "PanPhon has no rule for the mark. On a vowel this table reads it as one step down the rounding scale -, 0, +.",
245
+ "shift": {
246
+ "round": -1
247
+ },
248
+ "conditions": []
249
+ },
250
+ {
251
+ "marker": "̽",
252
+ "name": "Mid-centralized",
253
+ "vowels_only": false,
254
+ "content": {},
255
+ "source": "International Phonetic Association, The IPA Chart (revised to 2015), internationalphoneticassociation.org: the diacritic is named 'Mid-centralized'",
256
+ "reason": "PanPhon has no rule for the mark. On a vowel this table reads it as the central column, one row nearer mid.",
257
+ "shift": {
258
+ "central": 1,
259
+ "toward_mid": 1
260
+ },
261
+ "conditions": []
262
+ },
263
+ {
264
+ "marker": "̚",
265
+ "name": "No audible release",
266
+ "vowels_only": false,
267
+ "content": {},
268
+ "source": "International Phonetic Association, The IPA Chart (revised to 2015), internationalphoneticassociation.org: the diacritic is named 'No audible release'",
269
+ "reason": "PanPhon has no rule for the mark. No feature of this table encodes release, so the mark is read and changes nothing.",
270
+ "conditions": []
271
+ },
272
+ {
273
+ "marker": "ˑ",
274
+ "name": "Half-long",
275
+ "vowels_only": false,
276
+ "content": {
277
+ "long": "0"
278
+ },
279
+ "source": "International Phonetic Association, The IPA Chart (revised to 2015), internationalphoneticassociation.org: the diacritic is named 'Half-long'",
280
+ "reason": "PanPhon has no rule for the mark. It sets [long] to 0, between short and long.",
281
+ "conditions": []
282
+ }
283
+ ],
284
+ "segments": [
285
+ {
286
+ "segment": "ⱱ",
287
+ "name": "voiced labiodental flap",
288
+ "from": "ɾ",
289
+ "place_of": "v",
290
+ "set": {
291
+ "tap": "+",
292
+ "trill": "-"
293
+ },
294
+ "source": "International Phonetic Association, The IPA Chart (revised to 2015), internationalphoneticassociation.org: pulmonic consonants, Tap or Flap row, Labiodental column",
295
+ "reason": "PanPhon does not list the symbol. It is a tap at the labiodental place: the manner of [ɾ] with the place of [v]."
296
+ },
297
+ {
298
+ "segment": "ʜ",
299
+ "name": "voiceless epiglottal fricative",
300
+ "from": "ħ",
301
+ "set": {
302
+ "trill": "+"
303
+ },
304
+ "source": "Hassan, Esling, Moisik and Crevier-Buchman (2011), 'Aryepiglottic trilled variants of /ʕ, ħ/ in Iraqi Arabic', Proceedings of ICPhS XVII, Hong Kong, pp. 831-834: 'both pharyngeals and epiglottals occur at the aryepiglottic sphincter point of stricture', and sounds described as epiglottal 'are associated either with AE trilled varieties'",
305
+ "reason": "PanPhon does not list the symbol. It is the pharyngeal fricative [ħ] made by the aryepiglottic folds, with trilling."
306
+ },
307
+ {
308
+ "segment": "ʢ",
309
+ "name": "voiced epiglottal fricative",
310
+ "from": "ʕ",
311
+ "set": {
312
+ "trill": "+"
313
+ },
314
+ "source": "Hassan, Esling, Moisik and Crevier-Buchman (2011), 'Aryepiglottic trilled variants of /ʕ, ħ/ in Iraqi Arabic', Proceedings of ICPhS XVII, Hong Kong, pp. 831-834: 'both pharyngeals and epiglottals occur at the aryepiglottic sphincter point of stricture', and sounds described as epiglottal 'are associated either with AE trilled varieties'",
315
+ "reason": "PanPhon does not list the symbol. It is the voiced counterpart of [ʜ]."
316
+ },
317
+ {
318
+ "segment": "ʡ",
319
+ "name": "epiglottal plosive",
320
+ "from": "ħ",
321
+ "set": {
322
+ "cont": "-",
323
+ "voi": "-"
324
+ },
325
+ "source": "International Phonetic Association, The IPA Chart (revised to 2015), Other Symbols: 'Epiglottal plosive'; Hassan, Esling, Moisik and Crevier-Buchman (2011), 'Aryepiglottic trilled variants of /ʕ, ħ/ in Iraqi Arabic', Proceedings of ICPhS XVII, Hong Kong, pp. 831-834: 'both pharyngeals and epiglottals occur at the aryepiglottic sphincter point of stricture', and sounds described as epiglottal 'are associated either with AE trilled varieties'",
326
+ "reason": "PanPhon does not list the symbol. The chart lists it under Other Symbols as 'Epiglottal plosive'. It is built as a voiceless stop at the pharyngeal place."
327
+ }
328
+ ]
329
+ }
@@ -0,0 +1,87 @@
1
+ """Distances between IPA strings, built on the segment distance in :mod:`phonematcher.features`."""
2
+ import unicodedata
3
+ from typing import List, Sequence
4
+
5
+ from phonematcher.features import POST_DIACRITICS, PRE_DIACRITICS, is_vowel_phone, phonetic_distance
6
+
7
+ #: Stress, syllable and boundary marks: they carry no segment of their own.
8
+ SUPRASEGMENTALS = frozenset("ˈˌ.‿|‖")
9
+
10
+ _TIES = ("͡", "͜")
11
+
12
+
13
+ def segments(ipa: str) -> List[str]:
14
+ """Split *ipa* into segments.
15
+
16
+ A segment is a base symbol with the diacritics and modifier letters that follow it. A tie bar
17
+ joins the symbols on both sides into one segment, so ``t͡ʃ`` is one affricate. Spaces become a
18
+ ``" "`` segment between words; stress and syllable marks are dropped.
19
+ """
20
+ out: List[str] = []
21
+ join_next = False
22
+ pending = "" # marks written before the base they belong to
23
+
24
+ def release_pending() -> None:
25
+ """A held mark with no following base belongs to the segment before it, if any."""
26
+ nonlocal pending
27
+ if pending:
28
+ if out and out[-1] != " ":
29
+ out[-1] += pending
30
+ else:
31
+ out.append(pending)
32
+ pending = ""
33
+
34
+ for ch in unicodedata.normalize("NFD", ipa):
35
+ if ch in SUPRASEGMENTALS:
36
+ continue
37
+ if ch.isspace():
38
+ release_pending()
39
+ if out and out[-1] != " ":
40
+ out.append(" ")
41
+ continue
42
+ if ch in _TIES:
43
+ join_next = bool(out)
44
+ if join_next:
45
+ out[-1] += ch
46
+ continue
47
+ starts_word = not out or out[-1] == " "
48
+ if ch in PRE_DIACRITICS and (starts_word or is_vowel_phone(out[-1])) and not join_next:
49
+ pending += ch
50
+ continue
51
+ attaches = unicodedata.combining(ch) or ch in POST_DIACRITICS
52
+ if not pending and out and out[-1] != " " and (attaches or join_next):
53
+ out[-1] += ch
54
+ else:
55
+ out.append(pending + ch)
56
+ pending = ""
57
+ join_next = False
58
+ release_pending()
59
+ if out and out[-1] == " ":
60
+ out.pop()
61
+ return [unicodedata.normalize("NFC", s) for s in out]
62
+
63
+
64
+ def substitution_cost(a: str, b: str) -> float:
65
+ """Cost of replacing segment *a* by *b*: their feature distance, from 0 to 1, so a
66
+ substitution never costs more than a deletion plus an insertion."""
67
+ return phonetic_distance(a, b)
68
+
69
+
70
+ def edit_distance(a: Sequence[str], b: Sequence[str]) -> float:
71
+ """Levenshtein distance between two segment sequences, with feature-weighted substitutions.
72
+
73
+ Deletion and insertion cost 1. Pass the output of :func:`segments`, or call
74
+ :func:`ipa_distance` on strings.
75
+ """
76
+ row = [float(j) for j in range(len(b) + 1)]
77
+ for i, seg in enumerate(a, start=1):
78
+ prev, row[0] = row[0], float(i)
79
+ for j in range(1, len(b) + 1):
80
+ cur = min(row[j] + 1.0, row[j - 1] + 1.0, prev + substitution_cost(seg, b[j - 1]))
81
+ prev, row[j] = row[j], cur
82
+ return row[-1]
83
+
84
+
85
+ def ipa_distance(a: str, b: str) -> float:
86
+ """:func:`edit_distance` between two IPA strings."""
87
+ return edit_distance(segments(a), segments(b))
@@ -0,0 +1,145 @@
1
+ """Distinctive features of IPA segments, and the weighted distance between two segments.
2
+
3
+ The table is PanPhon's (Mortensen et al. 2016, "PanPhon: A Resource for Mapping IPA Segments to
4
+ Articulatory Feature Vectors", COLING; MIT License), with the changes listed in
5
+ ``data/overrides.json``, each of which names its source. ``scripts/build_features.py`` builds
6
+ ``data/features.json`` from both.
7
+
8
+ A feature value is ``True`` (+), ``False`` (−) or ``None`` (0: the feature does not apply).
9
+ """
10
+ import json
11
+ import unicodedata
12
+ from functools import lru_cache
13
+ from pathlib import Path
14
+ from typing import Dict, Optional, Tuple
15
+
16
+ from phonematcher.scales import shift
17
+
18
+ _DATA = json.loads((Path(__file__).parent / "data" / "features.json").read_text(encoding="utf-8"))
19
+
20
+ #: Feature names, in vector order.
21
+ FEATURES: Tuple[str, ...] = tuple(_DATA["features"])
22
+ #: PanPhon's weight for each feature.
23
+ WEIGHTS: Tuple[float, ...] = tuple(_DATA["weights"])
24
+ NUM_FEATURES = len(FEATURES)
25
+ SOURCE: str = _DATA["source"]
26
+
27
+ Vector = Tuple[Optional[bool], ...]
28
+
29
+ _VALUE = {"+": True, "-": False, "0": None}
30
+
31
+ #: Segment (NFD) -> feature vector.
32
+ phone_features: Dict[str, Vector] = {
33
+ seg: tuple(_VALUE[c] for c in vec) for seg, vec in _DATA["phones"].items()
34
+ }
35
+
36
+ _DIACRITICS: Dict[Tuple[str, str], dict] = {
37
+ (d["marker"], d["position"]): d for d in _DATA["diacritics"]
38
+ }
39
+ #: Diacritics written after their base.
40
+ POST_DIACRITICS = {marker for marker, position in _DIACRITICS if position == "post"}
41
+ #: Marks written before a stop: prenasalization takes PanPhon's rule for ◌ⁿ (the nasal's place
42
+ #: is the stop's), preaspiration its rule for ◌ʰ.
43
+ PRE_DIACRITICS = {
44
+ **{marker: (marker, "pre") for marker, position in _DIACRITICS if position == "pre"},
45
+ "ⁿ": ("ⁿ", "post"), "ᵐ": ("ⁿ", "post"), "ᵑ": ("ⁿ", "post"), "ʰ": ("ʰ", "post"),
46
+ }
47
+ _TIES = ("͡", "͜")
48
+ #: Letters that Unicode does not decompose into a base and its mark: the rhotic vowels, and [ɫ],
49
+ #: the IPA chart's example of ◌̴ (velarized or pharyngealized).
50
+ _COMPOSED_LETTERS = str.maketrans({"ɚ": "ə˞", "ɝ": "ɜ˞", "ɫ": "l\u0334"})
51
+
52
+
53
+ def _applies(diacritic: dict, vec: Vector) -> bool:
54
+ conditions = diacritic["conditions"]
55
+ return not conditions or any(
56
+ all(vec[FEATURES.index(f)] is _VALUE[v] for f, v in cond.items()) for cond in conditions
57
+ )
58
+
59
+
60
+ def _apply(diacritic: dict, vec: Vector) -> Vector:
61
+ out = list(vec)
62
+ for f, v in diacritic["content"].items():
63
+ out[FEATURES.index(f)] = _VALUE[v]
64
+ return tuple(out)
65
+
66
+
67
+ @lru_cache(maxsize=4096)
68
+ def vectorize_phones(segment: str) -> Vector:
69
+ """The feature vector of one IPA *segment*.
70
+
71
+ A segment in the table is read as listed. Otherwise the longest listed base in it takes the
72
+ diacritics around it, each by PanPhon's rule for that diacritic, as long as the rule's
73
+ conditions hold. Raises ``ValueError`` for a segment it cannot read.
74
+ """
75
+ seg = unicodedata.normalize("NFD", segment).translate(_COMPOSED_LETTERS)
76
+ if seg in phone_features:
77
+ return phone_features[seg]
78
+ untied = seg.replace(_TIES[1], _TIES[0])
79
+ if untied in phone_features:
80
+ return phone_features[untied]
81
+
82
+ start = 0
83
+ while start < len(seg) and seg[start] in PRE_DIACRITICS:
84
+ start += 1
85
+ for end in range(len(seg), start, -1):
86
+ base = seg[start:end]
87
+ if base in phone_features and all(m in POST_DIACRITICS for m in seg[end:]):
88
+ vec = phone_features[base]
89
+ for marker in seg[:start]:
90
+ vec = _apply_if(_DIACRITICS[PRE_DIACRITICS[marker]], vec, segment)
91
+ for marker in seg[end:]:
92
+ vec = _apply_if(_DIACRITICS[marker, "post"], vec, segment)
93
+ return vec
94
+ raise ValueError(f"Unrecognized phone {segment!r}")
95
+
96
+
97
+ _HI, _LO = FEATURES.index("hi"), FEATURES.index("lo")
98
+ _SIGN = {True: "+", False: "-", None: "0"}
99
+
100
+
101
+ def _apply_if(diacritic: dict, vec: Vector, segment: str) -> Vector:
102
+ """Apply one diacritic. [+high, +low] is impossible, so a mark that adds one keeps the
103
+ other as the base has it."""
104
+ if not _applies(diacritic, vec):
105
+ raise ValueError(f"{diacritic['marker']!r} does not apply to the base of {segment!r}")
106
+ out = list(_apply(diacritic, vec))
107
+ if "shift" in diacritic:
108
+ signs = {f: _SIGN[v] for f, v in zip(FEATURES, out)}
109
+ shift(signs, diacritic["shift"])
110
+ out = [_VALUE[signs[f]] for f in FEATURES]
111
+ if out[_HI] is True and out[_LO] is True:
112
+ out[_LO if vec[_HI] is True else _HI] = False
113
+ return tuple(out)
114
+
115
+
116
+ def is_vowel_phone(segment: str) -> bool:
117
+ """True for a syllabic segment."""
118
+ try:
119
+ return vectorize_phones(segment)[0] is True
120
+ except ValueError:
121
+ return False
122
+
123
+
124
+ def _number(value: Optional[bool]) -> int:
125
+ return 1 if value is True else -1 if value is False else 0
126
+
127
+
128
+ def phonetic_distance(a: str, b: str) -> float:
129
+ """Weighted feature distance between two segments, from 0 (same features) to 1.
130
+
131
+ Each feature contributes its PanPhon weight times half the difference of its values, with
132
+ + = 1, 0 = 0 and − = −1, so a +/− mismatch costs the full weight and a 0 against + or − costs
133
+ half. The sum is divided by the total weight. A segment that cannot be read is at distance 1
134
+ from every other segment. A space matches only a space.
135
+ """
136
+ if a == b:
137
+ return 0.0
138
+ if a == " " or b == " ":
139
+ return 1.0
140
+ try:
141
+ va, vb = vectorize_phones(a), vectorize_phones(b)
142
+ except ValueError:
143
+ return 1.0
144
+ diff = sum(w * abs(_number(x) - _number(y)) / 2 for w, x, y in zip(WEIGHTS, va, vb))
145
+ return diff / sum(WEIGHTS)
phonematcher/phonex.py ADDED
@@ -0,0 +1,15 @@
1
+ """Phonex keys: an IPA word as the sequence of its phone clusters.
2
+
3
+ Adapted from pyphone (https://github.com/lingz/pyphone), MIT License.
4
+ """
5
+ from typing import Callable, Sequence, Tuple
6
+
7
+
8
+ def phonex(word: Sequence[str], cluster_of: Callable[[str], int]) -> Tuple[int, ...]:
9
+ """The cluster ids of the segments of *word*, with repeats of one cluster collapsed."""
10
+ key = []
11
+ for seg in word:
12
+ cid = cluster_of(seg)
13
+ if not key or key[-1] != cid:
14
+ key.append(cid)
15
+ return tuple(key)
phonematcher/scales.py ADDED
@@ -0,0 +1,47 @@
1
+ """Ordered scales of the IPA vowel chart, on which some diacritics move a vowel one step.
2
+
3
+ A vector here is a mapping from feature name to "+", "-" or "0". Heights run from close (row 0)
4
+ to open (row 6) through the step features h1 ... h6; columns run front, central, back through
5
+ [front] and [back]; rounding runs -, 0, + through [round] and [labial].
6
+ """
7
+ from typing import Dict, MutableMapping
8
+
9
+ STEPS = tuple(f"h{k}" for k in range(1, 7))
10
+ COLUMNS = (("+", "-"), ("-", "-"), ("-", "+")) # (front, back): front, central, back
11
+ ROUNDING = ("-", "0", "+")
12
+ MID = 3
13
+
14
+
15
+ def _clamp(value: int, top: int) -> int:
16
+ return max(0, min(top, value))
17
+
18
+
19
+ def set_height(vec: MutableMapping[str, str], row: int) -> None:
20
+ row = _clamp(row, len(STEPS))
21
+ vec.update({s: "+" if row >= k else "-" for k, s in enumerate(STEPS, 1)})
22
+ vec["hi"] = "+" if row <= 1 else "-"
23
+ vec["lo"] = "+" if row >= 5 else "-"
24
+
25
+
26
+ def shift(vec: MutableMapping[str, str], moves: Dict[str, int]) -> None:
27
+ """Move a chart vowel along the scales in *moves*; any other segment is left as it is.
28
+
29
+ ``height`` moves the row (+1 is one row more open), ``toward_mid`` moves the row one step
30
+ toward mid, ``column`` moves the column (+1 is one column further back), ``central`` sets the
31
+ central column, and ``round`` moves the rounding (+1 is more rounded).
32
+ """
33
+ if vec.get("syl") != "+" or vec.get(STEPS[0], "0") == "0":
34
+ return
35
+ row = sum(vec[s] == "+" for s in STEPS)
36
+ if "height" in moves:
37
+ set_height(vec, row + moves["height"])
38
+ if moves.get("toward_mid"):
39
+ set_height(vec, row + (MID > row) - (MID < row))
40
+ column = COLUMNS.index((vec["front"], vec["back"])) if (vec["front"], vec["back"]) in COLUMNS else 1
41
+ if "column" in moves:
42
+ vec["front"], vec["back"] = COLUMNS[_clamp(column + moves["column"], 2)]
43
+ if moves.get("central"):
44
+ vec["front"], vec["back"] = COLUMNS[1]
45
+ if "round" in moves:
46
+ step = ROUNDING[_clamp(ROUNDING.index(vec["round"]) + moves["round"], 2)]
47
+ vec["round"] = vec["lab"] = step
@@ -0,0 +1,10 @@
1
+ # START_VERSION_BLOCK
2
+ VERSION_MAJOR = 0
3
+ VERSION_MINOR = 0
4
+ VERSION_BUILD = 0
5
+ VERSION_ALPHA = 2
6
+ # END_VERSION_BLOCK
7
+
8
+ VERSION_STR = f"{VERSION_MAJOR}.{VERSION_MINOR}.{VERSION_BUILD}"
9
+ if VERSION_ALPHA:
10
+ VERSION_STR += f"a{VERSION_ALPHA}"