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.distance._distance. |
20
|
|
|
|
21
|
|
|
The distance._distance module implements abstract class _Distance. |
22
|
|
|
""" |
23
|
|
|
|
24
|
1 |
|
from __future__ import ( |
25
|
|
|
unicode_literals, |
26
|
|
|
absolute_import, |
27
|
|
|
division, |
28
|
|
|
print_function, |
29
|
|
|
) |
30
|
|
|
|
31
|
|
|
|
32
|
1 |
|
class _Distance(object): |
|
|
|
|
33
|
|
|
"""Abstract Distance class.""" |
34
|
|
|
|
35
|
1 |
|
def sim(self, src, tar, *args, **kwargs): |
36
|
|
|
"""Return similarity. |
37
|
|
|
|
38
|
|
|
Args: |
39
|
|
|
src (str): Source string for comparison |
40
|
|
|
tar (str): Target string for comparison |
41
|
|
|
*args: Variable length argument list. |
42
|
|
|
**kwargs: Arbitrary keyword arguments. |
43
|
|
|
|
44
|
|
|
Returns: |
45
|
|
|
float: Similarity |
46
|
|
|
|
47
|
|
|
""" |
48
|
1 |
|
return 1.0 - self.dist(src, tar, *args, **kwargs) |
49
|
|
|
|
50
|
1 |
|
def dist(self, src, tar, *args, **kwargs): |
51
|
|
|
"""Return distance. |
52
|
|
|
|
53
|
|
|
Args: |
54
|
|
|
src (str): Source string for comparison |
55
|
|
|
tar (str): Target string for comparison |
56
|
|
|
*args: Variable length argument list. |
57
|
|
|
**kwargs: Arbitrary keyword arguments. |
58
|
|
|
|
59
|
|
|
Returns: |
60
|
|
|
float: Distance |
61
|
|
|
|
62
|
|
|
""" |
63
|
1 |
|
return 1.0 - self.sim(src, tar, *args, **kwargs) |
64
|
|
|
|
65
|
1 |
|
def dist_abs(self, src, tar, *args, **kwargs): |
66
|
|
|
"""Return absolute distance. |
67
|
|
|
|
68
|
|
|
Args: |
69
|
|
|
src (str): Source string for comparison |
70
|
|
|
tar (str): Target string for comparison |
71
|
|
|
*args: Variable length argument list. |
72
|
|
|
**kwargs: Arbitrary keyword arguments. |
73
|
|
|
|
74
|
|
|
Returns: |
75
|
|
|
int: Absolute distance |
76
|
|
|
|
77
|
|
|
""" |
78
|
|
|
return self.dist(src, tar, *args, **kwargs) |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
if __name__ == '__main__': |
82
|
|
|
import doctest |
83
|
|
|
|
84
|
|
|
doctest.testmod() |
85
|
|
|
|