Completed
Pull Request — master (#149)
by Chris
11:34
created

abydos.distance._suffix.Suffix.sim()   B

Complexity

Conditions 7

Size

Total Lines 42
Code Lines 11

Duplication

Lines 36
Ratio 85.71 %

Code Coverage

Tests 11
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 11
nop 3
dl 36
loc 42
ccs 11
cts 11
cp 1
crap 7
rs 8
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
3
# Copyright 2014-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._suffix.
20
21
Suffix similarity & distance
22
"""
23
24 1
from __future__ import (
25
    absolute_import,
26
    division,
27
    print_function,
28
    unicode_literals,
29
)
30
31 1
from six.moves import range
32
33 1
from ._distance import _Distance
34
35 1
__all__ = ['Suffix', 'dist_suffix', 'sim_suffix']
36
37
38 1 View Code Duplication
class Suffix(_Distance):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
39
    """Suffix similarity and distance."""
40
41 1
    def sim(self, src, tar):
0 ignored issues
show
Bug introduced by
Parameters differ from overridden 'sim' method
Loading history...
42
        """Return the suffix similarity of two strings.
43
44
        Suffix similarity is the ratio of the length of the shorter term that
45
        exactly matches the longer term to the length of the shorter term,
46
        beginning at the end of both terms.
47
48
        Parameters
49
        ----------
50
        src : str
51
            Source string for comparison
52
        tar : str
53
            Target string for comparison
54
55
        Returns
56
        -------
57
        float
58
            Suffix similarity
59
60
        Examples
61
        --------
62
        >>> cmp = Suffix()
63
        >>> cmp.sim('cat', 'hat')
64
        0.6666666666666666
65
        >>> cmp.sim('Niall', 'Neil')
66
        0.25
67
        >>> cmp.sim('aluminum', 'Catalan')
68
        0.0
69
        >>> cmp.sim('ATCG', 'TAGC')
70
        0.0
71
72
        """
73 1
        if src == tar:
74 1
            return 1.0
75 1
        if not src or not tar:
76 1
            return 0.0
77 1
        min_word, max_word = (src, tar) if len(src) < len(tar) else (tar, src)
78 1
        min_len = len(min_word)
79 1
        for i in range(min_len, 0, -1):
80 1
            if min_word[-i:] == max_word[-i:]:
81 1
                return i / min_len
82 1
        return 0.0
83
84
85 1
def sim_suffix(src, tar):
86
    """Return the suffix similarity of two strings.
87
88
    This is a wrapper for :py:meth:`Suffix.sim`.
89
90
    Parameters
91
    ----------
92
    src : str
93
        Source string for comparison
94
    tar : str
95
        Target string for comparison
96
97
    Returns
98
    -------
99
    float
100
        Suffix similarity
101
102
    Examples
103
    --------
104
    >>> sim_suffix('cat', 'hat')
105
    0.6666666666666666
106
    >>> sim_suffix('Niall', 'Neil')
107
    0.25
108
    >>> sim_suffix('aluminum', 'Catalan')
109
    0.0
110
    >>> sim_suffix('ATCG', 'TAGC')
111
    0.0
112
113
    """
114 1
    return Suffix().sim(src, tar)
115
116
117 1
def dist_suffix(src, tar):
118
    """Return the suffix distance between two strings.
119
120
    This is a wrapper for :py:meth:`Suffix.dist`.
121
122
    Parameters
123
    ----------
124
    src : str
125
        Source string for comparison
126
    tar : str
127
        Target string for comparison
128
129
    Returns
130
    -------
131
    float
132
        Suffix distance
133
134
    Examples
135
    --------
136
    >>> dist_suffix('cat', 'hat')
137
    0.33333333333333337
138
    >>> dist_suffix('Niall', 'Neil')
139
    0.75
140
    >>> dist_suffix('aluminum', 'Catalan')
141
    1.0
142
    >>> dist_suffix('ATCG', 'TAGC')
143
    1.0
144
145
    """
146 1
    return Suffix().dist(src, tar)
147
148
149
if __name__ == '__main__':
150
    import doctest
151
152
    doctest.testmod()
153