Completed
Branch master (87ccc1)
by Chris
10:18
created

tests.test_phones   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 122
dl 0
loc 182
rs 10
c 0
b 0
f 0
wmc 3
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
"""abydos.tests.test_phones.
20
21
This module contains unit tests for abydos.phones
22
"""
23
24
from __future__ import unicode_literals
25
26
import unittest
27
from math import isnan
28
29
from abydos.phones import cmp_features, get_feature, ipa_to_features
30
31
32
class IpaFeaturesTestCases(unittest.TestCase):
33
    """Test abydos.phones.ipa_to_features."""
34
35
    def test_ipa_to_features(self):
36
        """Test abydos.phones.ipa_to_features."""
37
        self.assertEqual(ipa_to_features('medçen'),
38
                         [2709662981243185770,
39
                          1826957430176000426,
40
                          2783230754501864106,
41
                          2783233463150094762,
42
                          1826957430176000426,
43
                          2711173160463936106])
44
        self.assertEqual(ipa_to_features('axtuŋ'),
45
                         [1826957425952336298,
46
                          2783233462881659306,
47
                          2783230754502126250,
48
                          1825831513894594986,
49
                          2711175868843469418])
50
        self.assertEqual(ipa_to_features('iç'),
51
                         [1826957412996131242,
52
                          2783233463150094762])
53
        self.assertEqual(ipa_to_features('bakofen'),
54
                         [2781720575281113770,
55
                          1826957425952336298,
56
                          2783233462881659562,
57
                          1825831531074464170,
58
                          2781702983095331242,
59
                          1826957430176000426,
60
                          2711173160463936106])
61
        self.assertEqual(ipa_to_features('dʒuŋel'),
62
                         [2783230754501864106,
63
                          2783231556184353178,
64
                          1825831513894594986,
65
                          2711175868843469418,
66
                          1826957430176000426,
67
                          2693158761954453926])
68
        self.assertEqual(ipa_to_features('kvatʃ'),
69
                         [2783233462881659562,
70
                          2781702983095069098,
71
                          1826957425952336298,
72
                          2783230754502126250,
73
                          2783231556184615322])
74
        self.assertEqual(ipa_to_features('nitʃe'),
75
                         [2711173160463936106,
76
                          1826957412996131242,
77
                          2783230754502126250,
78
                          2783231556184615322,
79
                          1826957430176000426])
80
        self.assertEqual(ipa_to_features('klø'),
81
                         [2783233462881659562,
82
                          2693158761954453926,
83
                          1825831530269157802])
84
        self.assertEqual(ipa_to_features('kybax'),
85
                         [2783233462881659562,
86
                          1825831513089288618,
87
                          2781720575281113770,
88
                          1826957425952336298,
89
                          2783233462881659306])
90
        self.assertEqual(ipa_to_features('i@c'),
91
                         [1826957412996131242,
92
                          -1,
93
                          2783233463150095018])
94
95
96
class HasFeatureTestCases(unittest.TestCase):
97
    """Test abydos.phones.get_feature."""
98
99
    def test_ipa_to_features(self):
100
        """Test abydos.phones.get_feature."""
101
        self.assertEqual(get_feature(ipa_to_features('medçen'), 'nasal'),
102
                         [1, -1, -1, -1, -1, 1])
103
        self.assertRaises(AttributeError, get_feature,
104
                          ipa_to_features('medçen'), 'vocalic')
105
106
        self.assertEqual(get_feature(ipa_to_features('nitʃe'), 'nasal'),
107
                         [1, -1, -1, -1, -1])
108
        self.assertEqual(get_feature(ipa_to_features('nitʃe'), 'strident'),
109
                         [-1, -1, -1, 1, -1])
110
        self.assertEqual(get_feature(ipa_to_features('nitʃe'), 'syllabic'),
111
                         [-1, 1, -1, -1, 1])
112
        self.assertEqual(get_feature(ipa_to_features('nitʃe'), 'continuant'),
113
                         [-1, 1, -1, 1, 1])
114
115
        self.assertEqual(get_feature(ipa_to_features('nit͡ʃe'), 'nasal'),
116
                         [1, -1, -1, -1])
117
        self.assertEqual(get_feature(ipa_to_features('nit͡ʃe'), 'strident'),
118
                         [-1, -1, 1, -1])
119
        self.assertEqual(get_feature(ipa_to_features('nit͡ʃe'), 'syllabic'),
120
                         [-1, 1, -1, 1])
121
        self.assertEqual(get_feature(ipa_to_features('nit͡ʃe'), 'continuant'),
122
                         [-1, 1, 2, 1])
123
124
        self.assertEqual(get_feature(ipa_to_features('løvenbroy'), 'atr'),
125
                         [0, 1, 0, 1, 0, 0, 0, 1, 1])
126
        self.assertNotEqual(get_feature(ipa_to_features('i@c'), 'syllabic'),
127
                            [1, float('NaN'), -1])
128
        self.assertTrue(isnan(get_feature(ipa_to_features('i@c'),
129
                                          'syllabic')[1]))
130
131
132
class CmpFeaturesTestCases(unittest.TestCase):
133
    """Test cases for abydos.phones.cmp_features."""
134
135
    def test_cmp_features(self):
136
        """Test abydos.phones.cmp_features."""
137
        # # negatives
138
        self.assertEqual(cmp_features(-1, 1826957425952336298), -1)
139
        self.assertEqual(cmp_features(1826957425952336298, -1), -1)
140
        self.assertEqual(cmp_features(-1, -1), -1)
141
        # # equals
142
        self.assertEqual(cmp_features(0, 0), 1)
143
        self.assertEqual(cmp_features(1826957425952336298,
144
                                      1826957425952336298), 1)
145
146
        # # unequals
147
        # pre-calc everything
148
        cced = ipa_to_features('ç')[0]
149
        esh = ipa_to_features('ʃ')[0]
150
        tesh = ipa_to_features('t͡ʃ')[0]
151
152
        cmp_cced_tesh = cmp_features(cced, tesh)
153
        cmp_cced_esh = cmp_features(cced, esh)
154
        cmp_esh_tesh = cmp_features(esh, tesh)
155
156
        cmp_tesh_cced = cmp_features(tesh, cced)
157
        cmp_esh_cced = cmp_features(esh, cced)
158
        cmp_tesh_esh = cmp_features(tesh, esh)
159
160
        # check symmetric equality
161
        self.assertEqual(cmp_cced_tesh, cmp_tesh_cced)
162
        self.assertEqual(cmp_cced_esh, cmp_esh_cced)
163
        self.assertEqual(cmp_esh_tesh, cmp_tesh_esh)
164
165
        # check that they're all greater than 0
166
        self.assertGreater(cmp_cced_tesh, 0)
167
        self.assertGreater(cmp_cced_esh, 0)
168
        self.assertGreater(cmp_esh_tesh, 0)
169
170
        # check that they're all less than 1
171
        self.assertLess(cmp_cced_tesh, 1)
172
        self.assertLess(cmp_cced_esh, 1)
173
        self.assertLess(cmp_esh_tesh, 1)
174
175
        # ʃ and t͡ʃ should be more similar than either of these and ç
176
        self.assertGreater(cmp_esh_tesh, cmp_cced_tesh)
177
        self.assertGreater(cmp_esh_tesh, cmp_cced_esh)
178
179
180
if __name__ == '__main__':
181
    unittest.main()
182