|
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._onca. |
|
20
|
|
|
|
|
21
|
|
|
Oxford Name Compression Algorithm (ONCA) |
|
22
|
|
|
""" |
|
23
|
|
|
|
|
24
|
1 |
|
from __future__ import ( |
|
25
|
|
|
absolute_import, |
|
26
|
|
|
division, |
|
27
|
|
|
print_function, |
|
28
|
|
|
unicode_literals, |
|
29
|
|
|
) |
|
30
|
|
|
|
|
31
|
1 |
|
from ._nysiis import NYSIIS |
|
32
|
1 |
|
from ._phonetic import _Phonetic |
|
33
|
1 |
|
from ._soundex import Soundex |
|
34
|
|
|
|
|
35
|
1 |
|
__all__ = ['ONCA', 'onca'] |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
1 |
|
class ONCA(_Phonetic): |
|
|
|
|
|
|
39
|
|
|
"""Oxford Name Compression Algorithm (ONCA). |
|
40
|
|
|
|
|
41
|
|
|
This is the Oxford Name Compression Algorithm, based on :cite:`Gill:1997`. |
|
42
|
|
|
|
|
43
|
|
|
I can find no complete description of the "anglicised version of the NYSIIS |
|
44
|
|
|
method" identified as the first step in this algorithm, so this is likely |
|
45
|
|
|
not a precisely correct implementation, in that it employs the standard |
|
46
|
|
|
NYSIIS algorithm. |
|
47
|
|
|
""" |
|
48
|
|
|
|
|
49
|
1 |
|
_nysiis = NYSIIS() |
|
50
|
1 |
|
_soundex = Soundex() |
|
51
|
|
|
|
|
52
|
1 |
|
def encode(self, word, max_length=4, zero_pad=True): |
|
|
|
|
|
|
53
|
|
|
"""Return the Oxford Name Compression Algorithm (ONCA) code for a word. |
|
54
|
|
|
|
|
55
|
|
|
Parameters |
|
56
|
|
|
---------- |
|
57
|
|
|
word : str |
|
58
|
|
|
The word to transform |
|
59
|
|
|
max_length : int |
|
60
|
|
|
The maximum length (default 5) of the code to return |
|
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 ONCA code |
|
69
|
|
|
|
|
70
|
|
|
Examples |
|
71
|
|
|
-------- |
|
72
|
|
|
>>> pe = ONCA() |
|
73
|
|
|
>>> pe.encode('Christopher') |
|
74
|
|
|
'C623' |
|
75
|
|
|
>>> pe.encode('Niall') |
|
76
|
|
|
'N400' |
|
77
|
|
|
>>> pe.encode('Smith') |
|
78
|
|
|
'S530' |
|
79
|
|
|
>>> pe.encode('Schmidt') |
|
80
|
|
|
'S530' |
|
81
|
|
|
|
|
82
|
|
|
""" |
|
83
|
|
|
# In the most extreme case, 3 characters of NYSIIS input can be |
|
84
|
|
|
# compressed to one character of output, so give it triple the |
|
85
|
|
|
# max_length. |
|
86
|
1 |
|
return self._soundex.encode( |
|
87
|
|
|
self._nysiis.encode(word, max_length=max_length * 3), |
|
88
|
|
|
max_length, |
|
89
|
|
|
zero_pad=zero_pad, |
|
90
|
|
|
) |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
1 |
|
def onca(word, max_length=4, zero_pad=True): |
|
94
|
|
|
"""Return the Oxford Name Compression Algorithm (ONCA) code for a word. |
|
95
|
|
|
|
|
96
|
|
|
This is a wrapper for :py:meth:`ONCA.encode`. |
|
97
|
|
|
|
|
98
|
|
|
Parameters |
|
99
|
|
|
---------- |
|
100
|
|
|
word : str |
|
101
|
|
|
The word to transform |
|
102
|
|
|
max_length : int |
|
103
|
|
|
The maximum length (default 5) of the code to return |
|
104
|
|
|
zero_pad : bool |
|
105
|
|
|
Pad the end of the return value with 0s to achieve a max_length string |
|
106
|
|
|
|
|
107
|
|
|
Returns |
|
108
|
|
|
------- |
|
109
|
|
|
str |
|
110
|
|
|
The ONCA code |
|
111
|
|
|
|
|
112
|
|
|
Examples |
|
113
|
|
|
-------- |
|
114
|
|
|
>>> onca('Christopher') |
|
115
|
|
|
'C623' |
|
116
|
|
|
>>> onca('Niall') |
|
117
|
|
|
'N400' |
|
118
|
|
|
>>> onca('Smith') |
|
119
|
|
|
'S530' |
|
120
|
|
|
>>> onca('Schmidt') |
|
121
|
|
|
'S530' |
|
122
|
|
|
|
|
123
|
|
|
""" |
|
124
|
1 |
|
return ONCA().encode(word, max_length, zero_pad) |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
if __name__ == '__main__': |
|
128
|
|
|
import doctest |
|
129
|
|
|
|
|
130
|
|
|
doctest.testmod() |
|
131
|
|
|
|