Completed
Branch master (78a222)
by Chris
14:36
created

abydos.phonetic._davidson.davidson()   A

Complexity

Conditions 2

Size

Total Lines 42
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 42
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 2
nop 3
crap 2
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._davidson.
20
21
The phonetic._davidson module implements Davidson's Consonant Code.
22
"""
23
24 1
from __future__ import unicode_literals
25
26 1
from six import text_type
27
28 1
from ._util import _delete_consecutive_repeats
29
30 1
__all__ = ['davidson']
31
32
33 1
def davidson(lname, fname='.', omit_fname=False):
34
    """Return Davidson's Consonant Code.
35
36
    This is based on the name compression system described in
37
    :cite:`Davidson:1962`.
38
39
    :cite:`Dolby:1970` identifies this as having been the name compression
40
    algorithm used by SABRE.
41
42
    :param str lname: Last name (or word) to be encoded
43
    :param str fname: First name (optional), of which the first character is
44
        included in the code.
45
    :param bool omit_fname: Set to True to completely omit the first character
46
        of the first name
47
    :returns: Davidson's Consonant Code
48
    :rtype: str
49
50
    >>> davidson('Gough')
51
    'G   .'
52
    >>> davidson('pneuma')
53
    'PNM .'
54
    >>> davidson('knight')
55
    'KNGT.'
56
    >>> davidson('trice')
57
    'TRC .'
58
    >>> davidson('judge')
59
    'JDG .'
60
    >>> davidson('Smith', 'James')
61
    'SMT J'
62
    >>> davidson('Wasserman', 'Tabitha')
63
    'WSRMT'
64
    """
65 1
    trans = {65: '', 69: '', 73: '', 79: '', 85: '', 72: '', 87: '', 89: ''}
66
67 1
    lname = text_type(lname.upper())
68 1
    code = _delete_consecutive_repeats(lname[:1] + lname[1:].translate(trans))
69 1
    code = code[:4] + (4 - len(code)) * ' '
70
71 1
    if not omit_fname:
72 1
        code += fname[:1].upper()
73
74 1
    return code
75
76
77
if __name__ == '__main__':
78
    import doctest
79
80
    doctest.testmod()
81