Mercurial > octave-nkf
comparison scripts/ode/private/fuzzy_compare.m @ 20584:eb9e2d187ed2
maint: Use Octave coding conventions in scripts/ode/private dir.
* AbsRel_Norm.m, fuzzy_compare.m, hermite_quartic_interpolation.m,
integrate_adaptive.m, integrate_const.m, integrate_n_steps.m, kahan.m,
ode_struct_value_check.m, odepkg_event_handle.m, odepkg_structure_check.m,
runge_kutta_45_dorpri.m, starting_stepsize.m:
Wrap long lines to < 80 chars.
Use double quotes rather than single quotes where possible.
Use ';' at end of keywords "return;" and "break;"
Use '##" for stand-alone comments and '#' for end-of-line comments.
Use two spaces after period before starting new sentence.
Use '!' instead of '~' for logical negation.
Use specific form of end (endif, endfor, etc.).
Don't use line continuation marker '...' unless necessary.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 04 Oct 2015 22:18:54 -0700 |
parents | 25623ef2ff4f |
children | b7ac1e94266e |
comparison
equal
deleted
inserted
replaced
20583:d746695bf494 | 20584:eb9e2d187ed2 |
---|---|
15 ## You should have received a copy of the GNU General Public License | 15 ## You should have received a copy of the GNU General Public License |
16 ## along with Octave; see the file COPYING. If not, see | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | 17 ## <http://www.gnu.org/licenses/>. |
18 | 18 |
19 ## -*- texinfo -*- | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {[@var{res}] =} fuzzy_compare (@var{"string1"}, @var{string_set}, [@var{correctness}]) | 20 ## @deftypefn {Function File} {@var{res} =} fuzzy_compare (@var{"string1"}, @var{string_set}) |
21 ## @deftypefnx {Function File} {@var{res} =} fuzzy_compare (@var{"string1"}, @var{string_set}, @var{correctness}) | |
21 ## | 22 ## |
22 ## Compare a string with a set of strings and returns the positions in the | 23 ## Compare a string with a set of strings and returns the positions in the |
23 ## set of strings at which there are the fields that best fit the one we are | 24 ## set of strings at which there are the fields that best fit the one we are |
24 ## comparing. | 25 ## comparing. |
25 ## | 26 ## |
26 ## The distance used to compare the words is the Levenshtein distance | 27 ## The distance used to compare the words is the Levenshtein distance. |
27 ## and for more details see | 28 ## For more details see |
28 ## @url{http://en.wikipedia.org/wiki/Levenshtein_distance}. | 29 ## @url{http://en.wikipedia.org/wiki/Levenshtein_distance}. |
29 ## | 30 ## |
30 ## This function must be called with one output argument @var{res} which | 31 ## This function must be called with one output argument @var{res} which |
31 ## contains the positions of the elements in @var{string_set} which best fit | 32 ## contains the positions of the elements in @var{string_set} which best fit |
32 ## the given word. The tolerance that is used to determine if a field of the | 33 ## the given word. The tolerance that is used to determine if a field of the |
93 endif | 94 endif |
94 | 95 |
95 res = []; | 96 res = []; |
96 | 97 |
97 m = length (string1); | 98 m = length (string1); |
98 fields_nb = size (string_set, 1); | 99 fields_nb = rows (string_set); |
99 | 100 |
100 values = inf .* ones (fields_nb, 1); | 101 values = Inf (fields_nb, 1); |
101 | 102 |
102 string1 = deblank (string1); | 103 string1 = deblank (string1); |
103 string2 = []; | 104 string2 = []; |
104 | 105 |
105 minimus = inf; | 106 minimus = inf; |
119 endfor | 120 endfor |
120 | 121 |
121 positions = find (values == minimus); | 122 positions = find (values == minimus); |
122 | 123 |
123 if (minimus == 0) # exact match | 124 if (minimus == 0) # exact match |
124 if (size (positions, 1) != 1) | 125 if (rows (positions) != 1) |
125 error ("OdePkg:InvalidArgument", | 126 error ("OdePkg:InvalidArgument", |
126 "there are %d strings perfectly matching ''%s''", | 127 "there are %d strings perfectly matching '%s'", |
127 size (positions, 1), string1); | 128 rows (positions), string1); |
128 endif | 129 endif |
129 res = positions; | 130 res = positions; |
130 return | 131 return; |
131 endif | 132 endif |
132 | 133 |
133 ## determine the tolerance with the formula described in the | 134 ## determine the tolerance with the formula described in the |
134 ## textinfo section it is a downwards parable with zeros in 0 and m | 135 ## textinfo section it is a downwards parable with zeros in 0 and m |
135 ## and with a maximum in m/2 of value m/2 | 136 ## and with a maximum in m/2 of value m/2 |
140 if (nargin == 3) | 141 if (nargin == 3) |
141 if ((isnumeric (correctness) | 142 if ((isnumeric (correctness) |
142 && isscalar (correctness) | 143 && isscalar (correctness) |
143 && correctness == 0) | 144 && correctness == 0) |
144 || (ischar (correctness) | 145 || (ischar (correctness) |
145 && strcmp (lower (deblank (correctness)), 'exact'))) | 146 && strcmp (lower (deblank (correctness)), "exact"))) |
146 error ("OdePkg:InvalidArgument", | 147 error ("OdePkg:InvalidArgument", |
147 "no exact matching for string ''%s''", string1); | 148 "no exact matching for string '%s'", string1); |
148 endif | 149 endif |
149 if (isnumeric (correctness) | 150 if (isnumeric (correctness) && isscalar (correctness)) |
150 && isscalar (correctness)) | |
151 tolerance = correctness; | 151 tolerance = correctness; |
152 endif | 152 endif |
153 endif | 153 endif |
154 | 154 |
155 ## returning the positions of the fields whose distance is lower | 155 ## returning the positions of the fields whose distance is lower |
156 ## than the tolerance | 156 ## than the tolerance |
157 for i = 1:1:fields_nb | 157 for i = 1:fields_nb |
158 if (values(i) <= tolerance) | 158 if (values(i) <= tolerance) |
159 res = [res; i]; | 159 res = [res; i]; |
160 endif | 160 endif |
161 endfor | 161 endfor |
162 | 162 |
163 endfunction | 163 endfunction |
164 | 164 |
165 ## Local Variables: *** | |
166 ## mode: octave *** | |
167 ## End: *** |