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

tests.distance.test_distance_synoname.SynonameTestCases.test_synoname()   B

Complexity

Conditions 1

Size

Total Lines 120
Code Lines 109

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 109
nop 1
dl 0
loc 120
rs 7
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
"""abydos.tests.test_distance.synoname.
20
21
This module contains unit tests for abydos.distance.synoname
22
"""
23
24
from __future__ import division, unicode_literals
25
26
import unittest
27
28
from abydos.distance.synoname import _synoname_strip_punct, \
29
    _synoname_word_approximation, synoname
30
31
32
class SynonameTestCases(unittest.TestCase):
33
    """Test Synoname functions.
34
35
    abydos.distance._synoname_strip_punct, _synoname_word_approximation, &
36
    synoname
37
    """
38
39
    def test_synoname_strip_punct(self):
40
        """Test abydos.distance._synoname_strip_punct."""
41
        # Base cases
42
        self.assertEqual(_synoname_strip_punct(''), '')
43
        self.assertEqual(_synoname_strip_punct('abcdefg'), 'abcdefg')
44
        self.assertEqual(_synoname_strip_punct('a\'b-c,d!e:f%g'), 'abcdefg')
45
46
    def test_synoname_word_approximation(self):
47
        """Test abydos.distance._synoname_word_approximation."""
48
        # Base cases
49
        self.assertEqual(_synoname_word_approximation('', ''), 0)
50
51
        self.assertEqual(
52
            _synoname_word_approximation('di Domenico di Bonaventura',
53
                                         'di Tomme di Nuto',
54
                                         'Cosimo', 'Luca'), 0.4)
55
        self.assertEqual(
56
            _synoname_word_approximation('Antonello da Messina',
57
                                         'Messina', '', 'Antonello da',
58
                                         {'gen_conflict': False,
59
                                          'roman_conflict': False,
60
                                          'src_specials':
61
                                              [(35, 'b'), (35, 'c')],
62
                                          'tar_specials':
63
                                              [(35, 'b'), (35, 'c')]}), 0)
64
        self.assertEqual(
65
            _synoname_word_approximation('louis ii', 'louis ii',
66
                                         'sr jean', 'sr  pierre',
67
                                         {'gen_conflict': False,
68
                                          'roman_conflict': False,
69
                                          'src_specials':
70
                                              [(49, 'b'), (68, 'd'),
71
                                               (121, 'b')],
72
                                          'tar_specials':
73
                                              [(49, 'b'), (68, 'd'),
74
                                               (121, 'b')]}), 0)
75
        self.assertEqual(
76
            _synoname_word_approximation('louis ii', 'louis ii',
77
                                         'il giovane', 'sr cadet',
78
                                         {'gen_conflict': False,
79
                                          'roman_conflict': False,
80
                                          'src_specials':
81
                                              [(46, 'a'), (49, 'b'),
82
                                               (52, 'a'), (68, 'd')],
83
                                          'tar_specials':
84
                                              [(8, 'a'), (49, 'b'),
85
                                               (68, 'd'), (121, 'a')]}), 1)
86
        self.assertAlmostEqual(
87
            _synoname_word_approximation('louis ii', 'louis ii',
88
                                         'ste.-geo ste.', 'ste.-jo ste.',
89
                                         {'gen_conflict': False,
90
                                          'roman_conflict': False,
91
                                          'src_specials':
92
                                              [(49, 'b'), (68, 'd'),
93
                                               (127, 'b'), (127, 'X')],
94
                                          'tar_specials':
95
                                              [(49, 'b'), (68, 'd'),
96
                                               (127, 'b'), (127, 'X')]}), 2/3)
97
        self.assertAlmostEqual(
98
            _synoname_word_approximation('louis ii', 'louis',
99
                                         'ste.-geo ste.', '',
100
                                         {'gen_conflict': False,
101
                                          'roman_conflict': False,
102
                                          'src_specials':
103
                                              [(49, 'b'), (68, 'd'),
104
                                               (127, 'b'), (127, 'X')],
105
                                          'tar_specials': []}), 0)
