Completed
Push — master ( f43547...71985b )
by Chris
12:00 queued 10s
created

_TokenDistance._get_qgrams()   A

Complexity

Conditions 4

Size

Total Lines 33
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nop 5
dl 0
loc 33
ccs 6
cts 6
cp 1
crap 4
rs 10
c 0
b 0
f 0
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._token_distance.
20
21
The distance._TokenDistance module implements abstract class _TokenDistance.
22
"""
23
24 1
from __future__ import (
25
    absolute_import,
26
    division,
27
    print_function,
28
    unicode_literals,
29
)
30
31 1
from collections import Counter
32
33 1
from ._distance import _Distance
34 1
from ..tokenizer import QGrams
35
36
37 1
class _TokenDistance(_Distance):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
38
    """Abstract Token Distance class."""
39
40 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...
41
        """Return the Q-Grams in src & tar.
42
43
        Parameters
44
        ----------
45
        src : str
46
            Source string (or QGrams/Counter objects) for comparison
47
        tar : str
48
            Target string (or QGrams/Counter objects) for comparison
49
        qval : int
50
            The length of each q-gram; 0 for non-q-gram version
51
        skip : int
52
            The number of characters to skip (only works when src and tar are
53
            strings)
54
55
        Returns
56
        -------
57
        tuple of Counters
58
            Q-Grams
59
60
        Examples
61
        --------
62
        >>> pe = _TokenDistance()
63
        >>> pe._get_qgrams('AT', 'TT', qval=2)
64
        (QGrams({'$A': 1, 'AT': 1, 'T#': 1}),
65
         QGrams({'$T': 1, 'TT': 1, 'T#': 1}))
66
67
        """
68 1
        if isinstance(src, Counter) and isinstance(tar, Counter):
69 1
            return src, tar
70 1
        if qval > 0:
71 1
            return QGrams(src, qval, '$#', skip), QGrams(tar, qval, '$#', skip)
72 1
        return Counter(src.strip().split()), Counter(tar.strip().split())
73
74
75
if __name__ == '__main__':
76
    import doctest
77
78
    doctest.testmod()
79