1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
# Copyright 2014-2018 by Christopher C. Little. |
4
|
|
|
# This file is part of Abydos. |
5
|
|
|
# |
6
|
|
|
# Abydos is free software: you can redistribute it and/or modify |
7
|
|
|
# it under the terms of the GNU General Public License as published by |
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
# (at your option) any later version. |
10
|
|
|
# |
11
|
|
|
# Abydos is distributed in the hope that it will be useful, |
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
# GNU General Public License for more details. |
15
|
|
|
# |
16
|
|
|
# You should have received a copy of the GNU General Public License |
17
|
|
|
# along with Abydos. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
|
19
|
1 |
|
"""abydos.phonetic._nysiis. |
20
|
|
|
|
21
|
|
|
New York State Identification and Intelligence System (NYSIIS) phonetic |
22
|
|
|
encoding |
23
|
|
|
""" |
24
|
|
|
|
25
|
1 |
|
from __future__ import ( |
26
|
|
|
absolute_import, |
27
|
|
|
division, |
28
|
|
|
print_function, |
29
|
|
|
unicode_literals, |
30
|
|
|
) |
31
|
|
|
|
32
|
1 |
|
from six.moves import range |
33
|
|
|
|
34
|
1 |
|
from ._phonetic import _Phonetic |
35
|
|
|
|
36
|
1 |
|
__all__ = ['NYSIIS', 'nysiis'] |
37
|
|
|
|
38
|
|
|
|
39
|
1 |
|
class NYSIIS(_Phonetic): |
|
|
|
|
40
|
|
|
"""NYSIIS Code. |
41
|
|
|
|
42
|
|
|
The New York State Identification and Intelligence System algorithm is |
43
|
|
|
defined in :cite:`Taft:1970`. |
44
|
|
|
|
45
|
|
|
The modified version of this algorithm is described in Appendix B of |
46
|
|
|
:cite:`Lynch:1977`. |
47
|
|
|
""" |
48
|
|
|
|
49
|
1 |
|
def encode(self, word, max_length=6, modified=False): |
|
|
|
|
50
|
|
|
"""Return the NYSIIS code for a word. |
51
|
|
|
|
52
|
|
|
Parameters |
53
|
|
|
---------- |
54
|
|
|
word : str |
55
|
|
|
The word to transform |
56
|
|
|
max_length : int |
57
|
|
|
The maximum length (default 6) of the code to return |
58
|
|
|
modified : bool |
59
|
|
|
Indicates whether to use USDA modified NYSIIS |
60
|
|
|
|
61
|
|
|
Returns |
62
|
|
|
------- |
63
|
|
|
str |
64
|
|
|
The NYSIIS value |
65
|
|
|
|
66
|
|
|
Examples |
67
|
|
|
-------- |
68
|
|
|
>>> pe = NYSIIS() |
69
|
|
|
>>> pe.encode('Christopher') |
70
|
|
|
'CRASTA' |
71
|
|
|
>>> pe.encode('Niall') |
72
|
|
|
'NAL' |
73
|
|
|
>>> pe.encode('Smith') |
74
|
|
|
'SNAT' |
75
|
|
|
>>> pe.encode('Schmidt') |
76
|
|
|
'SNAD' |
77
|
|
|
|
78
|
|
|
>>> pe.encode('Christopher', max_length=-1) |
79
|
|
|
'CRASTAFAR' |
80
|
|
|
|
81
|
|
|
>>> pe.encode('Christopher', max_length=8, modified=True) |
82
|
|
|
'CRASTAFA' |
83
|
|
|
>>> pe.encode('Niall', max_length=8, modified=True) |
84
|
|
|
'NAL' |
85
|
|
|
>>> pe.encode('Smith', max_length=8, modified=True) |
86
|
|
|
'SNAT' |
87
|
|
|
>>> pe.encode('Schmidt', max_length=8, modified=True) |
88
|
|
|
'SNAD' |
89
|
|
|
|
90
|
|
|
""" |
91
|
|
|
# Require a max_length of at least 6 |
92
|
1 |
|
if max_length > -1: |
93
|
1 |
|
max_length = max(6, max_length) |
94
|
|
|
|
95
|
1 |
|
word = ''.join(c for c in word.upper() if c.isalpha()) |
96
|
1 |
|
word = word.replace('ß', 'SS') |
97
|
|
|
|
98
|
|
|
# exit early if there are no alphas |
99
|
1 |
|
if not word: |
100
|
1 |
|
return '' |
101
|
|
|
|
102
|
1 |
|
original_first_char = word[0] |
103
|
|
|
|
104
|
1 |
|
if word[:3] == 'MAC': |
105
|
1 |
|
word = 'MCC' + word[3:] |
106
|
1 |
|
elif word[:2] == 'KN': |
107
|
1 |
|
word = 'NN' + word[2:] |
108
|
1 |
|
elif word[:1] == 'K': |
109
|
1 |
|
word = 'C' + word[1:] |
110
|
1 |
|
elif word[:2] in {'PH', 'PF'}: |
111
|
1 |
|
word = 'FF' + word[2:] |
112
|
1 |
|
elif word[:3] == 'SCH': |
113
|
1 |
|
word = 'SSS' + word[3:] |
114
|
1 |
|
elif modified: |
115
|
1 |
|
if word[:2] == 'WR': |
116
|
1 |
|
word = 'RR' + word[2:] |
117
|
1 |
|
elif word[:2] == 'RH': |
118
|
1 |
|
word = 'RR' + word[2:] |
119
|
1 |
|
elif word[:2] == 'DG': |
120
|
1 |
|
word = 'GG' + word[2:] |
121
|
1 |
|
elif word[:1] in self._uc_v_set: |
122
|
1 |
|
word = 'A' + word[1:] |
123
|
|
|
|
124
|
1 |
|
if modified and word[-1:] in {'S', 'Z'}: |
125
|
1 |
|
word = word[:-1] |
126
|
|
|
|
127
|
1 |
|
if ( |
128
|
|
|
word[-2:] == 'EE' |
|
|
|
|
129
|
|
|
or word[-2:] == 'IE' |
|
|
|
|
130
|
|
|
or (modified and word[-2:] == 'YE') |
|
|
|
|
131
|
|
|
): |
132
|
1 |
|
word = word[:-2] + 'Y' |
133
|
1 |
|
elif word[-2:] in {'DT', 'RT', 'RD'}: |
134
|
1 |
|
word = word[:-2] + 'D' |
135
|
1 |
|
elif word[-2:] in {'NT', 'ND'}: |
136
|
1 |
|
word = word[:-2] + ('N' if modified else 'D') |
137
|
1 |
|
elif modified: |
138
|
1 |
|
if word[-2:] == 'IX': |
139
|
1 |
|
word = word[:-2] + 'ICK' |
140
|
1 |
|
elif word[-2:] == 'EX': |
141
|
1 |
|
word = word[:-2] + 'ECK' |
142
|
1 |
|
elif word[-2:] in {'JR', 'SR'}: |
143
|
1 |
|
return 'ERROR' |
144
|
|
|
|
145
|
1 |
|
key = word[:1] |
146
|
|
|
|
147
|
1 |
|
skip = 0 |
148
|
1 |
|
for i in range(1, len(word)): |
149
|
1 |
|
if i >= len(word): |
150
|
1 |
|
continue |
151
|
1 |
|
elif skip: |
152
|
1 |
|
skip -= 1 |
153
|
1 |
|
continue |
154
|
1 |
|
elif word[i : i + 2] == 'EV': |
155
|
1 |
|
word = word[:i] + 'AF' + word[i + 2 :] |
156
|
1 |
|
skip = 1 |
157
|
1 |
|
elif word[i] in self._uc_v_set: |
158
|
1 |
|
word = word[:i] + 'A' + word[i + 1 :] |
159
|
1 |
|
elif modified and i != len(word) - 1 and word[i] == 'Y': |
160
|
1 |
|
word = word[:i] + 'A' + word[i + 1 :] |
161
|
1 |
|
elif word[i] == 'Q': |
162
|
1 |
|
word = word[:i] + 'G' + word[i + 1 :] |
163
|
1 |
|
elif word[i] == 'Z': |
164
|
1 |
|
word = word[:i] + 'S' + word[i + 1 :] |
165
|
1 |
|
elif word[i] == 'M': |
166
|
1 |
|
word = word[:i] + 'N' + word[i + 1 :] |
167
|
1 |
|
elif word[i : i + 2] == 'KN': |
168
|
1 |
|
word = word[:i] + 'N' + word[i + 2 :] |
169
|
1 |
|
elif word[i] == 'K': |
170
|
1 |
|
word = word[:i] + 'C' + word[i + 1 :] |
171
|
1 |
|
elif modified and i == len(word) - 3 and word[i : i + 3] == 'SCH': |
172
|
1 |
|
word = word[:i] + 'SSA' |
173
|
1 |
|
skip = 2 |
174
|
1 |
|
elif word[i : i + 3] == 'SCH': |
175
|
1 |
|
word = word[:i] + 'SSS' + word[i + 3 :] |
176
|
1 |
|
skip = 2 |
177
|
1 |
|
elif modified and i == len(word) - 2 and word[i : i + 2] == 'SH': |
178
|
1 |
|
word = word[:i] + 'SA' |
179
|
1 |
|
skip = 1 |
180
|
1 |
|
elif word[i : i + 2] == 'SH': |
181
|
1 |
|
word = word[:i] + 'SS' + word[i + 2 :] |
182
|
1 |
|
skip = 1 |
183
|
1 |
|
elif word[i : i + 2] == 'PH': |
184
|
1 |
|
word = word[:i] + 'FF' + word[i + 2 :] |
185
|
1 |
|
skip = 1 |
186
|
1 |
|
elif modified and word[i : i + 3] == 'GHT': |
187
|
1 |
|
word = word[:i] + 'TTT' + word[i + 3 :] |
188
|
1 |
|
skip = 2 |
189
|
1 |
|
elif modified and word[i : i + 2] == 'DG': |
190
|
1 |
|
word = word[:i] + 'GG' + word[i + 2 :] |
191
|
1 |
|
skip = 1 |
192
|
1 |
|
elif modified and word[i : i + 2] == 'WR': |
193
|
1 |
|
word = word[:i] + 'RR' + word[i + 2 :] |
194
|
1 |
|
skip = 1 |
195
|
1 |
|
elif word[i] == 'H' and ( |
196
|
|
|
word[i - 1] not in self._uc_v_set |
|
|
|
|
197
|
|
|
or word[i + 1 : i + 2] not in self._uc_v_set |
|
|
|
|
198
|
|
|
): |
199
|
1 |
|
word = word[:i] + word[i - 1] + word[i + 1 :] |
200
|
1 |
|
elif word[i] == 'W' and word[i - 1] in self._uc_v_set: |
201
|
1 |
|
word = word[:i] + word[i - 1] + word[i + 1 :] |
202
|
|
|
|
203
|
1 |
|
if word[i : i + skip + 1] != key[-1:]: |
204
|
1 |
|
key += word[i : i + skip + 1] |
205
|
|
|
|
206
|
1 |
|
key = self._delete_consecutive_repeats(key) |
207
|
|
|
|
208
|
1 |
|
if key[-1:] == 'S': |
209
|
1 |
|
key = key[:-1] |
210
|
1 |
|
if key[-2:] == 'AY': |
211
|
1 |
|
key = key[:-2] + 'Y' |
212
|
1 |
|
if key[-1:] == 'A': |
213
|
1 |
|
key = key[:-1] |
214
|
1 |
|
if modified and key[:1] == 'A': |
215
|
1 |
|
key = original_first_char + key[1:] |
216
|
|
|
|
217
|
1 |
|
if max_length > 0: |
218
|
1 |
|
key = key[:max_length] |
219
|
|
|
|
220
|
1 |
|
return key |
221
|
|
|
|
222
|
|
|
|
223
|
1 |
|
def nysiis(word, max_length=6, modified=False): |
224
|
|
|
"""Return the NYSIIS code for a word. |
225
|
|
|
|
226
|
|
|
This is a wrapper for :py:meth:`NYSIIS.encode`. |
227
|
|
|
|
228
|
|
|
Parameters |
229
|
|
|
---------- |
230
|
|
|
word : str |
231
|
|
|
The word to transform |
232
|
|
|
max_length : int |
233
|
|
|
The maximum length (default 6) of the code to return |
234
|
|
|
modified : bool |
235
|
|
|
Indicates whether to use USDA modified NYSIIS |
236
|
|
|
|
237
|
|
|
Returns |
238
|
|
|
------- |
239
|
|
|
str |
240
|
|
|
The NYSIIS value |
241
|
|
|
|
242
|
|
|
Examples |
243
|
|
|
-------- |
244
|
|
|
>>> nysiis('Christopher') |
245
|
|
|
'CRASTA' |
246
|
|
|
>>> nysiis('Niall') |
247
|
|
|
'NAL' |
248
|
|
|
>>> nysiis('Smith') |
249
|
|
|
'SNAT' |
250
|
|
|
>>> nysiis('Schmidt') |
251
|
|
|
'SNAD' |
252
|
|
|
|
253
|
|
|
>>> nysiis('Christopher', max_length=-1) |
254
|
|
|
'CRASTAFAR' |
255
|
|
|
|
256
|
|
|
>>> nysiis('Christopher', max_length=8, modified=True) |
257
|
|
|
'CRASTAFA' |
258
|
|
|
>>> nysiis('Niall', max_length=8, modified=True) |
259
|
|
|
'NAL' |
260
|
|
|
>>> nysiis('Smith', max_length=8, modified=True) |
261
|
|
|
'SNAT' |
262
|
|
|
>>> nysiis('Schmidt', max_length=8, modified=True) |
263
|
|
|
'SNAD' |
264
|
|
|
|
265
|
|
|
""" |
266
|
1 |
|
return NYSIIS().encode(word, max_length, modified) |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
if __name__ == '__main__': |
270
|
|
|
import doctest |
271
|
|
|
|
272
|
|
|
doctest.testmod() |
273
|
|
|
|