106
        self.assertAlmostEqual(
107
            _synoname_word_approximation('lou ii', 'louis', 'louis iv', 'ste.',
108
                                         {}), 0)
109
        self.assertEqual(
110
            _synoname_word_approximation('ren', 'loren ste.', '', '',
111
                                         {'tar_specials': [(68, 'd'),
112
                                                           (127, 'X')],
113
                                          'src_specials': [(0, '')]}), 1)
114
115
    def test_synoname(self):
116
        """Test abydos.distance.synoname."""
117
        # Base cases
118
        self.assertEqual(synoname('', ''), 1)
119
        self.assertEqual(synoname('', '', tests=['exact']), 1)
120
        self.assertEqual(synoname('', '', tests=[]), 13)
121
        self.assertEqual(synoname('', '', tests=['nonsense-test']), 13)
122
        self.assertEqual(synoname('', '', ret_name=True), 'exact')
123
124
        # Test input formats
125
        self.assertEqual(synoname(('Brueghel II (the Younger)', 'Pieter',
126
                                   'Workshop of'),
127
                                  ('Brueghel II (the Younger)', 'Pieter',
128
                                   'Workshop of')), 1)
129
        self.assertEqual(synoname('Brueghel II (the Younger)#Pieter#' +
130
                                  'Workshop of',
131
                                  'Brueghel II (the Younger)#Pieter#' +
132
                                  'Workshop of'), 1)
133
        self.assertEqual(synoname('22#Brueghel II (the Younger)#Pieter#' +
134
                                  'Workshop of',
135
                                  '44#Brueghel II (the Younger)#Pieter#' +
136
                                  'Workshop of'), 1)
137
138
        # approx_c tests
139
        self.assertEqual(synoname(('Master of Brueghel II (the Younger)',
140
                                   'Pieter', 'Workshop of'),
141
                                  ('Brueghel I (the Elder)', 'Pieter',
142
                                   'Workshop of')), 13)
143
        self.assertEqual(synoname(('Master of Brueghel II',
144
                                   'Pieter', 'Workshop of'),
145
                                  ('Master known as the Brueghel II', 'Pieter',
146
                                   'Workshop of')), 10)
147
148
        # Types 1-12
149
        self.assertEqual(synoname(('Brueghel', 'Pieter', ''),
150
                                  ('Brueghel', 'Pieter', ''),
151
                                  ret_name=True), 'exact')
152
153
        self.assertEqual(synoname(('Brueghel II', 'Pieter', ''),
154
                                  ('Brueghel I', 'Pieter', ''),
155
                                  ret_name=True), 'no_match')
156
        self.assertEqual(synoname(('Breghel', 'Pieter', ''),
157
                                  ('Brueghel', 'Pieter', ''),
158
                                  ret_name=True), 'omission')
159
        self.assertEqual(synoname(('Brueghel', 'Pieter', ''),
160
                                  ('Breghel', 'Pieter', ''),
161
                                  ret_name=True), 'omission')
162
        self.assertEqual(synoname(('Brueghel', 'Piter', ''),
163
                                  ('Brueghel', 'Pieter', ''),
164
                                  ret_name=True), 'omission')
165
        self.assertEqual(synoname(('Brueghel', 'Pieter', ''),
166
                                  ('Brueghel', 'Piter', ''),
167
                                  ret_name=True), 'omission')
168
        self.assertEqual(synoname(('Brughel', 'Pieter', ''),
169
                                  ('Breghel', 'Pieter', ''),
170
                                  ret_name=True), 'substitution')
171
        self.assertEqual(synoname(('Breughel', 'Peter', ''),
172
                                  ('Breughel', 'Piter', ''),
173
                                  ret_name=True), 'substitution')
