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 unicode_literals |
25
|
|
|
|
26
|
1 |
|
from collections import Counter |
27
|
|
|
|
28
|
1 |
|
from ..tokenizer import QGrams |
29
|
|
|
|
30
|
|
|
|
31
|
1 |
|
class Distance(object): |
|
|
|
|
32
|
|
|
"""Abstract Distance class.""" |
33
|
|
|
|
34
|
1 |
|
def sim(self, src, tar, *args, **kwargs): |
35
|
|
|
"""Return similarity. |
36
|
|
|
|
37
|
|
|
Args: |
38
|
|
|
src (str): Source string for comparison |
39
|
|
|
tar (str): Target string for comparison |
40
|
|
|
*args: Variable length argument list. |
41
|
|
|
**kwargs: Arbitrary keyword arguments. |
42
|
|
|
|
43
|
|
|
Returns: |
44
|
|
|
float: Similarity |
45
|
|
|
|
46
|
|
|
""" |
47
|
1 |
|
return 1.0 - self.dist(src, tar, *args, **kwargs) |
48
|
|
|
|
49
|
1 |
|
def dist(self, src, tar, *args, **kwargs): |
50
|
|
|
"""Return distance. |
51
|
|
|
|
52
|
|
|
Args: |
53
|
|
|
src (str): Source string for comparison |
54
|
|
|
tar (str): Target string for comparison |
55
|
|
|
*args: Variable length argument list. |
56
|
|
|
**kwargs: Arbitrary keyword arguments. |
57
|
|
|
|
58
|
|
|
Returns: |
59
|
|
|
float: Distance |
60
|
|
|
|
61
|
|
|
""" |
62
|
1 |
|
return 1.0 - self.sim(src, tar, *args, **kwargs) |
63
|
|
|
|
64
|
1 |
|
def dist_abs(self, src, tar, *args, **kwargs): |
65
|
|
|
"""Return absolute distance. |
66
|
|
|
|
67
|
|
|
Args: |
68
|
|
|
src (str): Source string for comparison |
69
|
|
|
tar (str): Target string for comparison |
70
|
|
|
*args: Variable length argument list. |
71
|
|
|
**kwargs: Arbitrary keyword arguments. |
72
|
|
|
|
73
|
|
|
Returns: |
74
|
|
|
int: Absolute distance |
75
|
|
|
|
76
|
|
|
""" |
77
|
|
|
return self.dist(src, tar, *args, **kwargs) |
78
|
|
|
|
79
|
|
|
|
80
|
1 |
|
class TokenDistance(Distance): |
|
|
|
|
81
|
|
|
"""Abstract Token Distance class.""" |
82
|
|
|
|
83
|
1 |
|
def _get_qgrams(self, src, tar, qval=0, skip=0): |
|
|
|
|
84
|
|
|
"""Return the Q-Grams in src & tar. |
85
|
|
|
|
86
|
|
|
Args: |
87
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
88
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
89
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
90
|
|
|
skip (int): the number of characters to skip (only works when src |
91
|
|
|
and tar are strings |
92
|
|
|
|
93
|
|
|
Returns: |
94
|
|
|
tuple of Counters: Q-Grams |
95
|
|
|
|
96
|
|
|
Examples: |
97
|
|
|
>>> pe = TokenDistance() |
98
|
|
|
>>> pe._get_qgrams('AT', 'TT', qval=2) |
99
|
|
|
(QGrams({'$A': 1, 'AT': 1, 'T#': 1}), |
100
|
|
|
QGrams({'$T': 1, 'TT': 1, 'T#': 1})) |
101
|
|
|
|
102
|
|
|
""" |
103
|
1 |
|
if isinstance(src, Counter) and isinstance(tar, Counter): |
104
|
1 |
|
return src, tar |
105
|
1 |
|
if qval > 0: |
106
|
1 |
|
return QGrams(src, qval, '$#', skip), QGrams(tar, qval, '$#', skip) |
107
|
1 |
|
return Counter(src.strip().split()), Counter(tar.strip().split()) |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
if __name__ == '__main__': |
111
|
|
|
import doctest |
112
|
|
|
|
113
|
|
|
doctest.testmod() |
114
|
|
|
|