Completed
Push — master ( 3b0a79...5eb1fa )
by Chris
13:35
created

SPEEDCOPTestCases.test_skeleton_key()   A

Complexity

Conditions 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nop 1
dl 0
loc 16
rs 9.75
c 0
b 0
f 0
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_fingerprint.
20
21
This module contains unit tests for abydos.fingerprint
22
"""
23
24
from __future__ import unicode_literals
25
26
import unittest
27
28
import abydos.phonetic as phonetic
29
from abydos.fingerprint import str_fingerprint, omission_key, \
30
    phonetic_fingerprint, qgram_fingerprint, skeleton_key
31
32
from six.moves import range
33
34
35
NIALL = ('Niall', 'Neal', 'Neil', 'Njall', 'Njáll', 'Nigel', 'Neel', 'Nele',
36
         'Nigelli', 'Nel', 'Kneale', 'Uí Néill', 'O\'Neill', 'MacNeil',
37
         'MacNele', 'Niall Noígíallach')
38
39
40
class FingerprintTestCases(unittest.TestCase):
41
    """Test fingerprint functions.
42
43
    abydos.clustering.str_fingerprint, abydos.clustering.qgram_fingerprint, &
44
    abydos.clustering.phonetic_fingerprint
45
    """
46
47
    _testset = ('À noite, vovô Kowalsky vê o ímã cair no pé do pingüim \
48
queixoso e vovó põe açúcar no chá de tâmaras do jabuti feliz.', )
49
    _anssetw = ('a acucar cair cha de do e feliz ima jabuti kowalsky no noite \
50
o pe pinguim poe queixoso tamaras ve vovo', )
51
    _anssetq2 = ('abacadaialamanarasbucachcudedoeaedeieleoetevfeguhaifiminirit\
52
ixizjakokylilsmamqngnoocoeoiojokoposovowpepipoqurarnsdsksotatetiucueuiutvevowa\
53
xoyv', )
54
    _anssetq1 = ('abcdefghijklmnopqrstuvwxyz', )
55
56
    def test_str_fingerprint(self):
57
        """Test abydos.clustering.str_fingerprint."""
58
        self.assertEqual(str_fingerprint(''), '')
59
        for i in range(len(self._testset)):
60
            self.assertEqual(str_fingerprint(self._testset[i]),
61
                             self._anssetw[i])
62
63
    def test_qgram_fingerprint(self):
64
        """Test abydos.clustering.qgram_fingerprint."""
65
        self.assertEqual(qgram_fingerprint(''), '')
66
        for i in range(len(self._testset)):
67
            self.assertEqual(qgram_fingerprint(self._testset[i], 1),
68
                             self._anssetq1[i])
69
            self.assertEqual(qgram_fingerprint(self._testset[i], 2),
70
                             self._anssetq2[i])
71
            self.assertEqual(qgram_fingerprint(self._testset[i]),
72
                             self._anssetq2[i])
73
74
        qgram_fp_niall = ('aliallni', 'aleane', 'eiilne', 'aljallnj',
75
                          'aljallnj', 'elgeigni', 'eeelne', 'ellene',
76
                          'elgeiglillni', 'elne', 'aleaknlene', 'eiilinllneui',
77
                          'eiilllneon', 'accneiilmane', 'accnellemane',
78
                          'acalchgiiaiglalllnninooi')
79
        for i in range(len(NIALL)):
80
            self.assertEqual(qgram_fingerprint(NIALL[i]), qgram_fp_niall[i])
81
82
    def test_phonetic_fingerprint(self):
83
        """Test abydos.clustering.phonetic_fingerprint."""
84
        self.assertEqual(phonetic_fingerprint(''), '')
85
86
        self.assertEqual(phonetic_fingerprint(' '.join(NIALL)),
87
                         'a anl mknl njl nklk nl')
88
        self.assertEqual(phonetic_fingerprint(' '.join(NIALL),
89
                                              phonetic.phonet),
90
                         'knile makneil maknele neil nel nele nial nigeli ' +
91
                         'nigl nil noigialach oneil ui')
92
        self.assertEqual(phonetic_fingerprint(' '.join(NIALL),
93
                                              phonetic.soundex),
94
                         'k540 m254 n240 n242 n400 o540 u000')
95
96
97
class SPEEDCOPTestCases(unittest.TestCase):
98
    """Test SPEEDCOP functions.
99
100
    abydos.clustering.skeleton_key & abydos.clustering.omission_key
101
    """
102
103
    def test_skeleton_key(self):
104
        """Test abydos.clustering.skeleton_key."""
105
        self.assertEqual(skeleton_key(''), '')
106
107
        # http://dl.acm.org/citation.cfm?id=358048
108
        self.assertEqual(skeleton_key('chemogenic'), 'CHMGNEOI')
109
        self.assertEqual(skeleton_key('chemomagnetic'), 'CHMGNTEOAI')
110
        self.assertEqual(skeleton_key('chemcal'), 'CHMLEA')
111
        self.assertEqual(skeleton_key('chemcial'), 'CHMLEIA')
112
        self.assertEqual(skeleton_key('chemical'), 'CHMLEIA')
113
        self.assertEqual(skeleton_key('chemicial'), 'CHMLEIA')
114
        self.assertEqual(skeleton_key('chimical'), 'CHMLIA')
115
        self.assertEqual(skeleton_key('chemiluminescence'), 'CHMLNSEIU')
116
        self.assertEqual(skeleton_key('chemiluminescent'), 'CHMLNSTEIU')
117
        self.assertEqual(skeleton_key('chemicals'), 'CHMLSEIA')
118
        self.assertEqual(skeleton_key('chemically'), 'CHMLYEIA')
119
120
    def test_omission_key(self):
121
        """Test abydos.clustering.omission_key."""
122
        self.assertEqual(omission_key(''), '')
123
124
        # http://dl.acm.org/citation.cfm?id=358048
125
        self.assertEqual(omission_key('microelectronics'), 'MCLNTSRIOE')
126
        self.assertEqual(omission_key('circumstantial'), 'MCLNTSRIUA')
127
        self.assertEqual(omission_key('luminescent'), 'MCLNTSUIE')
128
        self.assertEqual(omission_key('multinucleate'), 'MCLNTUIEA')
129
        self.assertEqual(omission_key('multinucleon'), 'MCLNTUIEO')
130
        self.assertEqual(omission_key('cumulene'), 'MCLNUE')
131
        self.assertEqual(omission_key('luminance'), 'MCLNUIAE')
132
        self.assertEqual(omission_key('coelomic'), 'MCLOEI')
133
        self.assertEqual(omission_key('molecule'), 'MCLOEU')
134
        self.assertEqual(omission_key('cameral'), 'MCLRAE')
135
        self.assertEqual(omission_key('caramel'), 'MCLRAE')
136
        self.assertEqual(omission_key('maceral'), 'MCLRAE')
137
        self.assertEqual(omission_key('lacrimal'), 'MCLRAI')
138
139
140
if __name__ == '__main__':
141
    unittest.main()
142