174
        self.assertEqual(synoname(('Brueghel', 'Pieter', ''),
175
                                  ('Breughel', 'Pieter', ''),
176
                                  ret_name=True), 'transposition')
177
        self.assertEqual(synoname(('Brueghel', 'Peiter', ''),
178
                                  ('Brueghel', 'Pieter', ''),
179
                                  ret_name=True), 'transposition')
180
181
        self.assertEqual(synoname(('Brueghel:', 'Pieter', ''),
182
                                  ('Brueghel', 'Pi-eter', ''),
183
                                  ret_name=True), 'punctuation')
184
        self.assertEqual(synoname(('Brueghel,', 'Pieter', ''),
185
                                  ('Brueghel', 'Pieter...', ''),
186
                                  ret_name=True), 'punctuation')
187
        self.assertEqual(synoname(('Seu rat', 'George Pierre', ''),
188
                                  ('Seu-rat', 'George-Pierre', ''),
189
                                  ret_name=True), 'punctuation')
190
        self.assertEqual(synoname(('Picasso', '', ''),
191
                                  ('Picasso', 'Pablo', ''),
192
                                  ret_name=True), 'no_first')
193
        self.assertEqual(synoname(('Pereira', 'I. R.', ''),
194
                                  ('Pereira', 'Irene Rice', ''),
195
                                  ret_name=True), 'initials')
196
        self.assertEqual(synoname(('Pereira', 'I.', ''),
197
                                  ('Pereira', 'Irene Rice', ''),
198
                                  ret_name=True), 'initials')
199
        self.assertNotEqual(synoname(('Pereira', 'I. R.', ''),
200
                                     ('Pereira', 'I. Smith', ''),
201
                                     ret_name=True), 'initials')
202
        self.assertNotEqual(synoname(('Pereira', 'I. R. S.', ''),
203
                                     ('Pereira', 'I. S. R.', ''),
204
                                     ret_name=True), 'initials')
205
        self.assertEqual(synoname(('de Goya', 'Francisco', ''),
206
                                  ('de Goya y Lucientes', 'Francisco', ''),
207
                                  ret_name=True), 'extension')
208
        self.assertEqual(synoname(('Seurat', 'George', ''),
209
                                  ('Seurat', 'George-Pierre', ''),
210
                                  ret_name=True), 'extension')
211
        self.assertEqual(synoname(('Gericault', 'Theodore', ''),
212
                                  ('Gericault', 'Jean Louis Andre Theodore',
213
                                   ''),
214
                                  ret_name=True), 'inclusion')
215
        self.assertEqual(synoname(('Dore', 'Gustave', ''),
216
                                  ('Dore', 'Paul Gustave Louis Christophe',
217
                                   ''),
218
                                  ret_name=True), 'inclusion')
219
220
        self.assertEqual(synoname(('Rosetti', 'Dante Gabriel', ''),
221
                                  ('Rosetti', 'Gabriel Charles Dante', ''),
222
                                  ret_name=True), 'word_approx')
223
        self.assertEqual(synoname(('di Domenico di Bonaventura', 'Cosimo', ''),
224
                                  ('di Tomme di Nuto', 'Luca', ''),
225
                                  ret_name=True), 'no_match')
226
        self.assertEqual(synoname(('Pereira', 'I. R.', ''),
227
                                  ('Pereira', 'I. Smith', ''),
228
                                  ret_name=True), 'word_approx')
229
        self.assertEqual(synoname(('Antonello da Messina', '', ''),
230
                                  ('Messina', 'Antonello da', ''),
231
                                  ret_name=True), 'confusions')
232
        self.assertEqual(synoname(('Brueghel', 'Pietter', ''),
233
                                  ('Bruegghel', 'Pieter', ''),
234
                                  ret_name=True), 'char_approx')
235
236
237
if __name__ == '__main__':
238
    unittest.main()
239