Completed
Push — master ( 643512...2b6b3e )
by Chris
20:40 queued 10:36
created

abydos.distance._chao_dice   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 23
dl 0
loc 140
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A ChaoDice.sim_score() 0 41 2
A ChaoDice.__init__() 0 13 1
A ChaoDice.sim() 0 34 1
1
# -*- coding: utf-8 -*-
2
3
# Copyright 2019 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._chao_dice.
20
21
Chao's Dice similarity
22
"""
23
24 1
from __future__ import (
25
    absolute_import,
26
    division,
27
    print_function,
28
    unicode_literals,
29
)
30
31 1
from ._chao_jaccard import ChaoJaccard
32
33 1
__all__ = ['ChaoDice']
34
35
36 1
class ChaoDice(ChaoJaccard):
37
    r"""Chao's Dice similarity.
38
39
    Chao's Dice similarity :cite:`Chao:2004`
40
41
    .. versionadded:: 0.4.1
42
    """
43
44 1
    def __init__(self, **kwargs):
45
        """Initialize ChaoDice instance.
46
47
        Parameters
48
        ----------
49
        **kwargs
50
            Arbitrary keyword arguments
51
52
53
        .. versionadded:: 0.4.1
54
55
        """
56 1
        super(ChaoDice, self).__init__(**kwargs)
57
58 1
    def sim(self, src, tar):
59
        """Return the normalized Chao's Dice similarity of two strings.
60
61
        Parameters
62
        ----------
63
        src : str
64
            Source string for comparison
65
        tar : str
66
            Target string for comparison
67
68
        Returns
69
        -------
70
        float
71
            Normalized Chao's Dice similarity
72
73
        Examples
74
        --------
75
        >>> import random
76
        >>> random.seed(0)
77
        >>> cmp = ChaoDice()
78
        >>> cmp.sim('cat', 'hat')
79
        0.36666666666666664
80
        >>> cmp.sim('Niall', 'Neil')
81
        0.27868852459016397
82
        >>> cmp.sim('aluminum', 'Catalan')
83
        0.0
84
        >>> cmp.sim('ATCG', 'TAGC')
85
        0.0
86
87
88
        .. versionadded:: 0.4.1
89
90
        """
91 1
        return max(0.0, min(1.0, self.sim_score(src, tar)))
92
93 1
    def sim_score(self, src, tar):
94
        """Return the Chao's Dice similarity of two strings.
95
96
        Parameters
97
        ----------
98
        src : str
99
            Source string for comparison
100
        tar : str
101
            Target string for comparison
102
103
        Returns
104
        -------
105
        float
106
            Chao's Dice similarity
107
108
        Examples
109
        --------
110
        >>> import random
111
        >>> random.seed(0)
112
        >>> cmp = ChaoDice()
113
        >>> cmp.sim_score('cat', 'hat')
114
        0.36666666666666664
115
        >>> cmp.sim_score('Niall', 'Neil')
116
        0.27868852459016397
117
        >>> cmp.sim_score('aluminum', 'Catalan')
118
        0.0
119
        >>> cmp.sim_score('ATCG', 'TAGC')
120
        0.0
121
122
123
        .. versionadded:: 0.4.1
124
125
        """
126 1
        self._tokenize(src, tar)
127
128 1
        u_hat, v_hat = self._get_estimates(src, tar)
129
130 1
        num = u_hat * v_hat
131 1
        if num:
132 1
            return 2 * num / (u_hat + v_hat)
133 1
        return 0.0
134
135
136
if __name__ == '__main__':
137
    import doctest
138
139
    doctest.testmod()
140