|
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._length. |
|
20
|
|
|
|
|
21
|
|
|
Length 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 ._distance import _Distance |
|
32
|
|
|
|
|
33
|
1 |
|
__all__ = ['Length', 'dist_length', 'sim_length'] |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
1 |
|
class Length(_Distance): |
|
|
|
|
|
|
37
|
|
|
"""Length similarity and distance.""" |
|
38
|
|
|
|
|
39
|
1 |
|
def sim(self, src, tar): |
|
|
|
|
|
|
40
|
|
|
"""Return the length similarity of two strings. |
|
41
|
|
|
|
|
42
|
|
|
Length similarity is the ratio of the length of the shorter string to |
|
43
|
|
|
the longer. |
|
44
|
|
|
|
|
45
|
|
|
Parameters |
|
46
|
|
|
---------- |
|
47
|
|
|
src : str |
|
48
|
|
|
Source string for comparison |
|
49
|
|
|
tar : str |
|
50
|
|
|
Target string for comparison |
|
51
|
|
|
|
|
52
|
|
|
Returns |
|
53
|
|
|
------- |
|
54
|
|
|
float |
|
55
|
|
|
Length similarity |
|
56
|
|
|
|
|
57
|
|
|
Examples |
|
58
|
|
|
-------- |
|
59
|
|
|
>>> cmp = Length() |
|
60
|
|
|
>>> cmp.sim('cat', 'hat') |
|
61
|
|
|
1.0 |
|
62
|
|
|
>>> cmp.sim('Niall', 'Neil') |
|
63
|
|
|
0.8 |
|
64
|
|
|
>>> cmp.sim('aluminum', 'Catalan') |
|
65
|
|
|
0.875 |
|
66
|
|
|
>>> cmp.sim('ATCG', 'TAGC') |
|
67
|
|
|
1.0 |
|
68
|
|
|
|
|
69
|
|
|
""" |
|
70
|
1 |
|
if src == tar: |
|
71
|
1 |
|
return 1.0 |
|
72
|
1 |
|
if not src or not tar: |
|
73
|
1 |
|
return 0.0 |
|
74
|
1 |
|
return ( |
|
75
|
|
|
len(src) / len(tar) if len(src) < len(tar) else len(tar) / len(src) |
|
76
|
|
|
) |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
1 |
|
def sim_length(src, tar): |
|
80
|
|
|
"""Return the length similarity of two strings. |
|
81
|
|
|
|
|
82
|
|
|
This is a wrapper for :py:meth:`Length.sim`. |
|
83
|
|
|
|
|
84
|
|
|
Parameters |
|
85
|
|
|
---------- |
|
86
|
|
|
src : str |
|
87
|
|
|
Source string for comparison |
|
88
|
|
|
tar : str |
|
89
|
|
|
Target string for comparison |
|
90
|
|
|
|
|
91
|
|
|
Returns |
|
92
|
|
|
------- |
|
93
|
|
|
float |
|
94
|
|
|
Length similarity |
|
95
|
|
|
|
|
96
|
|
|
Examples |
|
97
|
|
|
-------- |
|
98
|
|
|
>>> sim_length('cat', 'hat') |
|
99
|
|
|
1.0 |
|
100
|
|
|
>>> sim_length('Niall', 'Neil') |
|
101
|
|
|
0.8 |
|
102
|
|
|
>>> sim_length('aluminum', 'Catalan') |
|
103
|
|
|
0.875 |
|
104
|
|
|
>>> sim_length('ATCG', 'TAGC') |
|
105
|
|
|
1.0 |
|
106
|
|
|
|
|
107
|
|
|
""" |
|
108
|
1 |
|
return Length().sim(src, tar) |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
1 |
|
def dist_length(src, tar): |
|
112
|
|
|
"""Return the length distance between two strings. |
|
113
|
|
|
|
|
114
|
|
|
This is a wrapper for :py:meth:`Length.dist`. |
|
115
|
|
|
|
|
116
|
|
|
Parameters |
|
117
|
|
|
---------- |
|
118
|
|
|
src : str |
|
119
|
|
|
Source string for comparison |
|
120
|
|
|
tar : str |
|
121
|
|
|
Target string for comparison |
|
122
|
|
|
|
|
123
|
|
|
Returns |
|
124
|
|
|
------- |
|
125
|
|
|
float |
|
126
|
|
|
Length distance |
|
127
|
|
|
|
|
128
|
|
|
Examples |
|
129
|
|
|
-------- |
|
130
|
|
|
>>> dist_length('cat', 'hat') |
|
131
|
|
|
0.0 |
|
132
|
|
|
>>> dist_length('Niall', 'Neil') |
|
133
|
|
|
0.19999999999999996 |
|
134
|
|
|
>>> dist_length('aluminum', 'Catalan') |
|
135
|
|
|
0.125 |
|
136
|
|
|
>>> dist_length('ATCG', 'TAGC') |
|
137
|
|
|
0.0 |
|
138
|
|
|
|
|
139
|
|
|
""" |
|
140
|
1 |
|
return Length().dist(src, tar) |
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
if __name__ == '__main__': |
|
144
|
|
|
import doctest |
|
145
|
|
|
|
|
146
|
|
|
doctest.testmod() |
|
147
|
|
|
|