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._indel. |
20
|
|
|
|
21
|
|
|
Indel distance |
22
|
|
|
""" |
23
|
|
|
|
24
|
1 |
|
from __future__ import ( |
25
|
|
|
absolute_import, |
26
|
|
|
division, |
27
|
|
|
print_function, |
28
|
|
|
unicode_literals, |
29
|
|
|
) |
30
|
|
|
|
31
|
1 |
|
from ._distance import _Distance |
32
|
1 |
|
from ._levenshtein import Levenshtein |
33
|
|
|
|
34
|
1 |
|
__all__ = ['Indel', 'dist_indel', 'sim_indel'] |
35
|
|
|
|
36
|
|
|
|
37
|
1 |
|
class Indel(_Distance): |
|
|
|
|
38
|
|
|
"""Indel distance. |
39
|
|
|
|
40
|
|
|
This is equivalent to Levenshtein distance, when only inserts and deletes |
41
|
|
|
are possible. |
42
|
|
|
""" |
43
|
|
|
|
44
|
1 |
|
_lev = Levenshtein() |
45
|
|
|
|
46
|
1 |
|
def dist_abs(self, src, tar): |
|
|
|
|
47
|
|
|
"""Return the indel distance between two strings. |
48
|
|
|
|
49
|
|
|
Args: |
50
|
|
|
src (str): Source string for comparison |
51
|
|
|
tar (str): Target string for comparison |
52
|
|
|
|
53
|
|
|
Returns: |
54
|
|
|
int: Indel distance |
55
|
|
|
|
56
|
|
|
Examples: |
57
|
|
|
>>> cmp = Indel() |
58
|
|
|
>>> cmp.dist_abs('cat', 'hat') |
59
|
|
|
2 |
60
|
|
|
>>> cmp.dist_abs('Niall', 'Neil') |
61
|
|
|
3 |
62
|
|
|
>>> cmp.dist_abs('Colin', 'Cuilen') |
63
|
|
|
5 |
64
|
|
|
>>> cmp.dist_abs('ATCG', 'TAGC') |
65
|
|
|
4 |
66
|
|
|
|
67
|
|
|
""" |
68
|
1 |
|
return self._lev.dist_abs( |
69
|
|
|
src, tar, mode='lev', cost=(1, 1, 9999, 9999) |
70
|
|
|
) |
71
|
|
|
|
72
|
1 |
|
def dist(self, src, tar): |
|
|
|
|
73
|
|
|
"""Return the normalized indel distance between two strings. |
74
|
|
|
|
75
|
|
|
This is equivalent to normalized Levenshtein distance, when only |
76
|
|
|
inserts and deletes are possible. |
77
|
|
|
|
78
|
|
|
Args: |
79
|
|
|
src (str): Source string for comparison |
80
|
|
|
tar (str): Target string for comparison |
81
|
|
|
|
82
|
|
|
Returns: |
83
|
|
|
float: Normalized indel distance |
84
|
|
|
|
85
|
|
|
Examples: |
86
|
|
|
>>> cmp = Indel() |
87
|
|
|
>>> round(cmp.dist('cat', 'hat'), 12) |
88
|
|
|
0.333333333333 |
89
|
|
|
>>> round(cmp.dist('Niall', 'Neil'), 12) |
90
|
|
|
0.333333333333 |
91
|
|
|
>>> round(cmp.dist('Colin', 'Cuilen'), 12) |
92
|
|
|
0.454545454545 |
93
|
|
|
>>> cmp.dist('ATCG', 'TAGC') |
94
|
|
|
0.5 |
95
|
|
|
|
96
|
|
|
""" |
97
|
1 |
|
if src == tar: |
98
|
1 |
|
return 0.0 |
99
|
1 |
|
return self.dist_abs(src, tar) / (len(src) + len(tar)) |
100
|
|
|
|
101
|
|
|
|
102
|
1 |
|
def indel(src, tar): |
103
|
|
|
"""Return the indel distance between two strings. |
104
|
|
|
|
105
|
|
|
Args: |
106
|
|
|
src (str): Source string for comparison |
107
|
|
|
tar (str): Target string for comparison |
108
|
|
|
|
109
|
|
|
Returns: |
110
|
|
|
int: Indel distance |
111
|
|
|
|
112
|
|
|
Examples: |
113
|
|
|
>>> indel('cat', 'hat') |
114
|
|
|
2 |
115
|
|
|
>>> indel('Niall', 'Neil') |
116
|
|
|
3 |
117
|
|
|
>>> indel('Colin', 'Cuilen') |
118
|
|
|
5 |
119
|
|
|
>>> indel('ATCG', 'TAGC') |
120
|
|
|
4 |
121
|
|
|
|
122
|
|
|
""" |
123
|
|
|
return Indel().dist_abs(src, tar) |
124
|
|
|
|
125
|
|
|
|
126
|
1 |
|
def dist_indel(src, tar): |
127
|
|
|
"""Return the normalized indel distance between two strings. |
128
|
|
|
|
129
|
|
|
This is equivalent to normalized Levenshtein distance, when only inserts |
130
|
|
|
and deletes are possible. |
131
|
|
|
|
132
|
|
|
Args: |
133
|
|
|
src (str): Source string for comparison |
134
|
|
|
tar (str): Target string for comparison |
135
|
|
|
|
136
|
|
|
Returns: |
137
|
|
|
float: Normalized indel distance |
138
|
|
|
|
139
|
|
|
Examples: |
140
|
|
|
>>> round(dist_indel('cat', 'hat'), 12) |
141
|
|
|
0.333333333333 |
142
|
|
|
>>> round(dist_indel('Niall', 'Neil'), 12) |
143
|
|
|
0.333333333333 |
144
|
|
|
>>> round(dist_indel('Colin', 'Cuilen'), 12) |
145
|
|
|
0.454545454545 |
146
|
|
|
>>> dist_indel('ATCG', 'TAGC') |
147
|
|
|
0.5 |
148
|
|
|
|
149
|
|
|
""" |
150
|
1 |
|
return Indel().dist(src, tar) |
151
|
|
|
|
152
|
|
|
|
153
|
1 |
|
def sim_indel(src, tar): |
154
|
|
|
"""Return the normalized indel similarity of two strings. |
155
|
|
|
|
156
|
|
|
This is equivalent to normalized Levenshtein similarity, when only inserts |
157
|
|
|
and deletes are possible. |
158
|
|
|
|
159
|
|
|
Args: |
160
|
|
|
src (str): Source string for comparison |
161
|
|
|
tar (str): Target string for comparison |
162
|
|
|
|
163
|
|
|
Returns: |
164
|
|
|
float: Normalized indel similarity |
165
|
|
|
|
166
|
|
|
Examples: |
167
|
|
|
>>> round(sim_indel('cat', 'hat'), 12) |
168
|
|
|
0.666666666667 |
169
|
|
|
>>> round(sim_indel('Niall', 'Neil'), 12) |
170
|
|
|
0.666666666667 |
171
|
|
|
>>> round(sim_indel('Colin', 'Cuilen'), 12) |
172
|
|
|
0.545454545455 |
173
|
|
|
>>> sim_indel('ATCG', 'TAGC') |
174
|
|
|
0.5 |
175
|
|
|
|
176
|
|
|
""" |
177
|
1 |
|
return Indel().sim(src, tar) |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
if __name__ == '__main__': |
181
|
|
|
import doctest |
182
|
|
|
|
183
|
|
|
doctest.testmod() |
184
|
|
|
|