Completed
Pull Request — master (#135)
by Chris
11:32
created

TokenDistance._get_qgrams()   A

Complexity

Conditions 4

Size

Total Lines 23
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 23
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 4
nop 5
crap 4
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):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
32
    """Abstract Distance class."""
33
34 1
    def sim(self, src, tar, *args, **kwargs):
35
        """Return similarity.
36
37
        :param src:
38
        :param tar:
39
        :param args:
40
        :param kwargs:
41
        :return:
42
        """
43 1
        return 1.0 - self.dist(src, tar, *args, **kwargs)
44
45 1
    def dist(self, src, tar, *args, **kwargs):
46
        """Return distance.
47
48
        :param src:
49
        :param tar:
50
        :param args:
51
        :param kwargs:
52
        :return:
53
        """
54 1
        return 1.0 - self.sim(src, tar, *args, **kwargs)
55
56 1
    def dist_abs(self, src, tar, *args, **kwargs):
57
        """Return absolute distance.
58
59
        :param src:
60
        :param tar:
61
        :param args:
62
        :param kwargs:
63
        :return:
64
        """
65
        return self.dist(src, tar, *args, **kwargs)
66
67
68 1
class TokenDistance(Distance):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
69
    """Abstract Token Distance class."""
70
71 1
    def _get_qgrams(self, src, tar, qval=0, skip=0):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
72
        """Return the Q-Grams in src & tar.
73
74
        :param str src: source string (or QGrams/Counter objects) for
75
            comparison
76
        :param str tar: target string (or QGrams/Counter objects) for
77
            comparison
78
        :param int qval: the length of each q-gram; 0 for non-q-gram version
79
        :param int skip: the number of characters to skip (only works when
80
            src and tar are strings
81
        :returns: Q-Grams
82
        :rtype: tuple of Counters
83
84
        >>> pe = TokenDistance()
85
        >>> pe._get_qgrams('AT', 'TT', qval=2)
86
        (QGrams({'$A': 1, 'AT': 1, 'T#': 1}),
87
         QGrams({'$T': 1, 'TT': 1, 'T#': 1}))
88
        """
89 1
        if isinstance(src, Counter) and isinstance(tar, Counter):
90 1
            return src, tar
91 1
        if qval > 0:
92 1
            return QGrams(src, qval, '$#', skip), QGrams(tar, qval, '$#', skip)
93 1
        return Counter(src.strip().split()), Counter(tar.strip().split())
94
95
96
if __name__ == '__main__':
97
    import doctest
98
99
    doctest.testmod()
100