Completed
Push — master ( f43547...71985b )
by Chris
12:00 queued 10s
created

SpanishMetaphone.encode()   F

Complexity

Conditions 29

Size

Total Lines 183
Code Lines 101

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 87
CRAP Score 29

Importance

Changes 0
Metric Value
cc 29
eloc 101
nop 4
dl 0
loc 183
ccs 87
cts 87
cp 1
crap 29
rs 0
c 0
b 0
f 0

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._spanish_metaphone.SpanishMetaphone.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 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._spanish_metaphone.
20
21
Spanish Metaphone
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__ = ['SpanishMetaphone', 'spanish_metaphone']
38
39
40 1
class SpanishMetaphone(_Phonetic):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
41
    """Spanish Metaphone.
42
43
    This is a quick rewrite of the Spanish Metaphone Algorithm, as presented at
44
    https://github.com/amsqr/Spanish-Metaphone and discussed in
45
    :cite:`Mosquera:2012`.
46
47
    Modified version based on :cite:`delPilarAngeles:2016`.
48
    """
49
50 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...
51
        """Return the Spanish Metaphone of a word.
52
53
        Parameters
54
        ----------
55
        word : str
56
            The word to transform
57
        max_length : int
58
            The length of the code returned (defaults to 6)
59
        modified : bool
60
            Set to True to use del Pilar Angeles & Bailón-Miguel's modified
61
            version of the algorithm
62
63
        Returns
64
        -------
65
        str
66
            The Spanish Metaphone code
67
68
        Examples
69
        --------
70
        >>> pe = SpanishMetaphone()
71
        >>> pe.encode('Perez')
72
        'PRZ'
73
        >>> pe.encode('Martinez')
74
        'MRTNZ'
75
        >>> pe.encode('Gutierrez')
76
        'GTRRZ'
77
        >>> pe.encode('Santiago')
78
        'SNTG'
79
        >>> pe.encode('Nicolás')
80
        'NKLS'
81
82
        """
83
84 1
        def _is_vowel(pos):
85
            """Return True if the character at word[pos] is a vowel.
86
87
            Parameters
88
            ----------
89
            pos : int
90
                Position to check for a vowel
91
92
            Returns
93
            -------
94
            bool
95
                True if word[pos] is a vowel
96
97
            """
98 1
            return pos < len(word) and word[pos] in {'A', 'E', 'I', 'O', 'U'}
99
100 1
        word = unicode_normalize('NFC', text_type(word.upper()))
101
102 1
        meta_key = ''
103 1
        pos = 0
104
105
        # do some replacements for the modified version
106 1
        if modified:
107 1
            word = word.replace('MB', 'NB')
108 1
            word = word.replace('MP', 'NP')
109 1
            word = word.replace('BS', 'S')
110 1
            if word[:2] == 'PS':
111 1
                word = word[1:]
112
113
        # simple replacements
114 1
        word = word.replace('Á', 'A')
115 1
        word = word.replace('CH', 'X')
116 1
        word = word.replace('Ç', 'S')
117 1
        word = word.replace('É', 'E')
118 1
        word = word.replace('Í', 'I')
119 1
        word = word.replace('Ó', 'O')
120 1
        word = word.replace('Ú', 'U')
121 1
        word = word.replace('Ñ', 'NY')
122 1
        word = word.replace('GÜ', 'W')
123 1
        word = word.replace('Ü', 'U')
124 1
        word = word.replace('B', 'V')
125 1
        word = word.replace('LL', 'Y')
126
127 1
        while len(meta_key) < max_length:
128 1
            if pos >= len(word):
129 1
                break
130
131
            # get the next character
132 1
            current_char = word[pos]
133
134
            # if a vowel in pos 0, add to key
135 1
            if _is_vowel(pos) and pos == 0:
136 1
                meta_key += current_char
137 1
                pos += 1
138
            # otherwise, do consonant rules
139
            else:
140
                # simple consonants (unmutated)
141 1
                if current_char in {
142
                    'D',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
143
                    'F',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
144
                    'J',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
145
                    'K',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
146
                    'M',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
147
                    'N',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
148
                    'P',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
149
                    'T',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
150
                    'V',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
151
                    'L',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
152
                    'Y',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
153
                }:
