Completed
Pull Request — master (#138)
by Chris
14:20
created

abydos.phonetic._nysiis.NYSIIS.encode()   F

Complexity

Conditions 66

Size

Total Lines 162
Code Lines 114

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 109
CRAP Score 66

Importance

Changes 0
Metric Value
eloc 114
dl 0
loc 162
ccs 109
cts 109
cp 1
rs 0
c 0
b 0
f 0
cc 66
nop 4
crap 66

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like abydos.phonetic._nysiis.NYSIIS.encode() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
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):
0 ignored issues
show
Bug introduced by
Parameters differ from overridden 'encode' method
Loading history...
45
        """Return the NYSIIS code for a word.
46
47
        :param str word: the word to transform
48
        :param int max_length: the maximum length (default 6) of the code to
49
            return
50
        :param bool modified: indicates whether to use USDA modified NYSIIS
51
        :returns: the NYSIIS value
52
        :rtype: str
53
54
        >>> pe = NYSIIS()
55
        >>> pe.encode('Christopher')
56
        'CRASTA'
57
        >>> pe.encode('Niall')
58
        'NAL'
59
        >>> pe.encode('Smith')
60
        'SNAT'
61
        >>> pe.encode('Schmidt')
62
        'SNAD'
63
64
        >>> pe.encode('Christopher', max_length=-1)
65
        'CRASTAFAR'
66
67
        >>> pe.encode('Christopher', max_length=8, modified=True)
68
        'CRASTAFA'
69
        >>> pe.encode('Niall', max_length=8, modified=True)
70
        'NAL'
71
        >>> pe.encode('Smith', max_length=8, modified=True)
72
        'SNAT'
73
        >>> pe.encode('Schmidt', max_length=8, modified=True)
74
        'SNAD'
75
        """
76
        # Require a max_length of at least 6
77 1
        if max_length > -1:
78 1
            max_length = max(6, max_length)
79
80 1
        word = ''.join(c for c in word.upper() if c.isalpha())
81 1
        word = word.replace('ß', 'SS')
82
83
        # exit early if there are no alphas
84 1
        if not word:
85 1
            return ''
86
87 1
        original_first_char = word[0]
88
89 1
        if word[:3] == 'MAC':
90 1
            word = 'MCC' + word[3:]
91 1
        elif word[:2] == 'KN':
92 1
            word = 'NN' + word[2:]
93 1
        elif word[:1] == 'K':
94 1
            word = 'C' + word[1:]
95 1
        elif word[:2] in {'PH', 'PF'}:
96 1
            word = 'FF' + word[2:]
97 1
        elif word[:3] == 'SCH':
98 1
            word = 'SSS' + word[3:]
99 1
        elif modified:
100 1
            if word[:2] == 'WR':
101 1
                word = 'RR' + word[2:]
102 1
            elif word[:2] == 'RH':
103 1
                word = 'RR' + word[2:]
104 1
            elif word[:2] == 'DG':
105 1
                word = 'GG' + word[2:]
106 1
            elif word[:1] in self._uc_v_set:
107 1
                word = 'A' + word[1:]
108
109 1
        if modified and word[-1:] in {'S', 'Z'}:
110 1
            word = word[:-1]
111
112 1
        if (
113
            word[-2:] == 'EE'
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
114
            or word[-2:] == 'IE'
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
115
            or (modified and word[-2:] == 'YE')
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
116
        ):
117 1
            word = word[:-2] + 'Y'
118 1
        elif word[-2:] in {'DT', 'RT', 'RD'}:
119 1
            word = word[:-2] + 'D'
120 1
        elif word[-2:] in {'NT', 'ND'}:
121 1
            word = word[:-2] + ('N' if modified else 'D')
122 1
        elif modified:
123 1
            if word[-2:] == 'IX':
124 1
                word = word[:-2] + 'ICK'
125 1
            elif word[-2:] == 'EX':
126 1
                word = word[:-2] + 'ECK'
127 1
            elif word[-2:] in {'JR', 'SR'}:
128 1
                return 'ERROR'
129
130 1
        key = word[:1]
131
132 1
        skip = 0
133 1
        for i in range(1, len(word)):
134 1
            if i >= len(word):
135 1
                continue
136 1
            elif skip:
137 1
                skip -= 1
138 1
                continue
139 1
            elif word[i : i + 2] == 'EV':
140 1
                word = word[:i] + 'AF' + word[i + 2 :]
141 1
                skip = 1
142 1
            elif word[i] in self._uc_v_set:
143 1
                word = word[:i] + 'A' + word[i + 1 :]
