File: stringdist_api.h

package info (click to toggle)
r-cran-stringdist 0.9.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,424 kB
  • sloc: ansic: 1,690; sh: 13; makefile: 2
file content (313 lines) | stat: -rw-r--r-- 12,304 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313

/*  stringdist - a C library of string distance algorithms with an interface to R.
 *  Copyright (C) 2013  Mark van der Loo
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *
 *  You can contact the author at: mark _dot_ vanderloo _at_ gmail _dot_ com
 */

#ifndef _STRINGDIST_API_H
#define _STRINGDIST_API_H

#include <R_ext/Rdynload.h>

#ifdef HAVE_VISIBILITY_ATTRIBUTE
# define attribute_hidden __attribute__ ((visibility ("hidden")))
#else
# define attribute_hidden
#endif

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @mainpage Stringdist C API
 *
 * @author Mark van der Loo, Jan van der Laan, R Core Team, Paul Hsieh, Chris Muir
 * @version `R package stringdist` version `0.9.5.0` and higher.
 *
 * @section using Using the stringdist C API
 * To call the functions described here from your package you need to:
 * 
 * 1. Make sure that `stringdist` is installed.
 * 2. Add `stringdist` to `Imports` (or `Depends`) and `LinkingTo` in the `DESCRIPTION` file.
 * 3. In your source file under the package's `/src` directory, add the line 
 * ```
 * #include <stringdist_api.h>
 * ```
 * 
 * 
 * An example of a published package using this API is
 * [refinr](https://CRAN.R-project.org/package=refinr). A minimal example can be
 * found [here](https://github.com/markvanderloo/linkstringdist).
 *
 * @section encoding Character encoding
 * All `character` vector input is expected to be in `UTF-8` (this also allows
 * `ASCII`).  Distance computations are based on UTF [code
 * points](https://en.wikipedia.org/wiki/Code_point) unless `useBytes` is
 * `TRUE`, in which case distances are computed over byte sequences. Using
 * non-UTF-8 encoded strings is untested and is highly likely to result in
 * errors.
 *
 * @section threads Thread safety
 *
 * It is not safe to call functions from `stringdist` C API from 
 * multiple concurrent threads.
 *
 *
 *
 */


/** 
 * @file stringdist_api.h
 * @brief Functions exported from the stringdist package.
 *
 */


/*
SEXP attribute_hidden sd_all_int(SEXP X)
{
  static SEXP(*fun)(SEXP) = NULL;
  if (fun == NULL) fun = (SEXP(*)(SEXP)) R_GetCCallable("stringdist","R_all_int");
  return fun(X);
}
*/

/**
 * @brief Find the location of values in `x` in `table` by approximate matching.
 * 
 * @param x `[character]` vector.
 * @param table `[character]` vector (lookup table)
 * @param method `[integer]` scalar, indicating the distance method as follows
 *   @parblock
 *    - 0: Optimal String Alignment (`"osa"`)
 *    - 1: Levenshtein (`"lv"`)
 *    - 2: Damerau-Levenshtein (`"dl"`)
 *    - 3: Hamming (`"hamming"`)
 *    - 4: Longest Common Substring (`"lcs"`)
 *    - 5: q-gram (`"qgram"`)
 *    - 6: cosine (`"cosine"`)
 *    - 7: Jaccard (`"jaccard"`)
 *    - 8: Jaro-Winkler (`"jw"`)
 *    - 9: Soundex (`"soundex"`)
 *   @endparblock
 * @param nomatch `[integer]` The value to be returned when no match is found. 
 * @param matchNA Should `NA`s be matched? Default behaviour mimics the
 *   behaviour of base `match`, meaning that `NA` matches `NA` (see also the note
 *   on `NA` handling below).
 * @param weight `[numeric]` vector. Edit penalty
 *   @parblock
 *     For `method='osa'` or`'dl'`, the penalty for
 *     deletion, insertion, substitution and transposition, in that order. When
 *     `method='lv'`, the penalty for transposition is ignored. When
 *     `method='jw'`, the weights associated with characters of `a`,
 *     characters from `b` and the transposition weight, in that order. 
 *     Weights must be positive and not exceed 1. `weight` is ignored
 *     completely for other methods
 *   @endparblock
 * @param q  `[integer]` scalar. Size of the q-gram; must be nonnegative. Only
 *   applies to `method='qgram'`, `'jaccard'` or `'cosine'`.
 * @param maxDistance `[numeric]` scalar. The maximum distance allowed for matching.
 * @param p `[numeric]` scalar. Penalty factor for Jaro-Winkler distance. The
 *   valid range for `p` is `0 <= p <= 0.25`. If `p=0` (default), the
 *   Jaro-distance is returned. Applies only to `method='jw'`.
 * @param bt `[numeric]` vector. Winkler's boost threshold. Winkler's penalty
 *   factor is only applied when the Jaro distance is larger than `bt`.  Applies
 *   only to `method='jw'` and `p>0`.
 * @param useBytes Perform byte-wise comparison (i.e. do not translate UTF-8 to
 *   integer prior to distance calculation)
 * @param nthread `[integer]` scalar. Maximum number of threads to use. 
 *
 *
 * @return
 * `[integer]` vector of `length(x)` with indices in `table`.
 */
SEXP attribute_hidden sd_amatch(SEXP x, SEXP table, SEXP method 
                                  , SEXP nomatch, SEXP matchNA
                                  , SEXP weight, SEXP p, SEXP bt, SEXP q
                                  , SEXP maxDistance, SEXP useBytes
                                  , SEXP nthrd)
{
  static SEXP(*fun)(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP) = NULL;
  if (fun == NULL) fun = (SEXP(*)(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP)) R_GetCCallable("stringdist","R_amatch");
  return fun(x, table, method, nomatch, matchNA, weight, p, bt, q, maxDistance, useBytes, nthrd);
}

/**
 * @brief Compute q-gram counts
 * 
 * @param a `[character]` vector 
 * @param qq `[integer`] scalar. 
 * 
 * @return
 * A `[numeric]` vector of `length(a)*n_qgrams`, where `n_qrams` is the number
 * of different `qgrams` observed in the elements of `a`. The output vector has
 * an attribute called `qgrams`, which is an integer vector of size
 * `q*n_qgrams` containing integer (UTF-32) labels for the q-grams
 * sequentially.
 * 
 */
SEXP attribute_hidden sd_get_qgrams(SEXP a, SEXP qq)
{
  static SEXP(*fun)(SEXP, SEXP) = NULL;
  if (fun == NULL) fun = (SEXP(*)(SEXP, SEXP)) R_GetCCallable("stringdist","R_get_qgrams");
  return fun(a, qq);
}

/*
SEXP attribute_hidden sd_lengths(SEXP X)
{
  static SEXP(*fun)(SEXP) = NULL;
  if (fun == NULL) fun = (SEXP(*)(SEXP)) R_GetCCallable("stringdist","R_lengths");
  return fun(X);
}
*/

/**
 * @brief Lower tridiagonal elements of distance matrix.
 *
 * @param a `[character]` vector
 * @param method `[integer]` scalar, indicating the distance method as follows
 *   @parblock
 *    - 0: Optimal String Alignment (`"osa"`)
 *    - 1: Levenshtein (`"lv"`)
 *    - 2: Damerau-Levenshtein (`"dl"`)
 *    - 3: Hamming (`"hamming"`)
 *    - 4: Longest Common Substring (`"lcs"`)
 *    - 5: q-gram (`"qgram"`)
 *    - 6: cosine (`"cosine"`)
 *    - 7: Jaccard (`"jaccard"`)
 *    - 8: Jaro-Winkler (`"jw"`)
 *    - 9: Soundex (`"soundex"`)
 *   @endparblock
 * @param weight `[numeric]` vector. Edit penalty
 *   @parblock
 *     For `method='osa'` or`'dl'`, the penalty for deletion, insertion,
 *     substitution and transposition, in that order. When `method='lv'`, the
 *     penalty for transposition is ignored. When `method='jw'`, the weights
 *     associated with characters of `a`, characters from `b` and the
 *     transposition weight, in that order.  Weights must be positive and not
 *     exceed 1. `weight` is ignored completely for other methods
 *   @endparblock
 * @param q  `[integer]` scalar. Size of the q-gram; must be nonnegative. Only
 *   applies to `method='qgram'`, `'jaccard'` or `'cosine'`.
 * @param p `[numeric]` scalar. Penalty factor for Jaro-Winkler distance. The
 *   valid range for `p` is `0 <= p <= 0.25`. If `p=0` (default), the
 *   Jaro-distance is returned. Applies only to `method='jw'`.
 * @param bt `[numeric]` vector. Winkler's boost threshold. Winkler's penalty
 *   factor is only applied when the Jaro distance is larger than `bt`.  Applies
 *   only to `method='jw'` and `p>0`.
 * @param useBytes Perform byte-wise comparison (i.e. do not translate UTF-8 to
 *   integer prior to distance calculation)
 * @param nthread `[integer]` scalar. Maximum number of threads to use. 
 *
 * @return
 *   A `[numeric]` vector of length `n*(n-1)/2`, where `n=length(a)`. It contains
 *   the positive values of consequtive columns of the distance matrix. Also see
 *   the R-code in  `stringdist:::lower_tri`.
 */
SEXP attribute_hidden sd_lower_tri(SEXP a, SEXP method
                                     , SEXP weight, SEXP p,  SEXP bt, SEXP q
                                     , SEXP useBytes, SEXP nthrd)
{
  static SEXP(*fun)(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP) = NULL;
  if (fun == NULL) fun = (SEXP(*)(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP)) R_GetCCallable("stringdist","R_lower_tri");
  return fun(a, method, weight, p, bt, q, useBytes, nthrd);
}

/**
 * @brief Compute soundex code
 *
 * @param[in] x `[character]` vector
 * @param[in] useBytes `[logical]` scalar.
 *
 * @return
 *
 * A `list` with `length(x)` element. Each element is a length 4 integer vector 
 * representing a 4-character soundex code. The integers are ASCII code points.
 *
 */
SEXP attribute_hidden sd_soundex(SEXP x, SEXP useBytes)
{
  static SEXP(*fun)(SEXP, SEXP) = NULL;
  if (fun == NULL) fun = (SEXP(*)(SEXP, SEXP)) R_GetCCallable("stringdist","R_soundex");
  return fun(x, useBytes);
}

/**
 * @brief compute string distances
 * 
 * @param a `[character]` vector
 * @param b `[character]` vector
 * @param method `[integer]` scalar, indicating the distance method as follows
 *   @parblock
 *    - 0: Optimal String Alignment (`"osa"`)
 *    - 1: Levenshtein (`"lv"`)
 *    - 2: Damerau-Levenshtein (`"dl"`)
 *    - 3: Hamming (`"hamming"`)
 *    - 4: Longest Common Substring (`"lcs"`)
 *    - 5: q-gram (`"qgram"`)
 *    - 6: cosine (`"cosine"`)
 *    - 7: Jaccard (`"jaccard"`)
 *    - 8: Jaro-Winkler (`"jw"`)
 *    - 9: Soundex (`"soundex"`)
 *   @endparblock
 * @param weight `[numeric]` vector. Edit penalty
 *   @parblock
 *     For `method='osa'` or`'dl'`, the penalty for deletion, insertion,
 *     substitution and transposition, in that order. When `method='lv'`, the
 *     penalty for transposition is ignored. When `method='jw'`, the weights
 *     associated with characters of `a`, characters from `b` and the
 *     transposition weight, in that order.  Weights must be positive and not
 *     exceed 1. `weight` is ignored completely for other methods
 *   @endparblock
 * @param q  `[integer]` scalar. Size of the q-gram; must be nonnegative. Only
 *   applies to `method='qgram'`, `'jaccard'` or `'cosine'`.
 * @param p `[numeric]` scalar. Penalty factor for Jaro-Winkler distance. The
 *   valid range for `p` is `0 <= p <= 0.25`. If `p=0` (default), the
 *   Jaro-distance is returned. Applies only to `method='jw'`.
 * @param bt `[numeric]` vector. Winkler's boost threshold. Winkler's penalty
 *   factor is only applied when the Jaro distance is larger than `bt`.  Applies
 *   only to `method='jw'` and `p>0`.
 * @param useBytes Perform byte-wise comparison (i.e. do not translate UTF-8 to
 *   integer prior to distance calculation)
 * @param nthread `[integer]` scalar. Maximum number of threads to use. 
 *
 *
 * @return
 *   A `[numeric]` vector of length `max(length(a),length(b))` where the shortest
 *   vector is recycled over the longer (no warnings are given when the longer
 *   length is not an integer multiple of the shorter length).
 *
 *
 */
SEXP attribute_hidden sd_stringdist(SEXP a, SEXP b, SEXP method
                                      , SEXP weight, SEXP p, SEXP bt, SEXP q
                                      , SEXP useBytes, SEXP nthrd)
{
  static SEXP(*fun)(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP) = NULL;
  if (fun == NULL) fun = (SEXP(*)(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP)) R_GetCCallable("stringdist","R_stringdist");
  return fun(a, b, method, weight, p, bt, q, useBytes, nthrd);
}


#ifdef __cplusplus
}
#endif

#endif