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

parmar_kumbharana()   B

Complexity

Conditions 5

Size

Total Lines 57
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 57
ccs 16
cts 16
cp 1
rs 8.6693
c 0
b 0
f 0
cc 5
nop 1
crap 5

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 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._parmar_kumbharana.
20
21
The phonetic._parmar_kumbharana module implements the Parmar-Kumbharana
22
phonetic algorithm.
23
"""
24
25 1
from __future__ import unicode_literals
26
27 1
from six.moves import range
28
29 1
from ._util import _delete_consecutive_repeats
30
31 1
__all__ = ['parmar_kumbharana']
32
33
34 1
def parmar_kumbharana(word):
35
    """Return the Parmar-Kumbharana encoding of a word.
36
37
    This is based on the phonetic algorithm proposed in :cite:`Parmar:2014`.
38
39
    :param str word: the word to transform
40
    :returns: the Parmar-Kumbharana encoding
41
    :rtype: str
42
43
    >>> parmar_kumbharana('Gough')
44
    'GF'
45
    >>> parmar_kumbharana('pneuma')
46
    'NM'
47
    >>> parmar_kumbharana('knight')
48
    'NT'
49
    >>> parmar_kumbharana('trice')
50
    'TRS'
51
    >>> parmar_kumbharana('judge')
52
    'JJ'
53
    """
54 1
    rule_table = {
55
        4: {'OUGH': 'F'},
56
        3: {'DGE': 'J', 'OUL': 'U', 'GHT': 'T'},
57
        2: {
58
            'CE': 'S',
59
            'CI': 'S',
60
            'CY': 'S',
61
            'GE': 'J',
62
            'GI': 'J',
63
            'GY': 'J',
64
            'WR': 'R',
65
            'GN': 'N',
66
            'KN': 'N',
67
            'PN': 'N',
68
            'CK': 'K',
69
            'SH': 'S',
70
        },
71
    }
72 1
    vowel_trans = {65: '', 69: '', 73: '', 79: '', 85: '', 89: ''}
73
74 1
    word = word.upper()  # Rule 3
75 1
    word = _delete_consecutive_repeats(word)  # Rule 4
76
77
    # Rule 5
78 1
    i = 0
79 1
    while i < len(word):
80 1
        for match_len in range(4, 1, -1):
81 1
            if word[i : i + match_len] in rule_table[match_len]:
82 1
                repl = rule_table[match_len][word[i : i + match_len]]
83 1
                word = word[:i] + repl + word[i + match_len :]
84 1
                i += len(repl)
85 1
                break
86
        else:
87 1
            i += 1
88
89 1
    word = word[:1] + word[1:].translate(vowel_trans)  # Rule 6
90 1
    return word
91
92
93
if __name__ == '__main__':
94
    import doctest
95
96
    doctest.testmod()
97