|
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 unicode_literals |
|
25
|
|
|
|
|
26
|
1 |
|
from itertools import groupby |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
1 |
|
class Phonetic(object): |
|
|
|
|
|
|
30
|
|
|
"""Abstract Phonetic class.""" |
|
31
|
|
|
|
|
32
|
1 |
|
_uc_set = set('ABCDEFGHIJKLMNOPQRSTUVWXYZ') |
|
33
|
1 |
|
_lc_set = set('abcdefghijklmnopqrstuvwxyz') |
|
34
|
1 |
|
_uc_v_set = set('AEIOU') |
|
35
|
1 |
|
_lc_v_set = set('aeiou') |
|
36
|
1 |
|
_uc_vy_set = set('AEIOUY') |
|
37
|
1 |
|
_lc_vy_set = set('aeiouy') |
|
38
|
|
|
|
|
39
|
1 |
|
def _delete_consecutive_repeats(self, word): |
|
|
|
|
|
|
40
|
|
|
"""Delete consecutive repeated characters in a word. |
|
41
|
|
|
|
|
42
|
|
|
:param str word: the word to transform |
|
43
|
|
|
:returns: word with consecutive repeating characters collapsed to |
|
44
|
|
|
a single instance |
|
45
|
|
|
:rtype: str |
|
46
|
|
|
|
|
47
|
|
|
>>> pe = Phonetic() |
|
48
|
|
|
>>> pe._delete_consecutive_repeats('REDDEE') |
|
49
|
|
|
'REDE' |
|
50
|
|
|
>>> pe._delete_consecutive_repeats('AEIOU') |
|
51
|
|
|
'AEIOU' |
|
52
|
|
|
>>> pe._delete_consecutive_repeats('AAACCCTTTGGG') |
|
53
|
|
|
'ACTG' |
|
54
|
|
|
""" |
|
55
|
1 |
|
return ''.join(char for char, _ in groupby(word)) |
|
56
|
|
|
|
|
57
|
1 |
|
def encode(self, word): |
|
58
|
|
|
"""Encode phonetically. |
|
59
|
|
|
|
|
60
|
|
|
:param word: word to encode |
|
61
|
|
|
:return: |
|
62
|
|
|
""" |
|
63
|
|
|
pass |
|
64
|
|
|
|
|
65
|
1 |
|
def encode_alpha(self, word): |
|
66
|
|
|
"""Encode phonetically using alphabetic characters. |
|
67
|
|
|
|
|
68
|
|
|
:param word: word to encode |
|
69
|
|
|
:return: |
|
70
|
|
|
""" |
|
71
|
|
|
return self.encode(word) |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
if __name__ == '__main__': |
|
75
|
|
|
import doctest |
|
76
|
|
|
|
|
77
|
|
|
doctest.testmod() |
|
78
|
|
|
|