1
|
|
|
# Copyright 2019-2020 by Christopher C. Little. |
2
|
|
|
# This file is part of Abydos. |
3
|
|
|
# |
4
|
|
|
# Abydos is free software: you can redistribute it and/or modify |
5
|
|
|
# it under the terms of the GNU General Public License as published by |
6
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
# (at your option) any later version. |
8
|
|
|
# |
9
|
|
|
# Abydos is distributed in the hope that it will be useful, |
10
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
# GNU General Public License for more details. |
13
|
|
|
# |
14
|
|
|
# You should have received a copy of the GNU General Public License |
15
|
|
|
# along with Abydos. If not, see <http://www.gnu.org/licenses/>. |
16
|
|
|
|
17
|
|
|
"""abydos.fingerprint._lacss. |
18
|
|
|
|
19
|
1 |
|
L.A. County Sheriff's System fingerprint |
20
|
|
|
""" |
21
|
|
|
|
22
|
|
|
from ._fingerprint import _Fingerprint |
23
|
|
|
|
24
|
1 |
|
__all__ = ['LACSS'] |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
class LACSS(_Fingerprint): |
28
|
|
|
"""L.A. County Sheriff's System fingerprint. |
29
|
|
|
|
30
|
|
|
Based on the description from :cite:`Taft:1970`. |
31
|
1 |
|
|
32
|
|
|
.. versionadded:: 0.4.1 |
33
|
1 |
|
""" |
34
|
|
|
|
35
|
|
|
_vowels = set('AEIOUYWH') |
36
|
1 |
|
|
37
|
|
|
_t1 = {_[0]: _[1] for _ in zip('RNLTSCMDKAGBPFVZXJQ', range(1, 20))} |
38
|
|
|
_t1.update({_: 0 for _ in _vowels}) |
39
|
|
|
|
40
|
|
|
_t2 = {_[0]: _[1] for _ in zip('ABCDEFGHIJKLMNOPQRSTUVWXYZ', range(1, 27))} |
41
|
|
|
|
42
|
|
|
def fingerprint(self, word: str) -> str: |
43
|
|
|
"""Return the LACSS coding. |
44
|
1 |
|
|
45
|
|
|
Parameters |
46
|
1 |
|
---------- |
47
|
1 |
|
word : str |
48
|
|
|
The word to fingerprint |
49
|
1 |
|
|
50
|
|
|
Returns |
51
|
1 |
|
------- |
52
|
|
|
str |
53
|
|
|
The L.A. County Sheriff's System fingerprint |
54
|
|
|
|
55
|
|
|
Examples |
56
|
|
|
-------- |
57
|
|
|
>>> cf = LACSS() |
58
|
|
|
>>> cf.fingerprint('hat') |
59
|
|
|
'4911211' |
60
|
|
|
>>> cf.fingerprint('niall') |
61
|
|
|
'6488374' |
62
|
|
|
>>> cf.fingerprint('colin') |
63
|
|
|
'3015957' |
64
|
|
|
>>> cf.fingerprint('atcg') |
65
|
|
|
'1772371' |
66
|
|
|
>>> cf.fingerprint('entreatment') |
67
|
|
|
'3882324' |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
.. versionadded:: 0.4.1 |
71
|
|
|
.. versionchanged:: 0.6.0 |
72
|
|
|
Changed to return a str and added fingerprint_int method |
73
|
|
|
|
74
|
|
|
""" |
75
|
|
|
return str(self.fingerprint_int(word)) |
76
|
|
|
|
77
|
|
|
def fingerprint_int(self, word: str) -> int: |
78
|
|
|
"""Return the LACSS coding. |
79
|
|
|
|
80
|
|
|
Parameters |
81
|
|
|
---------- |
82
|
|
|
word : str |
83
|
1 |
|
The word to fingerprint |
84
|
|
|
|
85
|
|
|
Returns |
86
|
1 |
|
------- |
87
|
1 |
|
int |
88
|
|
|
The L.A. County Sheriff's System fingerprint as an int |
89
|
|
|
|
90
|
1 |
|
Examples |
91
|
1 |
|
-------- |
92
|
1 |
|
>>> cf = LACSS() |
93
|
1 |
|
>>> cf.fingerprint_int('hat') |
94
|
1 |
|
4911211 |
95
|
1 |
|
>>> cf.fingerprint_int('niall') |
96
|
|
|
6488374 |
97
|
1 |
|
>>> cf.fingerprint_int('colin') |
98
|
1 |
|
3015957 |
99
|
1 |
|
>>> cf.fingerprint_int('atcg') |
100
|
1 |
|
1772371 |
101
|
1 |
|
>>> cf.fingerprint_int('entreatment') |
102
|
1 |
|
3882324 |
103
|
1 |
|
|
104
|
|
|
|
105
|
1 |
|
.. versionadded:: 0.6.0 |
106
|
1 |
|
|
107
|
|
|
""" |
108
|
1 |
|
# uppercase |
109
|
|
|
word = word.upper() |
110
|
|
|
|
111
|
|
|
# remove vowels |
112
|
|
|
word = word[:1] + ''.join(_ for _ in word[1:] if _ not in 'AEIOUWHY') |
113
|
|
|
word += 12 * 'A' |
114
|
|
|
|
115
|
|
|
# step 1 |
116
|
|
|
code = 0 |
117
|
|
|
i = 0 |
118
|
|
|
while (not code) and (i < len(word)): |
119
|
|
|
if word[i] in self._t2: |
120
|
|
|
code = self._t2[word[i]] * 10 |
121
|
|
|
i += 1 |
122
|
|
|
|
123
|
|
|
letters = 11 |
124
|
|
|
while letters and i < len(word): |
125
|
|
|
if word[i] in self._t1: |
126
|
|
|
code *= 10 |
127
|
|
|
code += self._t1[word[i]] |
128
|
|
|
letters -= 1 |
129
|
|
|
i += 1 |
130
|
|
|
|
131
|
|
|
code *= 3 |
132
|
|
|
code = int(code ** 0.5) |
133
|
|
|
|
134
|
|
|
return code |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
if __name__ == '__main__': |
138
|
|
|
import doctest |
139
|
|
|
|
140
|
|
|
doctest.testmod() |
141
|
|
|
|