1
|
|
|
# Copyright 2014-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._phonetic. |
18
|
|
|
|
19
|
1 |
|
phonetic fingerprint |
20
|
|
|
""" |
21
|
|
|
|
22
|
|
|
from ._string import String |
23
|
|
|
from ..phonetic import DoubleMetaphone |
24
|
1 |
|
from ..phonetic._phonetic import _Phonetic |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
__all__ = ['Phonetic'] |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class Phonetic(String): |
31
|
1 |
|
"""Phonetic Fingerprint. |
32
|
|
|
|
33
|
1 |
|
A phonetic fingerprint is identical to a standard string fingerprint, as |
34
|
|
|
implemented in :py:class:`.String`, but performs the fingerprinting |
35
|
1 |
|
function after converting the string to its phonetic form, as determined by |
36
|
1 |
|
some phonetic algorithm. This fingerprint is described at |
37
|
1 |
|
:cite:`OpenRefine:2012`. |
38
|
1 |
|
|
39
|
|
|
.. versionadded:: 0.3.6 |
40
|
|
|
""" |
41
|
1 |
|
|
42
|
|
|
def __init__(self, phonetic_algorithm=None, joiner=' '): |
43
|
|
|
"""Initialize Phonetic instance. |
44
|
1 |
|
|
45
|
|
|
phonetic_algorithm : function |
46
|
|
|
A phonetic algorithm that takes a string and returns a string |
47
|
|
|
(presumably a phonetic representation of the original string). By |
48
|
|
|
default, this function uses :py:func:`.double_metaphone`. |
49
|
|
|
joiner : str |
50
|
|
|
The string that will be placed between each word |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
.. versionadded:: 0.4.0 |
54
|
|
|
|
55
|
|
|
""" |
56
|
1 |
|
self._phonetic_algorithm = phonetic_algorithm |
57
|
|
|
if phonetic_algorithm is None: |
58
|
|
|
self._phonetic_algorithm = DoubleMetaphone() |
59
|
|
|
|
60
|
|
|
self._joiner = joiner |
61
|
|
|
|
62
|
|
|
def fingerprint(self, phrase): |
63
|
|
|
"""Return the phonetic fingerprint of a phrase. |
64
|
|
|
|
65
|
|
|
Parameters |
66
|
|
|
---------- |
67
|
|
|
phrase : str |
68
|
|
|
The string from which to calculate the phonetic fingerprint |
69
|
|
|
|
70
|
1 |
|
Returns |
71
|
1 |
|
------- |
72
|
1 |
|
str |
73
|
|
|
The phonetic fingerprint of the phrase |
74
|
1 |
|
|
75
|
|
|
Examples |
76
|
1 |
|
-------- |
77
|
|
|
>>> pf = Phonetic() |
78
|
|
|
>>> pf.fingerprint('The quick brown fox jumped over the lazy dog.') |
79
|
|
|
'0 afr fks jmpt kk ls prn tk' |
80
|
|
|
|
81
|
|
|
>>> from abydos.phonetic import Soundex |
82
|
|
|
>>> pf = Phonetic(Soundex()) |
83
|
|
|
>>> pf.fingerprint('The quick brown fox jumped over the lazy dog.') |
84
|
|
|
'b650 d200 f200 j513 l200 o160 q200 t000' |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
.. versionadded:: 0.1.0 |
88
|
|
|
.. versionchanged:: 0.3.6 |
89
|
|
|
Encapsulated in class |
90
|
|
|
|
91
|
|
|
""" |
92
|
|
|
phonetic = '' |
93
|
|
|
for word in phrase.split(): |
94
|
|
|
if isinstance(self._phonetic_algorithm, _Phonetic): |
95
|
|
|
word = self._phonetic_algorithm.encode(word) |
96
|
|
|
else: |
97
|
|
|
word = self._phonetic_algorithm(word) |
98
|
|
|
if not isinstance(word, str) and hasattr(word, '__iter__'): |
99
|
|
|
word = word[0] |
100
|
|
|
phonetic += word + self._joiner |
101
|
|
|
phonetic = phonetic[: -len(self._joiner)] |
102
|
|
|
return super(Phonetic, self).fingerprint(phonetic) |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
if __name__ == '__main__': |
106
|
1 |
|
import doctest |
107
|
1 |
|
|
108
|
|
|
doctest.testmod() |
109
|
|
|
|