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.fingerprint._skeleton_key. |
20
|
|
|
|
21
|
|
|
skeleton key |
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 ._fingerprint import Fingerprint |
36
|
|
|
|
37
|
1 |
|
__all__ = ['SkeletonKey', 'skeleton_key'] |
38
|
|
|
|
39
|
|
|
|
40
|
1 |
|
class SkeletonKey(Fingerprint): |
|
|
|
|
41
|
|
|
"""Skeleton Key. |
42
|
|
|
|
43
|
|
|
The skeleton key of a word is defined in :cite:`Pollock:1984`. |
44
|
|
|
""" |
45
|
|
|
|
46
|
1 |
|
_vowels = set('AEIOU') |
47
|
1 |
|
_letters = set('ABCDEFGHIJKLMNOPQRSTUVWXYZ') |
48
|
|
|
|
49
|
1 |
|
def fingerprint(self, word): |
50
|
|
|
"""Return the skeleton key. |
51
|
|
|
|
52
|
|
|
Args: |
53
|
|
|
word (str): The word to transform into its skeleton key |
54
|
|
|
|
55
|
|
|
Returns: |
56
|
|
|
str: The skeleton key |
57
|
|
|
|
58
|
|
|
Examples: |
59
|
|
|
>>> sk = SkeletonKey() |
60
|
|
|
>>> sk.fingerprint('The quick brown fox jumped over the lazy dog.') |
61
|
|
|
'THQCKBRWNFXJMPDVLZYGEUIOA' |
62
|
|
|
>>> sk.fingerprint('Christopher') |
63
|
|
|
'CHRSTPIOE' |
64
|
|
|
>>> sk.fingerprint('Niall') |
65
|
|
|
'NLIA' |
66
|
|
|
|
67
|
|
|
""" |
68
|
1 |
|
word = unicode_normalize('NFKD', text_type(word.upper())) |
69
|
1 |
|
word = ''.join(c for c in word if c in self._letters) |
70
|
1 |
|
start = word[0:1] |
71
|
1 |
|
consonant_part = '' |
72
|
1 |
|
vowel_part = '' |
73
|
|
|
|
74
|
|
|
# add consonants & vowels to to separate strings |
75
|
|
|
# (omitting the first char & duplicates) |
76
|
1 |
|
for char in word[1:]: |
77
|
1 |
|
if char != start: |
78
|
1 |
|
if char in self._vowels: |
79
|
1 |
|
if char not in vowel_part: |
80
|
1 |
|
vowel_part += char |
81
|
1 |
|
elif char not in consonant_part: |
82
|
1 |
|
consonant_part += char |
83
|
|
|
# return the first char followed by consonants followed by vowels |
84
|
1 |
|
return start + consonant_part + vowel_part |
85
|
|
|
|
86
|
|
|
|
87
|
1 |
|
def skeleton_key(word): |
88
|
|
|
"""Return the skeleton key. |
89
|
|
|
|
90
|
|
|
This is a wrapper for :py:meth:`SkeletonKey.fingerprint`. |
91
|
|
|
|
92
|
|
|
Args: |
93
|
|
|
word (str): The word to transform into its skeleton key |
94
|
|
|
|
95
|
|
|
Returns: |
96
|
|
|
str: The skeleton key |
97
|
|
|
|
98
|
|
|
Examples: |
99
|
|
|
>>> skeleton_key('The quick brown fox jumped over the lazy dog.') |
100
|
|
|
'THQCKBRWNFXJMPDVLZYGEUIOA' |
101
|
|
|
>>> skeleton_key('Christopher') |
102
|
|
|
'CHRSTPIOE' |
103
|
|
|
>>> skeleton_key('Niall') |
104
|
|
|
'NLIA' |
105
|
|
|
|
106
|
|
|
""" |
107
|
1 |
|
return SkeletonKey().fingerprint(word) |
108
|
|
|
|
109
|
|
|
|
110
|
1 |
View Code Duplication |
class OmissionKey(Fingerprint): |
|
|
|
|
111
|
|
|
"""Omission Key. |
112
|
|
|
|
113
|
|
|
The omission key of a word is defined in :cite:`Pollock:1984`. |
114
|
|
|
""" |
115
|
|
|
|
116
|
1 |
|
_consonants = tuple('JKQXZVWYBFMGPDHCLNTSR') |
117
|
1 |
|
_letters = set('ABCDEFGHIJKLMNOPQRSTUVWXYZ') |
118
|
|
|
|
119
|
1 |
|
def fingerprint(self, word): |
120
|
|
|
"""Return the omission key. |
121
|
|
|
|
122
|
|
|
Args: |
123
|
|
|
word (str): The word to transform into its omission key |
124
|
|
|
|
125
|
|
|
Returns: |
126
|
|
|
str: The omission key |
127
|
|
|
|
128
|
|
|
Examples: |
129
|
|
|
>>> ok = OmissionKey() |
130
|
|
|
>>> ok.fingerprint('The quick brown fox jumped over the lazy dog.') |
131
|
|
|
'JKQXZVWYBFMGPDHCLNTREUIOA' |
132
|
|
|
>>> ok.fingerprint('Christopher') |
133
|
|
|
'PHCTSRIOE' |
134
|
|
|
>>> ok.fingerprint('Niall') |
135
|
|
|
'LNIA' |
136
|
|
|
|
137
|
|
|
""" |
138
|
|
|
word = unicode_normalize('NFKD', text_type(word.upper())) |
139
|
|
|
word = ''.join(c for c in word if c in self._letters) |
140
|
|
|
|
141
|
|
|
key = '' |
142
|
|
|
|
143
|
|
|
# add consonants in order supplied by _consonants (no duplicates) |
144
|
|
|
for char in self._consonants: |
145
|
|
|
if char in word: |
146
|
|
|
key += char |
147
|
|
|
|
148
|
|
|
# add vowels in order they appeared in the word (no duplicates) |
149
|
|
|
for char in word: |
150
|
|
|
if char not in self._consonants and char not in key: |
151
|
|
|
key += char |
152
|
|
|
|
153
|
|
|
return key |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
if __name__ == '__main__': |
157
|
|
|
import doctest |
158
|
|
|
|
159
|
|
|
doctest.testmod() |
160
|
|
|
|