1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
# Copyright 2014-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._lein. |
20
|
|
|
|
21
|
|
|
Lein |
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__ = ['Lein', 'lein'] |
38
|
|
|
|
39
|
|
|
|
40
|
1 |
|
class Lein(_Phonetic): |
41
|
|
|
"""Lein code. |
42
|
|
|
|
43
|
|
|
This is Lein name coding, described in :cite:`Moore:1977`. |
44
|
|
|
""" |
45
|
|
|
|
46
|
1 |
|
_trans = dict( |
47
|
|
|
zip((ord(_) for _ in 'BCDFGJKLMNPQRSTVXZ'), '451455532245351455') |
|
|
|
|
48
|
|
|
) |
49
|
|
|
|
50
|
1 |
|
_del_trans = {num: None for num in (32, 65, 69, 72, 73, 79, 85, 87, 89)} |
51
|
|
|
|
52
|
1 |
|
def encode(self, word, max_length=4, zero_pad=True): |
53
|
|
|
"""Return the Lein code for a word. |
54
|
|
|
|
55
|
|
|
Parameters |
56
|
|
|
---------- |
57
|
|
|
word : str |
58
|
|
|
The word to transform |
59
|
|
|
max_length : int |
60
|
|
|
The length of the code returned (defaults to 4) |
61
|
|
|
zero_pad : bool |
62
|
|
|
Pad the end of the return value with 0s to achieve a max_length |
63
|
|
|
string |
64
|
|
|
|
65
|
|
|
Returns |
66
|
|
|
------- |
67
|
|
|
str |
68
|
|
|
The Lein code |
69
|
|
|
|
70
|
|
|
Examples |
71
|
|
|
-------- |
72
|
|
|
>>> pe = Lein() |
73
|
|
|
>>> pe.encode('Christopher') |
74
|
|
|
'C351' |
75
|
|
|
>>> pe.encode('Niall') |
76
|
|
|
'N300' |
77
|
|
|
>>> pe.encode('Smith') |
78
|
|
|
'S210' |
79
|
|
|
>>> pe.encode('Schmidt') |
80
|
|
|
'S521' |
81
|
|
|
|
82
|
|
|
""" |
83
|
|
|
# uppercase, normalize, decompose, and filter non-A-Z out |
84
|
1 |
|
word = unicode_normalize('NFKD', text_type(word.upper())) |
85
|
1 |
|
word = word.replace('ß', 'SS') |
86
|
1 |
|
word = ''.join(c for c in word if c in self._uc_set) |
87
|
|
|
|
88
|
1 |
|
code = word[:1] # Rule 1 |
89
|
1 |
|
word = word[1:].translate(self._del_trans) # Rule 2 |
90
|
1 |
|
word = self._delete_consecutive_repeats(word) # Rule 3 |
91
|
1 |
|
code += word.translate(self._trans) # Rule 4 |
92
|
|
|
|
93
|
1 |
|
if zero_pad: |
94
|
1 |
|
code += '0' * max_length # Rule 4 |
95
|
|
|
|
96
|
1 |
|
return code[:max_length] |
97
|
|
|
|
98
|
|
|
|
99
|
1 |
|
def lein(word, max_length=4, zero_pad=True): |
100
|
|
|
"""Return the Lein code for a word. |
101
|
|
|
|
102
|
|
|
This is a wrapper for :py:meth:`Lein.encode`. |
103
|
|
|
|
104
|
|
|
Parameters |
105
|
|
|
---------- |
106
|
|
|
word : str |
107
|
|
|
The word to transform |
108
|
|
|
max_length : int |
109
|
|
|
The length of the code returned (defaults to 4) |
110
|
|
|
zero_pad : bool |
111
|
|
|
Pad the end of the return value with 0s to achieve a max_length string |
112
|
|
|
|
113
|
|
|
Returns |
114
|
|
|
------- |
115
|
|
|
str |
116
|
|
|
The Lein code |
117
|
|
|
|
118
|
|
|
Examples |
119
|
|
|
-------- |
120
|
|
|
>>> lein('Christopher') |
121
|
|
|
'C351' |
122
|
|
|
>>> lein('Niall') |
123
|
|
|
'N300' |
124
|
|
|
>>> lein('Smith') |
125
|
|
|
'S210' |
126
|
|
|
>>> lein('Schmidt') |
127
|
|
|
'S521' |
128
|
|
|
|
129
|
|
|
""" |
130
|
1 |
|
return Lein().encode(word, max_length, zero_pad) |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
if __name__ == '__main__': |
134
|
|
|
import doctest |
135
|
|
|
|
136
|
|
|
doctest.testmod() |
137
|
|
|
|