Passed
Push — master ( d2a11f...643512 )
by Chris
01:59 queued 12s
created

abydos.fingerprint._lacss.LACSS.fingerprint()   B

Complexity

Conditions 7

Size

Total Lines 58
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 7

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 58
ccs 20
cts 20
cp 1
rs 8
c 0
b 0
f 0
cc 7
nop 2
crap 7

How to fix   Long Method   

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:

1
# -*- coding: utf-8 -*-
2
3
# Copyright 2019 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.fingerprint._lacss.
20
21
L.A. County Sheriff's System fingerprint
22
"""
23
24 1
from __future__ import (
25
    absolute_import,
26
    division,
27
    print_function,
28
    unicode_literals,
29
)
30
31 1
from ._fingerprint import _Fingerprint
32
33 1
__all__ = ['LACSS']
34
35
36 1
class LACSS(_Fingerprint):
37
    """L.A. County Sheriff's System fingerprint.
38
39
    Based on the description from :cite:`Taft:1970`.
40
41
    .. versionadded:: 0.4.1
42
    """
43
44 1
    _vowels = set('AEIOUYWH')
45
46 1
    _t1 = {_[0]: _[1] for _ in zip('RNLTSCMDKAGBPFVZXJQ', range(1, 20))}
47 1
    _t1.update({_: 0 for _ in _vowels})
48
49 1
    _t2 = {_[0]: _[1] for _ in zip('ABCDEFGHIJKLMNOPQRSTUVWXYZ', range(1, 27))}
50
51 1
    def fingerprint(self, word):
52
        """Return the LACSS coding.
53
54
        Parameters
55
        ----------
56
        word : str
57
            The word to fingerprint
58
59
        Returns
60
        -------
61
        int
62
            The L.A. County Sheriff's System fingerprint
63
64
        Examples
65
        --------
66
        >>> cf = LACSS()
67
        >>> cf.fingerprint('hat')
68
        '4911211'
69
        >>> cf.fingerprint('niall')
70
        '6488374'
71
        >>> cf.fingerprint('colin')
72
        '3015957'
73
        >>> cf.fingerprint('atcg')
74
        '1772371'
75
        >>> cf.fingerprint('entreatment')
76
        '3882324'
77
78
79
        .. versionadded:: 0.4.1
80
81
        """
82
        # uppercase
83 1
        word = word.upper()
84
85
        # remove vowels
86 1
        word = word[:1] + ''.join(_ for _ in word[1:] if _ not in 'AEIOUWHY')
87 1
        word += 12 * 'A'
88
89
        # step 1
90 1
        code = 0
91 1
        i = 0
92 1
        while (not code) and (i < len(word)):
93 1
            if word[i] in self._t2:
94 1
                code = self._t2[word[i]] * 10
95 1
            i += 1
96
97 1
        letters = 11
98 1
        while letters and i < len(word):
99 1
            if word[i] in self._t1:
100 1
                code *= 10
101 1
                code += self._t1[word[i]]
102 1
                letters -= 1
103 1
            i += 1
104
105 1
        code *= 3
106 1
        code = str(int(code ** 0.5))
107
108 1
        return code
109
110
111
if __name__ == '__main__':
112
    import doctest
113
114
    doctest.testmod()
115