Test Failed
Push — master ( 23810f...afe14d )
by Chris
09:47
created

abydos.distance._token_distance   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 79
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A _TokenDistance._get_qgrams() 0 33 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._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):
38
    """Abstract Token Distance class."""
39
40 1
    def _get_qgrams(self, src, tar, qval=0, skip=0):
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