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._pshp_soundex_first. |
20
|
|
|
|
21
|
|
|
PSHP Soundex/Viewex Coding for first names |
22
|
|
|
""" |
23
|
|
|
|
24
|
1 |
|
from __future__ import ( |
25
|
|
|
absolute_import, |
26
|
|
|
division, |
27
|
|
|
print_function, |
28
|
|
|
unicode_literals, |
29
|
|
|
) |
30
|
|
|
|
31
|
1 |
|
from unicodedata import normalize as unicode_normalize |
32
|
|
|
|
33
|
1 |
|
from six import text_type |
34
|
|
|
|
35
|
1 |
|
from ._phonetic import _Phonetic |
36
|
|
|
|
37
|
1 |
|
__all__ = ['PSHPSoundexFirst', 'pshp_soundex_first'] |
38
|
|
|
|
39
|
|
|
|
40
|
1 |
|
class PSHPSoundexFirst(_Phonetic): |
|
|
|
|
41
|
|
|
"""PSHP Soundex/Viewex Coding of a first name. |
42
|
|
|
|
43
|
|
|
This coding is based on :cite:`Hershberg:1976`. |
44
|
|
|
|
45
|
|
|
Reference was also made to the German version of the same: |
46
|
|
|
:cite:`Hershberg:1979`. |
47
|
|
|
|
48
|
|
|
A separate class, :py:class:`PSHPSoundexLast` is used for last names. |
49
|
|
|
""" |
50
|
|
|
|
51
|
1 |
|
_trans = dict( |
52
|
|
|
zip( |
53
|
|
|
(ord(_) for _ in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), |
|
|
|
|
54
|
|
|
'01230120022455012523010202', |
55
|
|
|
) |
56
|
|
|
) |
57
|
|
|
|
58
|
1 |
|
def encode(self, fname, max_length=4, german=False): |
|
|
|
|
59
|
|
|
"""Calculate the PSHP Soundex/Viewex Coding of a first name. |
60
|
|
|
|
61
|
|
|
Parameters |
62
|
|
|
---------- |
63
|
|
|
fname : str |
64
|
|
|
The first name to encode |
65
|
|
|
max_length : int |
66
|
|
|
The length of the code returned (defaults to 4) |
67
|
|
|
german : bool |
68
|
|
|
Set to True if the name is German (different rules apply) |
69
|
|
|
|
70
|
|
|
Returns |
71
|
|
|
------- |
72
|
|
|
str |
73
|
|
|
The PSHP Soundex/Viewex Coding |
74
|
|
|
|
75
|
|
|
Examples |
76
|
|
|
-------- |
77
|
|
|
>>> pe = PSHPSoundexFirst() |
78
|
|
|
>>> pe.encode('Smith') |
79
|
|
|
'S530' |
80
|
|
|
>>> pe.encode('Waters') |
81
|
|
|
'W352' |
82
|
|
|
>>> pe.encode('James') |
83
|
|
|
'J700' |
84
|
|
|
>>> pe.encode('Schmidt') |
85
|
|
|
'S500' |
86
|
|
|
>>> pe.encode('Ashcroft') |
87
|
|
|
'A220' |
88
|
|
|
>>> pe.encode('John') |
89
|
|
|
'J500' |
90
|
|
|
>>> pe.encode('Colin') |
91
|
|
|
'K400' |
92
|
|
|
>>> pe.encode('Niall') |
93
|
|
|
'N400' |
94
|
|
|
>>> pe.encode('Sally') |
95
|
|
|
'S400' |
96
|
|
|
>>> pe.encode('Jane') |
97
|
|
|
'J500' |
98
|
|
|
|
99
|
|
|
""" |
100
|
1 |
|
fname = unicode_normalize('NFKD', text_type(fname.upper())) |
101
|
1 |
|
fname = fname.replace('ß', 'SS') |
102
|
1 |
|
fname = ''.join(c for c in fname if c in self._uc_set) |
103
|
|
|
|
104
|
|
|
# special rules |
105
|
1 |
|
if fname == 'JAMES': |
106
|
1 |
|
code = 'J7' |
107
|
1 |
|
elif fname == 'PAT': |
108
|
1 |
|
code = 'P7' |
109
|
|
|
|
110
|
|
|
else: |
111
|
|
|
# A. Prefix treatment |
112
|
1 |
|
if fname[:2] in {'GE', 'GI', 'GY'}: |
113
|
1 |
|
fname = 'J' + fname[1:] |
114
|
1 |
|
elif fname[:2] in {'CE', 'CI', 'CY'}: |
115
|
1 |
|
fname = 'S' + fname[1:] |
116
|
1 |
|
elif fname[:3] == 'CHR': |
117
|
1 |
|
fname = 'K' + fname[1:] |
118
|
1 |
|
elif fname[:1] == 'C' and fname[:2] != 'CH': |
119
|
1 |
|
fname = 'K' + fname[1:] |
120
|
|
|
|
121
|
1 |
|
if fname[:2] == 'KN': |
122
|
1 |
|
fname = 'N' + fname[1:] |
123
|
1 |
|
elif fname[:2] == 'PH': |
124
|
1 |
|
fname = 'F' + fname[1:] |
125
|
1 |
|
elif fname[:3] in {'WIE', 'WEI'}: |
126
|
1 |
|
fname = 'V' + fname[1:] |
127
|
|
|
|
128
|
1 |
|
if german and fname[:1] in {'W', 'M', 'Y', 'Z'}: |
129
|
1 |
|
fname = {'W': 'V', 'M': 'N', 'Y': 'J', 'Z': 'S'}[ |
130
|
|
|
fname[0] |
131
|
|
|
] + fname[1:] |
132
|
|
|
|
133
|
1 |
|
code = fname[:1] |
134
|
|
|
|
135
|
|
|
# B. Soundex coding |
136
|
|
|
# code for Y unspecified, but presumably is 0 |
137
|
1 |
|
fname = fname.translate(self._trans) |
138
|
1 |
|
fname = self._delete_consecutive_repeats(fname) |
139
|
|
|
|
140
|
1 |
|
code += fname[1:] |
141
|
1 |
|
syl_ptr = code.find('0') |
142
|
1 |
|
syl2_ptr = code[syl_ptr + 1 :].find('0') |
143
|
1 |
|
if syl_ptr != -1 and syl2_ptr != -1 and syl2_ptr - syl_ptr > -1: |
144
|
1 |
|
code = code[: syl_ptr + 2] |
145
|
|
|
|
146
|
1 |
|
code = code.replace('0', '') # rule 1 |
147
|
|
|
|
148
|
1 |
|
if max_length != -1: |
149
|
1 |
|
if len(code) < max_length: |
150
|
1 |
|
code += '0' * (max_length - len(code)) |
151
|
|
|
else: |
152
|
1 |
|
code = code[:max_length] |
153
|
|
|
|
154
|
1 |
|
return code |
155
|
|
|
|
156
|
|
|
|
157
|
1 |
|
def pshp_soundex_first(fname, max_length=4, german=False): |
158
|
|
|
"""Calculate the PSHP Soundex/Viewex Coding of a first name. |
159
|
|
|
|
160
|
|
|
This is a wrapper for :py:meth:`PSHPSoundexFirst.encode`. |
161
|
|
|
|
162
|
|
|
Parameters |
163
|
|
|
---------- |
164
|
|
|
fname : str |
165
|
|
|
The first name to encode |
166
|
|
|
max_length : int |
167
|
|
|
The length of the code returned (defaults to 4) |
168
|
|
|
german : bool |
169
|
|
|
Set to True if the name is German (different rules apply) |
170
|
|
|
|
171
|
|
|
Returns |
172
|
|
|
------- |
173
|
|
|
str |
174
|
|
|
The PSHP Soundex/Viewex Coding |
175
|
|
|
|
176
|
|
|
Examples |
177
|
|
|
-------- |
178
|
|
|
>>> pshp_soundex_first('Smith') |
179
|
|
|
'S530' |
180
|
|
|
>>> pshp_soundex_first('Waters') |
181
|
|
|
'W352' |
182
|
|
|
>>> pshp_soundex_first('James') |
183
|
|
|
'J700' |
184
|
|
|
>>> pshp_soundex_first('Schmidt') |
185
|
|
|
'S500' |
186
|
|
|
>>> pshp_soundex_first('Ashcroft') |
187
|
|
|
'A220' |
188
|
|
|
>>> pshp_soundex_first('John') |
189
|
|
|
'J500' |
190
|
|
|
>>> pshp_soundex_first('Colin') |
191
|
|
|
'K400' |
192
|
|
|
>>> pshp_soundex_first('Niall') |
193
|
|
|
'N400' |
194
|
|
|
>>> pshp_soundex_first('Sally') |
195
|
|
|
'S400' |
196
|
|
|
>>> pshp_soundex_first('Jane') |
197
|
|
|
'J500' |
198
|
|
|
|
199
|
|
|
""" |
200
|
1 |
|
return PSHPSoundexFirst().encode(fname, max_length, german) |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
if __name__ == '__main__': |
204
|
|
|
import doctest |
205
|
|
|
|
206
|
|
|
doctest.testmod() |
207
|
|
|
|