144 1
            elif modified and i != len(word) - 1 and word[i] == 'Y':
145 1
                word = word[:i] + 'A' + word[i + 1 :]
146 1
            elif word[i] == 'Q':
147 1
                word = word[:i] + 'G' + word[i + 1 :]
148 1
            elif word[i] == 'Z':
149 1
                word = word[:i] + 'S' + word[i + 1 :]
150 1
            elif word[i] == 'M':
151 1
                word = word[:i] + 'N' + word[i + 1 :]
152 1
            elif word[i : i + 2] == 'KN':
153 1
                word = word[:i] + 'N' + word[i + 2 :]
154 1
            elif word[i] == 'K':
155 1
                word = word[:i] + 'C' + word[i + 1 :]
156 1
            elif modified and i == len(word) - 3 and word[i : i + 3] == 'SCH':
157 1
                word = word[:i] + 'SSA'
158 1
                skip = 2
159 1
            elif word[i : i + 3] == 'SCH':
160 1
                word = word[:i] + 'SSS' + word[i + 3 :]
161 1
                skip = 2
162 1
            elif modified and i == len(word) - 2 and word[i : i + 2] == 'SH':
163 1
                word = word[:i] + 'SA'
164 1
                skip = 1
165 1
            elif word[i : i + 2] == 'SH':
166 1
                word = word[:i] + 'SS' + word[i + 2 :]
167 1
                skip = 1
168 1
            elif word[i : i + 2] == 'PH':
169 1
                word = word[:i] + 'FF' + word[i + 2 :]
170 1
                skip = 1
171 1
            elif modified and word[i : i + 3] == 'GHT':
172 1
                word = word[:i] + 'TTT' + word[i + 3 :]
173 1
                skip = 2
174 1
            elif modified and word[i : i + 2] == 'DG':
175 1
                word = word[:i] + 'GG' + word[i + 2 :]
176 1
                skip = 1
177 1
            elif modified and word[i : i + 2] == 'WR':
178 1
                word = word[:i] + 'RR' + word[i + 2 :]
179 1
                skip = 1
180 1
            elif word[i] == 'H' and (
181
                word[i - 1] not in self._uc_v_set
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
182
                or word[i + 1 : i + 2] not in self._uc_v_set
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
183
            ):
184 1
                word = word[:i] + word[i - 1] + word[i + 1 :]
185 1
            elif word[i] == 'W' and word[i - 1] in self._uc_v_set:
186 1
                word = word[:i] + word[i - 1] + word[i + 1 :]
187
188 1
            if word[i : i + skip + 1] != key[-1:]:
189 1
                key += word[i : i + skip + 1]
190
191 1
        key = self._delete_consecutive_repeats(key)
192
193 1
        if key[-1:] == 'S':
194 1
            key = key[:-1]
195 1
        if key[-2:] == 'AY':
196 1
            key = key[:-2] + 'Y'
197 1
        if key[-1:] == 'A':
198 1
            key = key[:-1]
199 1
        if modified and key[:1] == 'A':
200 1
            key = original_first_char + key[1:]
201
202 1
        if max_length > 0:
203 1
            key = key[:max_length]
204
205 1
        return key
206
207
208 1
def nysiis(word, max_length=6, modified=False):
209
    """Return the NYSIIS code for a word.
210
211
    This is a wrapper for :py:meth:`Metaphone.encode`.
212
213
    :param str word: the word to transform
214
    :param int max_length: the maximum length (default 6) of the code to
215
        return
216
    :param bool modified: indicates whether to use USDA modified NYSIIS
217
    :returns: the NYSIIS value
218
    :rtype: str
219
220
    >>> nysiis('Christopher')
221
    'CRASTA'
222
    >>> nysiis('Niall')
223
    'NAL'
224
    >>> nysiis('Smith')
225
    'SNAT'
226
    >>> nysiis('Schmidt')
227
    'SNAD'
228
229
    >>> nysiis('Christopher', max_length=-1)
230
    'CRASTAFAR'
231
232
    >>> nysiis('Christopher', max_length=8, modified=True)
233
    'CRASTAFA'
234
    >>> nysiis('Niall', max_length=8, modified=True)
235
    'NAL'
236
    >>> nysiis('Smith', max_length=8, modified=True)
237
    'SNAT'
238
    >>> nysiis('Schmidt', max_length=8, modified=True)
239
    'SNAD'
240
    """
241 1
    return NYSIIS().encode(word, max_length, modified)
242
243
244
if __name__ == '__main__':
245
    import doctest
246
247
    doctest.testmod()
248