|
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._phonetic_spanish. |
|
20
|
|
|
|
|
21
|
|
|
Phonetic Spanish |
|
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__ = ['PhoneticSpanish', 'phonetic_spanish'] |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
1 |
|
class PhoneticSpanish(_Phonetic): |
|
|
|
|
|
|
41
|
|
|
"""PhoneticSpanish. |
|
42
|
|
|
|
|
43
|
|
|
This follows the coding described in :cite:`Amon:2012` and |
|
44
|
|
|
:cite:`delPilarAngeles:2015`. |
|
45
|
|
|
""" |
|
46
|
|
|
|
|
47
|
1 |
|
_trans = dict( |
|
48
|
|
|
zip((ord(_) for _ in 'BCDFGHJKLMNPQRSTVXYZ'), '14328287566079431454') |
|
|
|
|
|
|
49
|
|
|
) |
|
50
|
|
|
|
|
51
|
1 |
|
_uc_set = set('BCDFGHJKLMNPQRSTVXYZ') |
|
52
|
|
|
|
|
53
|
1 |
|
def encode(self, word, max_length=-1): |
|
|
|
|
|
|
54
|
|
|
"""Return the PhoneticSpanish coding of word. |
|
55
|
|
|
|
|
56
|
|
|
Parameters |
|
57
|
|
|
---------- |
|
58
|
|
|
word : str |
|
59
|
|
|
The word to transform |
|
60
|
|
|
max_length : int |
|
61
|
|
|
The length of the code returned (defaults to unlimited) |
|
62
|
|
|
|
|
63
|
|
|
Returns |
|
64
|
|
|
------- |
|
65
|
|
|
str |
|
66
|
|
|
The PhoneticSpanish code |
|
67
|
|
|
|
|
68
|
|
|
Examples |
|
69
|
|
|
-------- |
|
70
|
|
|
>>> pe = PhoneticSpanish() |
|
71
|
|
|
>>> pe.encode('Perez') |
|
72
|
|
|
'094' |
|
73
|
|
|
>>> pe.encode('Martinez') |
|
74
|
|
|
'69364' |
|
75
|
|
|
>>> pe.encode('Gutierrez') |
|
76
|
|
|
'83994' |
|
77
|
|
|
>>> pe.encode('Santiago') |
|
78
|
|
|
'4638' |
|
79
|
|
|
>>> pe.encode('Nicolás') |
|
80
|
|
|
'6454' |
|
81
|
|
|
|
|
82
|
|
|
""" |
|
83
|
|
|
# uppercase, normalize, and decompose, filter to A-Z minus vowels & W |
|
84
|
1 |
|
word = unicode_normalize('NFKD', text_type(word.upper())) |
|
85
|
1 |
|
word = ''.join(c for c in word if c in self._uc_set) |
|
86
|
|
|
|
|
87
|
|
|
# merge repeated Ls & Rs |
|
88
|
1 |
|
word = word.replace('LL', 'L') |
|
89
|
1 |
|
word = word.replace('R', 'R') |
|
90
|
|
|
|
|
91
|
|
|
# apply the Soundex algorithm |
|
92
|
1 |
|
sdx = word.translate(self._trans) |
|
93
|
|
|
|
|
94
|
1 |
|
if max_length > 0: |
|
95
|
1 |
|
sdx = (sdx + ('0' * max_length))[:max_length] |
|
96
|
|
|
|
|
97
|
1 |
|
return sdx |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
1 |
|
def phonetic_spanish(word, max_length=-1): |
|
101
|
|
|
"""Return the PhoneticSpanish coding of word. |
|
102
|
|
|
|
|
103
|
|
|
This is a wrapper for :py:meth:`PhoneticSpanish.encode`. |
|
104
|
|
|
|
|
105
|
|
|
Parameters |
|
106
|
|
|
---------- |
|
107
|
|
|
word : str |
|
108
|
|
|
The word to transform |
|
109
|
|
|
max_length : int |
|
110
|
|
|
The length of the code returned (defaults to unlimited) |
|
111
|
|
|
|
|
112
|
|
|
Returns |
|
113
|
|
|
------- |
|
114
|
|
|
str |
|
115
|
|
|
The PhoneticSpanish code |
|
116
|
|
|
|
|
117
|
|
|
Examples |
|
118
|
|
|
-------- |
|
119
|
|
|
>>> phonetic_spanish('Perez') |
|
120
|
|
|
'094' |
|
121
|
|
|
>>> phonetic_spanish('Martinez') |
|
122
|
|
|
'69364' |
|
123
|
|
|
>>> phonetic_spanish('Gutierrez') |
|
124
|
|
|
'83994' |
|
125
|
|
|
>>> phonetic_spanish('Santiago') |
|
126
|
|
|
'4638' |
|
127
|
|
|
>>> phonetic_spanish('Nicolás') |
|
128
|
|
|
'6454' |
|
129
|
|
|
|
|
130
|
|
|
""" |
|
131
|
1 |
|
return PhoneticSpanish().encode(word, max_length) |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
if __name__ == '__main__': |
|
135
|
|
|
import doctest |
|
136
|
|
|
|
|
137
|
|
|
doctest.testmod() |
|
138
|
|
|
|