154 1
                    meta_key += current_char
155
                    # skip doubled consonants
156 1
                    if word[pos + 1 : pos + 2] == current_char:
157 1
                        pos += 2
158
                    else:
159 1
                        pos += 1
160
                else:
161 1
                    if current_char == 'C':
162
                        # special case 'acción', 'reacción',etc.
163 1
                        if word[pos + 1 : pos + 2] == 'C':
164 1
                            meta_key += 'X'
165 1
                            pos += 2
166
                        # special case 'cesar', 'cien', 'cid', 'conciencia'
167 1
                        elif word[pos + 1 : pos + 2] in {'E', 'I'}:
168 1
                            meta_key += 'Z'
169 1
                            pos += 2
170
                        # base case
171
                        else:
172 1
                            meta_key += 'K'
173 1
                            pos += 1
174 1
                    elif current_char == 'G':
175
                        # special case 'gente', 'ecologia',etc
176 1
                        if word[pos + 1 : pos + 2] in {'E', 'I'}:
177 1
                            meta_key += 'J'
178 1
                            pos += 2
179
                        # base case
180
                        else:
181 1
                            meta_key += 'G'
182 1
                            pos += 1
183 1
                    elif current_char == 'H':
184
                        # since the letter 'H' is silent in Spanish,
185
                        # set the meta key to the vowel after the letter 'H'
186 1
                        if _is_vowel(pos + 1):
187 1
                            meta_key += word[pos + 1]
188 1
                            pos += 2
189
                        else:
190 1
                            meta_key += 'H'
191 1
                            pos += 1
192 1
                    elif current_char == 'Q':
193 1
                        if word[pos + 1 : pos + 2] == 'U':
194 1
                            pos += 2
195
                        else:
196 1
                            pos += 1
197 1
                        meta_key += 'K'
198 1
                    elif current_char == 'W':
199 1
                        meta_key += 'U'
200 1
                        pos += 1
201 1
                    elif current_char == 'R':
202 1
                        meta_key += 'R'
203 1
                        pos += 1
204 1
                    elif current_char == 'S':
205 1
                        if not _is_vowel(pos + 1) and pos == 0:
206 1
                            meta_key += 'ES'
207 1
                            pos += 1
208
                        else:
209 1
                            meta_key += 'S'
210 1
                            pos += 1
211 1
                    elif current_char == 'Z':
212 1
                        meta_key += 'Z'
213 1
                        pos += 1
214 1
                    elif current_char == 'X':
215 1
                        if (
216
                            len(word) > 1
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
217
                            and pos == 0
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
218
                            and not _is_vowel(pos + 1)
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
219
                        ):
220 1
                            meta_key += 'EX'
221 1
                            pos += 1
222
                        else:
223 1
                            meta_key += 'X'
224 1
                            pos += 1
225
                    else:
226 1
                        pos += 1
227
228
        # Final change from S to Z in modified version
229 1
        if modified:
230 1
            meta_key = meta_key.replace('S', 'Z')
231
232 1
        return meta_key
233
234
235 1
def spanish_metaphone(word, max_length=6, modified=False):
236
    """Return the Spanish Metaphone of a word.
237
238
    This is a wrapper for :py:meth:`SpanishMetaphone.encode`.
239
240
    Parameters
241
    ----------
242
    word : str
243
        The word to transform
244
    max_length : int
245
        The length of the code returned (defaults to 6)
246
    modified : bool
247
        Set to True to use del Pilar Angeles & Bailón-Miguel's modified version
248
        of the algorithm
249
250
    Returns
251
    -------
252
    str
253
        The Spanish Metaphone code
254
255
    Examples
256
    --------
257
    >>> spanish_metaphone('Perez')
258
    'PRZ'
259
    >>> spanish_metaphone('Martinez')
260
    'MRTNZ'
261
    >>> spanish_metaphone('Gutierrez')
262
    'GTRRZ'
263
    >>> spanish_metaphone('Santiago')
264
    'SNTG'
265
    >>> spanish_metaphone('Nicolás')
266
    'NKLS'
267
268
    """
269 1
    return SpanishMetaphone().encode(word, max_length, modified)
270
271
272
if __name__ == '__main__':
273
    import doctest
274
275
    doctest.testmod()
276