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
|
|
|
The phonetic._nysiis module implements New York State Identification and |
22
|
|
|
Intelligence System (NYSIIS) phonetic encoding. |
23
|
|
|
""" |
24
|
|
|
|
25
|
1 |
|
from __future__ import unicode_literals |
26
|
|
|
|
27
|
1 |
|
from six.moves import range |
28
|
|
|
|
29
|
1 |
|
from ._phonetic import Phonetic |
30
|
|
|
|
31
|
1 |
|
__all__ = ['NYSIIS', 'nysiis'] |
32
|
|
|
|
33
|
|
|
|
34
|
1 |
|
class NYSIIS(Phonetic): |
|
|
|
|
35
|
|
|
"""NYSIIS Code. |
36
|
|
|
|
37
|
|
|
The New York State Identification and Intelligence System algorithm is |
38
|
|
|
defined in :cite:`Taft:1970`. |
39
|
|
|
|
40
|
|
|
The modified version of this algorithm is described in Appendix B of |
41
|
|
|
:cite:`Lynch:1977`. |
42
|
|
|
""" |
43
|
|
|
|
44
|
1 |
|
def encode(self, word, max_length=6, modified=False): |
|
|
|
|
45
|
|
|
"""Return the NYSIIS code for a word. |
46
|
|
|
|
47
|
|
|
Args: |
48
|
|
|
word (str): The word to transform |
49
|
|
|
max_length (int): The maximum length (default 6) of the code to |
50
|
|
|
return |
51
|
|
|
modified (bool): Indicates whether to use USDA modified NYSIIS |
52
|
|
|
|
53
|
|
|
Returns: |
54
|
|
|
str: The NYSIIS value |
55
|
|
|
|
56
|
|
|
Examples: |
57
|
|
|
>>> pe = NYSIIS() |
58
|
|
|
>>> pe.encode('Christopher') |
59
|
|
|
'CRASTA' |
60
|
|
|
>>> pe.encode('Niall') |
61
|
|
|
'NAL' |
62
|
|
|
>>> pe.encode('Smith') |
63
|
|
|
'SNAT' |
64
|
|
|
>>> pe.encode('Schmidt') |
65
|
|
|
'SNAD' |
66
|
|
|
|
67
|
|
|
>>> pe.encode('Christopher', max_length=-1) |
68
|
|
|
'CRASTAFAR' |
69
|
|
|
|
70
|
|
|
>>> pe.encode('Christopher', max_length=8, modified=True) |
71
|
|
|
'CRASTAFA' |
72
|
|
|
>>> pe.encode('Niall', max_length=8, modified=True) |
73
|
|
|
'NAL' |
74
|
|
|
>>> pe.encode('Smith', max_length=8, modified=True) |
75
|
|
|
'SNAT' |
76
|
|
|
>>> pe.encode('Schmidt', max_length=8, modified=True) |
77
|
|
|
'SNAD' |
78
|
|
|
|
79
|
|
|
""" |
80
|
|
|
# Require a max_length of at least 6 |
81
|
1 |
|
if max_length > -1: |
82
|
1 |
|
max_length = max(6, max_length) |
83
|
|
|
|
84
|
1 |
|
word = ''.join(c for c in word.upper() if c.isalpha()) |
85
|
1 |
|
word = word.replace('ß', 'SS') |
86
|
|
|
|
87
|
|
|
# exit early if there are no alphas |
88
|
1 |
|
if not word: |
89
|
1 |
|
return '' |
90
|
|
|
|
91
|
1 |
|
original_first_char = word[0] |
92
|
|
|
|
93
|
1 |
|
if word[:3] == 'MAC': |
94
|
1 |
|
word = 'MCC' + word[3:] |
95
|
1 |
|
elif word[:2] == 'KN': |
96
|
1 |
|
word = 'NN' + word[2:] |
97
|
1 |
|
elif word[:1] == 'K': |
98
|
1 |
|
word = 'C' + word[1:] |
99
|
1 |
|
elif word[:2] in {'PH', 'PF'}: |
100
|
1 |
|
word = 'FF' + word[2:] |
101
|
1 |
|
elif word[:3] == 'SCH': |
102
|
1 |
|
word = 'SSS' + word[3:] |
103
|
1 |
|
elif modified: |
104
|
1 |
|
if word[:2] == 'WR': |
105
|
1 |
|
word = 'RR' + word[2:] |
106
|
1 |
|
elif word[:2] == 'RH': |
107
|
1 |
|
word = 'RR' + word[2:] |
108
|
1 |
|
elif word[:2] == 'DG': |
109
|
1 |
|
word = 'GG' + word[2:] |
110
|
1 |
|
elif word[:1] in self._uc_v_set: |
111
|
1 |
|
word = 'A' + word[1:] |
112
|
|
|
|
113
|
1 |
|
if modified and word[-1:] in {'S', 'Z'}: |
114
|
1 |
|
word = word[:-1] |
115
|
|
|
|
116
|
1 |
|
if ( |
117
|
|
|
word[-2:] == 'EE' |
|
|
|
|
118
|
|
|
or word[-2:] == 'IE' |
|
|
|
|
119
|
|
|
or (modified and word[-2:] == 'YE') |
|
|
|
|
120
|
|
|
): |
121
|
1 |
|
word = word[:-2] + 'Y' |
122
|
1 |
|
elif word[-2:] in {'DT', 'RT', 'RD'}: |
123
|
1 |
|
word = word[:-2] + 'D' |
124
|
1 |
|
elif word[-2:] in {'NT', 'ND'}: |
125
|
1 |
|
word = word[:-2] + ('N' if modified else 'D') |
126
|
1 |
|
elif modified: |
127
|
1 |
|
if word[-2:] == 'IX': |
128
|
1 |
|
word = word[:-2] + 'ICK' |
129
|
1 |
|
elif word[-2:] == 'EX': |
130
|
1 |
|
word = word[:-2] + 'ECK' |
131
|
1 |
|
elif word[-2:] in {'JR', 'SR'}: |
132
|
1 |
|
return 'ERROR' |
133
|
|
|
|
134
|
1 |
|
key = word[:1] |
135
|
|
|
|
136
|
1 |
|
skip = 0 |
137
|
1 |
|
for i in range(1, len(word)): |
138
|
1 |
|
if i >= len(word): |
139
|
1 |
|
continue |
140
|
1 |
|
elif skip: |
141
|
1 |
|
skip -= 1 |
142
|
1 |
|
continue |
143
|
1 |
|
elif word[i : i + 2] == 'EV': |
144
|
1 |
|
word = word[:i] + 'AF' + word[i + 2 :] |
145
|
1 |
|
skip = 1 |
146
|
1 |
|
elif word[i] in self._uc_v_set: |
147
|
1 |
|
word = word[:i] + 'A' + word[i + 1 :] |
148
|
1 |
|
elif modified and i != len(word) - 1 and word[i] == 'Y': |
149
|
1 |
|
word = word[:i] + 'A' + word[i + 1 :] |
150
|
1 |
|
elif word[i] == 'Q': |
151
|
1 |
|
word = word[:i] + 'G' + word[i + 1 :] |
152
|
1 |
|
elif word[i] == 'Z': |
153
|
1 |
|
word = word[:i] + 'S' + word[i + 1 :] |
154
|
1 |
|
elif word[i] == 'M': |
155
|
1 |
|
word = word[:i] + 'N' + word[i + 1 :] |
156
|
1 |
|
elif word[i : i + 2] == 'KN': |
157
|
1 |
|
word = word[:i] + 'N' + word[i + 2 :] |
158
|
1 |
|
elif word[i] == 'K': |
159
|
1 |
|
word = word[:i] + 'C' + word[i + 1 :] |
160
|
1 |
|
elif modified and i == len(word) - 3 and word[i : i + 3] == 'SCH': |
161
|
1 |
|
word = word[:i] + 'SSA' |
162
|
1 |
|
skip = 2 |
163
|
1 |
|
elif word[i : i + 3] == 'SCH': |
164
|
1 |
|
word = word[:i] + 'SSS' + word[i + 3 :] |
165
|
1 |
|
skip = 2 |
166
|
1 |
|
elif modified and i == len(word) - 2 and word[i : i + 2] == 'SH': |
167
|
1 |
|
word = word[:i] + 'SA' |
168
|
1 |
|
skip = 1 |
169
|
1 |
|
elif word[i : i + 2] == 'SH': |
170
|
1 |
|
word = word[:i] + 'SS' + word[i + 2 :] |
171
|
1 |
|
skip = 1 |
172
|
1 |
|
elif word[i : i + 2] == 'PH': |
173
|
1 |
|
word = word[:i] + 'FF' + word[i + 2 :] |
174
|
1 |
|
skip = 1 |
175
|
1 |
|
elif modified and word[i : i + 3] == 'GHT': |
176
|
1 |
|
word = word[:i] + 'TTT' + word[i + 3 :] |
177
|
1 |
|
skip = 2 |
178
|
1 |
|
elif modified and word[i : i + 2] == 'DG': |
179
|
1 |
|
word = word[:i] + 'GG' + word[i + 2 :] |
180
|
1 |
|
skip = 1 |
181
|
1 |
|
elif modified and word[i : i + 2] == 'WR': |
182
|
1 |
|
word = word[:i] + 'RR' + word[i + 2 :] |
183
|
1 |
|
skip = 1 |
184
|
1 |
|
elif word[i] == 'H' and ( |
185
|
|
|
word[i - 1] not in self._uc_v_set |
|
|
|
|
186
|
|
|
or word[i + 1 : i + 2] not in self._uc_v_set |
|
|
|
|
187
|
|
|
): |
188
|
1 |
|
word = word[:i] + word[i - 1] + word[i + 1 :] |
189
|
1 |
|
elif word[i] == 'W' and word[i - 1] in self._uc_v_set: |
190
|
1 |
|
word = word[:i] + word[i - 1] + word[i + 1 :] |
191
|
|
|
|
192
|
1 |
|
if word[i : i + skip + 1] != key[-1:]: |
193
|
1 |
|
key += word[i : i + skip + 1] |
194
|
|
|
|
195
|
1 |
|
key = self._delete_consecutive_repeats(key) |
196
|
|
|
|
197
|
1 |
|
if key[-1:] == 'S': |
198
|
1 |
|
key = key[:-1] |
199
|
1 |
|
if key[-2:] == 'AY': |
200
|
1 |
|
key = key[:-2] + 'Y' |
201
|
1 |
|
if key[-1:] == 'A': |
202
|
1 |
|
key = key[:-1] |
203
|
1 |
|
if modified and key[:1] == 'A': |
204
|
1 |
|
key = original_first_char + key[1:] |
205
|
|
|
|
206
|
1 |
|
if max_length > 0: |
207
|
1 |
|
key = key[:max_length] |
208
|
|
|
|
209
|
1 |
|
return key |
210
|
|
|
|
211
|
|
|
|
212
|
1 |
|
def nysiis(word, max_length=6, modified=False): |
213
|
|
|
"""Return the NYSIIS code for a word. |
214
|
|
|
|
215
|
|
|
This is a wrapper for :py:meth:`Metaphone.encode`. |
216
|
|
|
|
217
|
|
|
Args: |
218
|
|
|
word (str): The word to transform |
219
|
|
|
max_length (int): The maximum length (default 6) of the code to return |
220
|
|
|
modified (bool): Indicates whether to use USDA modified NYSIIS |
221
|
|
|
|
222
|
|
|
Returns: |
223
|
|
|
str: The NYSIIS value |
224
|
|
|
|
225
|
|
|
Examples: |
226
|
|
|
>>> nysiis('Christopher') |
227
|
|
|
'CRASTA' |
228
|
|
|
>>> nysiis('Niall') |
229
|
|
|
'NAL' |
230
|
|
|
>>> nysiis('Smith') |
231
|
|
|
'SNAT' |
232
|
|
|
>>> nysiis('Schmidt') |
233
|
|
|
'SNAD' |
234
|
|
|
|
235
|
|
|
>>> nysiis('Christopher', max_length=-1) |
236
|
|
|
'CRASTAFAR' |
237
|
|
|
|
238
|
|
|
>>> nysiis('Christopher', max_length=8, modified=True) |
239
|
|
|
'CRASTAFA' |
240
|
|
|
>>> nysiis('Niall', max_length=8, modified=True) |
241
|
|
|
'NAL' |
242
|
|
|
>>> nysiis('Smith', max_length=8, modified=True) |
243
|
|
|
'SNAT' |
244
|
|
|
>>> nysiis('Schmidt', max_length=8, modified=True) |
245
|
|
|
'SNAD' |
246
|
|
|
|
247
|
|
|
""" |
248
|
1 |
|
return NYSIIS().encode(word, max_length, modified) |
249
|
|
|
|
250
|
|
|
|
251
|
|
|
if __name__ == '__main__': |
252
|
|
|
import doctest |
253
|
|
|
|
254
|
|
|
doctest.testmod() |
255
|
|
|
|