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._phonetic. |
20
|
|
|
|
21
|
|
|
The phonetic._phonetic module implements abstract class Phonetic. |
22
|
|
|
""" |
23
|
|
|
|
24
|
1 |
|
from __future__ import ( |
25
|
|
|
absolute_import, |
26
|
|
|
division, |
27
|
|
|
print_function, |
28
|
|
|
unicode_literals, |
29
|
|
|
) |
30
|
|
|
|
31
|
1 |
|
from itertools import groupby |
32
|
|
|
|
33
|
|
|
|
34
|
1 |
|
class Phonetic(object): |
|
|
|
|
35
|
|
|
"""Abstract Phonetic class.""" |
36
|
|
|
|
37
|
1 |
|
_uc_set = set('ABCDEFGHIJKLMNOPQRSTUVWXYZ') |
38
|
1 |
|
_lc_set = set('abcdefghijklmnopqrstuvwxyz') |
39
|
1 |
|
_uc_v_set = set('AEIOU') |
40
|
1 |
|
_lc_v_set = set('aeiou') |
41
|
1 |
|
_uc_vy_set = set('AEIOUY') |
42
|
1 |
|
_lc_vy_set = set('aeiouy') |
43
|
|
|
|
44
|
1 |
|
def _delete_consecutive_repeats(self, word): |
|
|
|
|
45
|
|
|
"""Delete consecutive repeated characters in a word. |
46
|
|
|
|
47
|
|
|
Parameters |
48
|
|
|
---------- |
49
|
|
|
word : str |
50
|
|
|
The word to transform |
51
|
|
|
|
52
|
|
|
Returns |
53
|
|
|
------- |
54
|
|
|
str |
55
|
|
|
Word with consecutive repeating characters collapsed to a single |
56
|
|
|
instance |
57
|
|
|
|
58
|
|
|
Examples |
59
|
|
|
-------- |
60
|
|
|
>>> pe = Phonetic() |
61
|
|
|
>>> pe._delete_consecutive_repeats('REDDEE') |
62
|
|
|
'REDE' |
63
|
|
|
>>> pe._delete_consecutive_repeats('AEIOU') |
64
|
|
|
'AEIOU' |
65
|
|
|
>>> pe._delete_consecutive_repeats('AAACCCTTTGGG') |
66
|
|
|
'ACTG' |
67
|
|
|
|
68
|
|
|
""" |
69
|
1 |
|
return ''.join(char for char, _ in groupby(word)) |
70
|
|
|
|
71
|
1 |
|
def encode(self, word): |
72
|
|
|
"""Encode phonetically. |
73
|
|
|
|
74
|
|
|
Parameters |
75
|
|
|
---------- |
76
|
|
|
word : str |
77
|
|
|
The word to transform |
78
|
|
|
|
79
|
|
|
""" |
80
|
|
|
pass |
81
|
|
|
|
82
|
1 |
|
def encode_alpha(self, word): |
83
|
|
|
"""Encode phonetically using alphabetic characters. |
84
|
|
|
|
85
|
|
|
Parameters |
86
|
|
|
---------- |
87
|
|
|
word : str |
88
|
|
|
The word to transform |
89
|
|
|
|
90
|
|
|
Returns |
91
|
|
|
------- |
92
|
|
|
str |
93
|
|
|
The word transformed |
94
|
|
|
|
95
|
|
|
""" |
96
|
|
|
return self.encode(word) |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
if __name__ == '__main__': |
100
|
|
|
import doctest |
101
|
|
|
|
102
|
|
|
doctest.testmod() |
103
|
|
|
|