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
|
|
|
Parmar-Kumbharana phonetic algorithm |
22
|
|
|
""" |
23
|
|
|
|
24
|
1 |
|
from __future__ import ( |
25
|
|
|
absolute_import, |
26
|
|
|
division, |
27
|
|
|
print_function, |
28
|
|
|
unicode_literals, |
29
|
|
|
) |
30
|
|
|
|
31
|
1 |
|
from six.moves import range |
32
|
|
|
|
33
|
1 |
|
from ._phonetic import _Phonetic |
34
|
|
|
|
35
|
1 |
|
__all__ = ['ParmarKumbharana', 'parmar_kumbharana'] |
36
|
|
|
|
37
|
|
|
|
38
|
1 |
|
class ParmarKumbharana(_Phonetic): |
|
|
|
|
39
|
|
|
"""Parmar-Kumbharana code. |
40
|
|
|
|
41
|
|
|
This is based on the phonetic algorithm proposed in :cite:`Parmar:2014`. |
42
|
|
|
""" |
43
|
|
|
|
44
|
1 |
|
_rules = { |
45
|
|
|
4: {'OUGH': 'F'}, |
46
|
|
|
3: {'DGE': 'J', 'OUL': 'U', 'GHT': 'T'}, |
47
|
|
|
2: { |
48
|
|
|
'CE': 'S', |
49
|
|
|
'CI': 'S', |
50
|
|
|
'CY': 'S', |
51
|
|
|
'GE': 'J', |
52
|
|
|
'GI': 'J', |
53
|
|
|
'GY': 'J', |
54
|
|
|
'WR': 'R', |
55
|
|
|
'GN': 'N', |
56
|
|
|
'KN': 'N', |
57
|
|
|
'PN': 'N', |
58
|
|
|
'CK': 'K', |
59
|
|
|
'SH': 'S', |
60
|
|
|
}, |
61
|
|
|
} |
62
|
1 |
|
_del_trans = {65: '', 69: '', 73: '', 79: '', 85: '', 89: ''} |
63
|
|
|
|
64
|
1 |
|
def encode(self, word): |
65
|
|
|
"""Return the Parmar-Kumbharana encoding of a word. |
66
|
|
|
|
67
|
|
|
Parameters |
68
|
|
|
---------- |
69
|
|
|
word : str |
70
|
|
|
The word to transform |
71
|
|
|
|
72
|
|
|
Returns |
73
|
|
|
------- |
74
|
|
|
str |
75
|
|
|
The Parmar-Kumbharana encoding |
76
|
|
|
|
77
|
|
|
Examples |
78
|
|
|
-------- |
79
|
|
|
>>> pe = ParmarKumbharana() |
80
|
|
|
>>> pe.encode('Gough') |
81
|
|
|
'GF' |
82
|
|
|
>>> pe.encode('pneuma') |
83
|
|
|
'NM' |
84
|
|
|
>>> pe.encode('knight') |
85
|
|
|
'NT' |
86
|
|
|
>>> pe.encode('trice') |
87
|
|
|
'TRS' |
88
|
|
|
>>> pe.encode('judge') |
89
|
|
|
'JJ' |
90
|
|
|
|
91
|
|
|
""" |
92
|
1 |
|
word = word.upper() # Rule 3 |
93
|
1 |
|
word = self._delete_consecutive_repeats(word) # Rule 4 |
94
|
|
|
|
95
|
|
|
# Rule 5 |
96
|
1 |
|
i = 0 |
97
|
1 |
|
while i < len(word): |
98
|
1 |
|
for match_len in range(4, 1, -1): |
99
|
1 |
|
if word[i : i + match_len] in self._rules[match_len]: |
100
|
1 |
|
repl = self._rules[match_len][word[i : i + match_len]] |
101
|
1 |
|
word = word[:i] + repl + word[i + match_len :] |
102
|
1 |
|
i += len(repl) |
103
|
1 |
|
break |
104
|
|
|
else: |
105
|
1 |
|
i += 1 |
106
|
|
|
|
107
|
1 |
|
word = word[:1] + word[1:].translate(self._del_trans) # Rule 6 |
108
|
1 |
|
return word |
109
|
|
|
|
110
|
|
|
|
111
|
1 |
|
def parmar_kumbharana(word): |
112
|
|
|
"""Return the Parmar-Kumbharana encoding of a word. |
113
|
|
|
|
114
|
|
|
This is a wrapper for :py:meth:`ParmarKumbharana.encode`. |
115
|
|
|
|
116
|
|
|
Parameters |
117
|
|
|
---------- |
118
|
|
|
word : str |
119
|
|
|
The word to transform |
120
|
|
|
|
121
|
|
|
Returns |
122
|
|
|
------- |
123
|
|
|
str |
124
|
|
|
The Parmar-Kumbharana encoding |
125
|
|
|
|
126
|
|
|
Examples |
127
|
|
|
-------- |
128
|
|
|
>>> parmar_kumbharana('Gough') |
129
|
|
|
'GF' |
130
|
|
|
>>> parmar_kumbharana('pneuma') |
131
|
|
|
'NM' |
132
|
|
|
>>> parmar_kumbharana('knight') |
133
|
|
|
'NT' |
134
|
|
|
>>> parmar_kumbharana('trice') |
135
|
|
|
'TRS' |
136
|
|
|
>>> parmar_kumbharana('judge') |
137
|
|
|
'JJ' |
138
|
|
|
|
139
|
|
|
""" |
140
|
1 |
|
return ParmarKumbharana().encode(word) |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
if __name__ == '__main__': |
144
|
|
|
import doctest |
145
|
|
|
|
146
|
|
|
doctest.testmod() |
147
|
|
|
|