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._phonetic. |
20
|
|
|
|
21
|
|
|
phonetic fingerprint |
22
|
|
|
""" |
23
|
|
|
|
24
|
1 |
|
from __future__ import ( |
25
|
|
|
absolute_import, |
26
|
|
|
division, |
27
|
|
|
print_function, |
28
|
|
|
unicode_literals, |
29
|
|
|
) |
30
|
|
|
|
31
|
1 |
|
from six import text_type |
32
|
|
|
|
33
|
1 |
|
from ._string import String |
34
|
1 |
|
from ..phonetic import double_metaphone |
35
|
|
|
|
36
|
1 |
|
__all__ = ['Phonetic', 'phonetic_fingerprint'] |
37
|
|
|
|
38
|
|
|
|
39
|
1 |
|
class Phonetic(String): |
|
|
|
|
40
|
|
|
"""Phonetic Fingerprint. |
41
|
|
|
|
42
|
|
|
A phonetic fingerprint is identical to a standard string fingerprint, as |
43
|
|
|
implemented in abydos.clustering.fingerprint(), but performs the |
44
|
|
|
fingerprinting function after converting the string to its phonetic form, |
45
|
|
|
as determined by some phonetic algorithm. This fingerprint is described at |
46
|
|
|
:cite:`OpenRefine:2012`. |
47
|
|
|
""" |
48
|
|
|
|
49
|
1 |
|
def fingerprint( |
|
|
|
|
50
|
|
|
self, |
|
|
|
|
51
|
|
|
phrase, |
|
|
|
|
52
|
|
|
phonetic_algorithm=double_metaphone, |
|
|
|
|
53
|
|
|
joiner=' ', |
|
|
|
|
54
|
|
|
*args, |
|
|
|
|
55
|
|
|
**kwargs |
|
|
|
|
56
|
|
|
): |
57
|
|
|
"""Return the phonetic fingerprint of a phrase. |
58
|
|
|
|
59
|
|
|
Parameters |
60
|
|
|
---------- |
61
|
|
|
phrase : str |
62
|
|
|
The string from which to calculate the phonetic fingerprint |
63
|
|
|
phonetic_algorithm : function |
64
|
|
|
A phonetic algorithm that takes a string and returns a string |
65
|
|
|
(presumably a phonetic representation of the original string). By |
66
|
|
|
default, this function uses |
67
|
|
|
:py:func:`abydos.phonetic.double_metaphone` |
68
|
|
|
joiner : str |
69
|
|
|
The string that will be placed between each word |
70
|
|
|
*args |
71
|
|
|
Variable length argument list |
72
|
|
|
**kwargs |
73
|
|
|
Arbitrary keyword arguments |
74
|
|
|
|
75
|
|
|
Returns |
76
|
|
|
------- |
77
|
|
|
str |
78
|
|
|
The phonetic fingerprint of the phrase |
79
|
|
|
|
80
|
|
|
Examples |
81
|
|
|
-------- |
82
|
|
|
>>> pf = Phonetic() |
83
|
|
|
>>> pf.fingerprint('The quick brown fox jumped over the lazy dog.') |
84
|
|
|
'0 afr fks jmpt kk ls prn tk' |
85
|
|
|
>>> from abydos.phonetic import soundex |
86
|
|
|
>>> pf.fingerprint('The quick brown fox jumped over the lazy dog.', |
87
|
|
|
... phonetic_algorithm=soundex) |
88
|
|
|
'b650 d200 f200 j513 l200 o160 q200 t000' |
89
|
|
|
|
90
|
|
|
""" |
91
|
1 |
|
phonetic = '' |
92
|
1 |
|
for word in phrase.split(): |
93
|
1 |
|
word = phonetic_algorithm(word, *args, **kwargs) |
94
|
1 |
|
if not isinstance(word, text_type) and hasattr(word, '__iter__'): |
95
|
1 |
|
word = word[0] |
96
|
1 |
|
phonetic += word + joiner |
97
|
1 |
|
phonetic = phonetic[: -len(joiner)] |
98
|
1 |
|
return super(self.__class__, self).fingerprint(phonetic) |
|
|
|
|
99
|
|
|
|
100
|
|
|
|
101
|
1 |
|
def phonetic_fingerprint( |
102
|
|
|
phrase, phonetic_algorithm=double_metaphone, joiner=' ', *args, **kwargs |
|
|
|
|
103
|
|
|
): |
104
|
|
|
"""Return the phonetic fingerprint of a phrase. |
105
|
|
|
|
106
|
|
|
This is a wrapper for :py:meth:`PhoneticFingerprint.fingerprint`. |
107
|
|
|
|
108
|
|
|
Parameters |
109
|
|
|
---------- |
110
|
|
|
phrase : str |
111
|
|
|
The string from which to calculate the phonetic fingerprint |
112
|
|
|
phonetic_algorithm : function |
113
|
|
|
A phonetic algorithm that takes a string and returns a string |
114
|
|
|
(presumably a phonetic representation of the original string). By |
115
|
|
|
default, this function uses |
116
|
|
|
:py:func:`abydos.phonetic.double_metaphone`. |
117
|
|
|
joiner : str |
118
|
|
|
The string that will be placed between each word |
119
|
|
|
*args |
120
|
|
|
Variable length argument list |
121
|
|
|
**kwargs |
122
|
|
|
Arbitrary keyword arguments |
123
|
|
|
|
124
|
|
|
Returns |
125
|
|
|
------- |
126
|
|
|
str |
127
|
|
|
The phonetic fingerprint of the phrase |
128
|
|
|
|
129
|
|
|
Examples |
130
|
|
|
-------- |
131
|
|
|
>>> phonetic_fingerprint('The quick brown fox jumped over the lazy dog.') |
132
|
|
|
'0 afr fks jmpt kk ls prn tk' |
133
|
|
|
>>> from abydos.phonetic import soundex |
134
|
|
|
>>> phonetic_fingerprint('The quick brown fox jumped over the lazy dog.', |
135
|
|
|
... phonetic_algorithm=soundex) |
136
|
|
|
'b650 d200 f200 j513 l200 o160 q200 t000' |
137
|
|
|
|
138
|
|
|
""" |
139
|
1 |
|
return Phonetic().fingerprint( |
140
|
|
|
phrase, phonetic_algorithm, joiner, *args, **kwargs |
141
|
|
|
) |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
if __name__ == '__main__': |
145
|
|
|
import doctest |
146
|
|
|
|
147
|
|
|
doctest.testmod() |
148
|
|
|
|