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._morisita. |
20
|
|
|
|
21
|
|
|
Morisita index of overlap |
22
|
|
|
""" |
23
|
|
|
|
24
|
1 |
|
from __future__ import ( |
25
|
|
|
absolute_import, |
26
|
|
|
division, |
27
|
|
|
print_function, |
28
|
|
|
unicode_literals, |
29
|
|
|
) |
30
|
|
|
|
31
|
1 |
|
from ._token_distance import _TokenDistance |
32
|
|
|
|
33
|
1 |
|
__all__ = ['Morisita'] |
34
|
|
|
|
35
|
|
|
|
36
|
1 |
|
class Morisita(_TokenDistance): |
37
|
|
|
r"""Morisita index of overlap. |
38
|
|
|
|
39
|
|
|
Morisita index of overlap :cite:`Morisita:1959`, following the description |
40
|
|
|
of :cite:`Horn:1966`, given two populations X and Y drawn from S species, |
41
|
|
|
is: |
42
|
|
|
|
43
|
|
|
.. math:: |
44
|
|
|
|
45
|
|
|
sim_{Morisita}(X, Y) = |
46
|
|
|
C_{\lambda} = \frac{2\sum_{i=1}^S x_i y_i}{(\lambda_x + \lambda_y)XY} |
47
|
|
|
|
48
|
|
|
where |
49
|
|
|
|
50
|
|
|
.. math:: |
51
|
|
|
|
52
|
|
|
X = \sum_{i=1}^S x_i ~~;~~ Y = \sum_{i=1}^S y_i |
53
|
|
|
|
54
|
|
|
.. math:: |
55
|
|
|
|
56
|
|
|
\lambda_x = \frac{\sum_{i=1}^S x_i(x_i-1)}{X(X-1)} ~~;~~ |
57
|
|
|
\lambda_y = \frac{\sum_{i=1}^S y_i(y_i-1)}{Y(Y-1)} |
58
|
|
|
|
59
|
|
|
.. versionadded:: 0.4.1 |
60
|
|
|
""" |
61
|
|
|
|
62
|
1 |
|
def __init__(self, **kwargs): |
63
|
|
|
"""Initialize Morisita instance. |
64
|
|
|
|
65
|
|
|
Parameters |
66
|
|
|
---------- |
67
|
|
|
**kwargs |
68
|
|
|
Arbitrary keyword arguments |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
.. versionadded:: 0.4.1 |
72
|
|
|
|
73
|
|
|
""" |
74
|
1 |
|
super(Morisita, self).__init__(**kwargs) |
75
|
|
|
|
76
|
1 |
|
def sim_score(self, src, tar): |
77
|
|
|
"""Return the Morisita similarity of two strings. |
78
|
|
|
|
79
|
|
|
Parameters |
80
|
|
|
---------- |
81
|
|
|
src : str |
82
|
|
|
Source string for comparison |
83
|
|
|
tar : str |
84
|
|
|
Target string for comparison |
85
|
|
|
|
86
|
|
|
Returns |
87
|
|
|
------- |
88
|
|
|
float |
89
|
|
|
Morisita similarity |
90
|
|
|
|
91
|
|
|
Examples |
92
|
|
|
-------- |
93
|
|
|
>>> cmp = Morisita() |
94
|
|
|
>>> cmp.sim_score('cat', 'hat') |
95
|
|
|
0.25 |
96
|
|
|
>>> cmp.sim_score('Niall', 'Neil') |
97
|
|
|
0.13333333333333333 |
98
|
|
|
>>> cmp.sim_score('aluminum', 'Catalan') |
99
|
|
|
1.0 |
100
|
|
|
>>> cmp.sim_score('ATCG', 'TAGC') |
101
|
|
|
0.0 |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
.. versionadded:: 0.4.1 |
105
|
|
|
|
106
|
|
|
""" |
107
|
1 |
|
self._tokenize(src, tar) |
108
|
|
|
|
109
|
1 |
|
intersection = self._intersection() |
110
|
|
|
|
111
|
1 |
|
src_card = self._src_card() |
112
|
1 |
|
tar_card = self._tar_card() |
113
|
|
|
|
114
|
1 |
|
src_lambda = 0 |
115
|
1 |
|
tar_lambda = 0 |
116
|
1 |
|
for val in self._src_tokens.values(): |
117
|
1 |
|
src_lambda += val * (val - 1) |
118
|
1 |
|
if src_lambda: |
119
|
1 |
|
src_lambda /= src_card * (src_card - 1) |
120
|
1 |
|
for val in self._tar_tokens.values(): |
121
|
1 |
|
tar_lambda += val * (val - 1) |
122
|
1 |
|
if tar_lambda: |
123
|
1 |
|
tar_lambda /= tar_card * (tar_card - 1) |
124
|
|
|
|
125
|
1 |
|
sim = 0 |
126
|
1 |
|
for symbol in intersection.keys(): |
127
|
1 |
|
sim += self._src_tokens[symbol] * self._tar_tokens[symbol] |
128
|
1 |
|
sim *= 2 |
129
|
1 |
|
if src_card: |
130
|
1 |
|
sim /= src_card |
131
|
1 |
|
if tar_card: |
132
|
1 |
|
sim /= tar_card |
133
|
1 |
|
if src_lambda + tar_lambda: |
134
|
1 |
|
sim /= src_lambda + tar_lambda |
135
|
|
|
|
136
|
1 |
|
return sim |
137
|
|
|
|
138
|
1 |
|
def sim(self, *args, **kwargs): |
139
|
|
|
"""Raise exception when called. |
140
|
|
|
|
141
|
|
|
Parameters |
142
|
|
|
---------- |
143
|
|
|
*args |
144
|
|
|
Variable length argument list |
145
|
|
|
**kwargs |
146
|
|
|
Arbitrary keyword arguments |
147
|
|
|
|
148
|
|
|
Raises |
149
|
|
|
------ |
150
|
|
|
NotImplementedError |
151
|
|
|
Method disabled for Morisita similarity. |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
.. versionadded:: 0.3.6 |
155
|
|
|
|
156
|
|
|
""" |
157
|
1 |
|
raise NotImplementedError('Method disabled for Morisita similarity.') |
158
|
|
|
|
159
|
1 |
|
def dist(self, *args, **kwargs): |
160
|
|
|
"""Raise exception when called. |
161
|
|
|
|
162
|
|
|
Parameters |
163
|
|
|
---------- |
164
|
|
|
*args |
165
|
|
|
Variable length argument list |
166
|
|
|
**kwargs |
167
|
|
|
Arbitrary keyword arguments |
168
|
|
|
|
169
|
|
|
Raises |
170
|
|
|
------ |
171
|
|
|
NotImplementedError |
172
|
|
|
Method disabled for Morisita similarity. |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
.. versionadded:: 0.3.6 |
176
|
|
|
|
177
|
|
|
""" |
178
|
1 |
|
raise NotImplementedError('Method disabled for Morisita similarity.') |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
if __name__ == '__main__': |
182
|
|
|
import doctest |
183
|
|
|
|
184
|
|
|
doctest.testmod() |
185
|
|
|
|