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
|
|
|
|