|
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._sound_d. |
|
20
|
|
|
|
|
21
|
|
|
SoundD 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 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__ = ['SoundD', 'sound_d'] |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
1 |
|
class SoundD(_Phonetic): |
|
|
|
|
|
|
41
|
|
|
"""SoundD code. |
|
42
|
|
|
|
|
43
|
|
|
SoundD is defined in :cite:`Varol:2012`. |
|
44
|
|
|
""" |
|
45
|
|
|
|
|
46
|
1 |
|
_trans = dict( |
|
47
|
|
|
zip( |
|
48
|
|
|
(ord(_) for _ in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), |
|
|
|
|
|
|
49
|
|
|
'01230120022455012623010202', |
|
50
|
|
|
) |
|
51
|
|
|
) |
|
52
|
|
|
|
|
53
|
1 |
|
def encode(self, word, max_length=4): |
|
|
|
|
|
|
54
|
|
|
"""Return the SoundD code. |
|
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 4) |
|
62
|
|
|
|
|
63
|
|
|
Returns |
|
64
|
|
|
------- |
|
65
|
|
|
str |
|
66
|
|
|
The SoundD code |
|
67
|
|
|
|
|
68
|
|
|
Examples |
|
69
|
|
|
-------- |
|
70
|
|
|
>>> sound_d('Gough') |
|
71
|
|
|
'2000' |
|
72
|
|
|
>>> sound_d('pneuma') |
|
73
|
|
|
'5500' |
|
74
|
|
|
>>> sound_d('knight') |
|
75
|
|
|
'5300' |
|
76
|
|
|
>>> sound_d('trice') |
|
77
|
|
|
'3620' |
|
78
|
|
|
>>> sound_d('judge') |
|
79
|
|
|
'2200' |
|
80
|
|
|
|
|
81
|
|
|
""" |
|
82
|
1 |
|
word = unicode_normalize('NFKD', text_type(word.upper())) |
|
83
|
1 |
|
word = word.replace('ß', 'SS') |
|
84
|
1 |
|
word = ''.join(c for c in word if c in self._uc_set) |
|
85
|
|
|
|
|
86
|
1 |
|
if word[:2] in {'KN', 'GN', 'PN', 'AC', 'WR'}: |
|
87
|
1 |
|
word = word[1:] |
|
88
|
1 |
|
elif word[:1] == 'X': |
|
89
|
1 |
|
word = 'S' + word[1:] |
|
90
|
1 |
|
elif word[:2] == 'WH': |
|
91
|
1 |
|
word = 'W' + word[2:] |
|
92
|
|
|
|
|
93
|
1 |
|
word = ( |
|
94
|
|
|
word.replace('DGE', '20').replace('DGI', '20').replace('GH', '0') |
|
95
|
|
|
) |
|
96
|
|
|
|
|
97
|
1 |
|
word = word.translate(self._trans) |
|
98
|
1 |
|
word = self._delete_consecutive_repeats(word) |
|
99
|
1 |
|
word = word.replace('0', '') |
|
100
|
|
|
|
|
101
|
1 |
|
if max_length != -1: |
|
102
|
1 |
|
if len(word) < max_length: |
|
103
|
1 |
|
word += '0' * (max_length - len(word)) |
|
104
|
|
|
else: |
|
105
|
1 |
|
word = word[:max_length] |
|
106
|
|
|
|
|
107
|
1 |
|
return word |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
1 |
|
def sound_d(word, max_length=4): |
|
111
|
|
|
"""Return the SoundD code. |
|
112
|
|
|
|
|
113
|
|
|
Parameters |
|
114
|
|
|
---------- |
|
115
|
|
|
word : str |
|
116
|
|
|
The word to transform |
|
117
|
|
|
max_length : int |
|
118
|
|
|
The length of the code returned (defaults to 4) |
|
119
|
|
|
|
|
120
|
|
|
Returns |
|
121
|
|
|
------- |
|
122
|
|
|
str |
|
123
|
|
|
The SoundD code |
|
124
|
|
|
|
|
125
|
|
|
Examples |
|
126
|
|
|
-------- |
|
127
|
|
|
>>> sound_d('Gough') |
|
128
|
|
|
'2000' |
|
129
|
|
|
>>> sound_d('pneuma') |
|
130
|
|
|
'5500' |
|
131
|
|
|
>>> sound_d('knight') |
|
132
|
|
|
'5300' |
|
133
|
|
|
>>> sound_d('trice') |
|
134
|
|
|
'3620' |
|
135
|
|
|
>>> sound_d('judge') |
|
136
|
|
|
'2200' |
|
137
|
|
|
|
|
138
|
|
|
""" |
|
139
|
1 |
|
return SoundD().encode(word, max_length) |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
if __name__ == '__main__': |
|
143
|
|
|
import doctest |
|
144
|
|
|
|
|
145
|
|
|
doctest.testmod() |
|
146
|
|
|
|