/
fuzzy_filter.R
222 lines (185 loc) · 6.28 KB
/
fuzzy_filter.R
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
#' Match names that start or contain a specified text string
#'
#' @param name vector of names (scientific or common, see `by`)
#' to be matched against.
#' @param match should we match by names starting with the term or containing
#' the term anywhere in the name?
#' @inheritParams filter_by
#' @importFrom dplyr union mutate_at mutate select
#' @details Note that fuzzy filter will be fast with an single or small number
#' of names, but will be slower if given a very large vector of
#' names to match, as unlike other `filter_` commands,
#' fuzzy matching requires separate SQL calls for each name.
#' As fuzzy matches should all be confirmed manually in any event, e.g.
#' not every common name containing "monkey" belongs to a primate species.
#'
#' This method utilizes the database operation `%like%` to filter tables without
#' loading into memory. Note that this does not support the use of regular
#' expressions at this time.
#' @export
#' @examples
#' \donttest{
#' \dontshow{
#' ## All examples use a temporary directory
#' Sys.setenv(TAXADB_HOME=file.path(tempdir(), "taxadb"))
#' options("taxadb_default_provider"="itis_test")
#' }
#'
#' ## match any common name containing:
#' name <- c("woodpecker", "monkey")
#' fuzzy_filter(name, "vernacularName")
#'
#' ## match scientific name
#' fuzzy_filter("Chera", "scientificName",
#' match = "starts_with")
#' }
#'
fuzzy_filter <- function(name,
by = c("scientificName", "vernacularName"),
provider = getOption("taxadb_default_provider", "itis"),
match = c("contains", "starts_with"),
version = latest_version(),
db = td_connect(),
ignore_case = TRUE,
collect = TRUE){
by <- match.arg(by)
match <- match.arg(match)
db_tbl <- dplyr::mutate(taxa_tbl(provider, "dwc", version, db),
input = !!sym(by))
input <- "input" # NSE
if(ignore_case){
name <- stringi::stri_trans_tolower(name)
db_tbl <- dplyr::mutate_at(db_tbl, .var = "input", .fun = tolower)
}
pattern <- switch(match,
starts_with = paste0(name, "%"),
contains = paste0("%", name, "%"))
out <- filter_like(db_tbl, input, pattern[[1]])
if(length(pattern) > 1){
for(p in pattern[-1]){
out <- dplyr::union(out, filter_like(db_tbl, input, p))
}
}
if (collect) return( dplyr::collect(out) )
out
}
globalVariables("%like%")
filter_like <- function(db_tbl, input, pattern){
if(inherits(db_tbl, "tbl_dbi")){
out <- db_tbl %>%
dplyr::filter(input %like% pattern)
} else {
out <- db_tbl %>%
dplyr::filter(grepl(gsub(pattern, "%", ""), input))
}
out %>%
select(-input) %>%
dplyr::distinct()
}
## Consider creating functions that are explicitly named to create more semantic
## code, rather than relying on setting the behavior in the `by` and `match`
## arguments to `fuzzy_filter`, e.g. :
#' return all taxa in which scientific name contains the text provided
#'
#' @export
#' @examples
#' \donttest{
#' \dontshow{
#' ## All examples use a temporary directory
#' Sys.setenv(TAXADB_HOME=file.path(tempdir(), "taxadb"))
#' options("taxadb_default_provider"="itis_test")
#' }
#' name_contains("Chera")
#' }
#' @inheritParams fuzzy_filter
name_contains <- function(name,
provider = getOption("taxadb_default_provider", "itis"),
version = latest_version(),
db = td_connect(),
ignore_case = TRUE){
fuzzy_filter(name,
by = "scientificName",
provider = provider,
match = "contains",
version = version,
db = db,
ignore_case = ignore_case)
}
#' scientific name starts with
#'
#' @examples
#' \donttest{
#' \dontshow{
#' ## All examples use a temporary directory
#' Sys.setenv(TAXADB_HOME=file.path(tempdir(), "taxadb"))
#' options("taxadb_default_provider"="itis_test")
#' }
#' name_starts_with("Chera")
#' }
#' @inheritParams fuzzy_filter
#' @export
name_starts_with <- function(name,
provider = getOption("taxadb_default_provider", "itis"),
version = latest_version(),
db = td_connect(),
ignore_case = TRUE){
fuzzy_filter(name,
by = "scientificName",
provider = provider,
match = "starts_with",
version = version,
db = db,
ignore_case = ignore_case)
}
#' common name starts with
#'
#' @examples
#' \donttest{
#' \dontshow{
#' ## All examples use a temporary directory
#' Sys.setenv(TAXADB_HOME=file.path(tempdir(), "taxadb"))
#' options("taxadb_default_provider"="itis_test")
#' }
#' common_starts_with("monkey")
#' }
#' @inheritParams fuzzy_filter
#' @export
common_starts_with <- function(name,
provider = getOption("taxadb_default_provider", "itis"),
version = latest_version(),
db = td_connect(),
ignore_case = TRUE){
fuzzy_filter(name,
by = "vernacularName",
provider = provider,
match = "starts_with",
version = version,
db = db,
ignore_case = ignore_case)
}
#' common name starts with
#'
#' @examples
#' \donttest{
#' \dontshow{
#' ## All examples use a temporary directory
#' Sys.setenv(TAXADB_HOME=file.path(tempdir(), "taxadb"))
#' options("taxadb_default_provider"="itis_test")
#' }
#' common_contains("monkey")
#' }
#' @inheritParams fuzzy_filter
#' @export
common_contains <- function(name,
provider = getOption("taxadb_default_provider", "itis"),
version = latest_version(),
db = td_connect(),
ignore_case = TRUE){
fuzzy_filter(name,
by = "vernacularName",
provider = provider,
match = "contains",
version = version,
db = db,
ignore_case = ignore_case)
}