File: match.R

package info (click to toggle)
r-cran-triebeard 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 392 kB
  • sloc: cpp: 1,095; sh: 13; makefile: 2; ansic: 1
file content (157 lines) | stat: -rw-r--r-- 5,914 bytes parent folder | download
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
#'@title Find the longest match in a trie
#'@description \code{longest_match} accepts a trie and a character vector
#'and returns the value associated with whichever key had the \emph{longest match}
#'to each entry in the character vector. A trie of "binary" and "bind", for example,
#'with an entry-to-compare of "binder", will match to "bind".
#'
#'@param trie a trie object, created with \code{\link{trie}}
#'
#'@param to_match a character vector containing the strings to match against the
#'trie's keys.
#'
#'@param include_keys a logical value indicating whether to include the keys in the
#'returned results or not. If TRUE (\emph{not} the default) the returned object will
#'be a data.frame, rather than a vector.
#'
#'@examples
#'trie <- trie(keys = c("afford", "affair", "available", "binary", "bind", "blind"),
#'                    values = c("afford", "affair", "available", "binary", "bind", "blind"))
#'longest_match(trie, "binder")
#'
#'@seealso \code{\link{prefix_match}} and \code{\link{greedy_match}}
#'for prefix and greedy matching, respectively.
#'
#'@export
longest_match <- function(trie, to_match, include_keys = FALSE){
  stopifnot("trie" %in% class(trie))
  stopifnot(!is.na(include_keys))
  UseMethod("longest_match", trie)
}

#'@export
longest_match.string_trie <- function(trie, to_match, include_keys = FALSE){
  return(longest_string(trie, to_match, include_keys))
}

#'@export
longest_match.integer_trie <- function(trie, to_match, include_keys = FALSE){
  return(longest_integer(trie, to_match, include_keys))
}

#'@export
longest_match.numeric_trie <- function(trie, to_match, include_keys = FALSE){
  return(longest_numeric(trie, to_match, include_keys))
}

#'@export
longest_match.logical_trie <- function(trie, to_match, include_keys = FALSE){
  return(longest_logical(trie, to_match, include_keys))
}

#'@title Find the prefix matches in a trie
#'@description \code{prefix_match} accepts a trie and a character vector
#'and returns the values associated with any key that has a particular
#'character vector entry as a prefix (see the examples).
#'
#'@param trie a trie object, created with \code{\link{trie}}
#'
#'@param to_match a character vector containing the strings to check against the
#'trie's keys.
#'
#'@param include_keys a logical value indicating whether to include the keys in the
#'returned results or not. If TRUE (\emph{not} the default) the returned object will
#'be a list of data.frames, rather than of vector.
#'
#'@return a list, the length of \code{to_match}, with each entry containing any trie values
#'where the \code{to_match} element was a prefix of the associated key. In the case that
#'nothing was found, the entry will contain \code{NA}.
#'
#'@examples
#'trie <- trie(keys = c("afford", "affair", "available", "binary", "bind", "blind"),
#'                    values = c("afford", "affair", "available", "binary", "bind", "blind"))
#'prefix_match(trie, "aff")
#'
#'@seealso \code{\link{longest_match}} and \code{\link{greedy_match}}
#'for longest and greedy matching, respectively.
#'
#'@export
prefix_match <- function(trie, to_match, include_keys = FALSE){
  stopifnot("trie" %in% class(trie))
  stopifnot(!is.na(include_keys))
  UseMethod("prefix_match", trie)
}

#'@export
prefix_match.numeric_trie <- function(trie, to_match, include_keys = FALSE){
  return(prefix_numeric(trie, to_match, include_keys))
}

#'@export
prefix_match.integer_trie <- function(trie, to_match, include_keys = FALSE){
  return(prefix_integer(trie, to_match, include_keys))
}

#'@export
prefix_match.string_trie <- function(trie, to_match, include_keys = FALSE){
  return(prefix_string(trie, to_match, include_keys))
}

#'@export
prefix_match.logical_trie <- function(trie, to_match, include_keys = FALSE){
  return(prefix_logical(trie, to_match, include_keys))
}

#'@title Greedily match against a tree
#'@description \code{greedy_match} accepts a trie and a character vector
#'and returns the values associated with any key that is "greedily" (read: fuzzily)
#'matched against one of the character vector entries.
#'
#'@param trie a trie object, created with \code{\link{trie}}
#'
#'@param to_match a character vector containing the strings to check against the
#'trie's keys.
#'
#'@param include_keys a logical value indicating whether to include the keys in the
#'returned results or not. If TRUE (\emph{not} the default) the returned object will
#'be a list of data.frames, rather than of vectors.
#'
#'@return a list, the length of \code{to_match}, with each entry containing any trie values
#'where the \code{to_match} element greedily matches the associated key. In the case that
#'nothing was found, the entry will contain \code{NA}. In the case that \code{include_keys}
#'is TRUE, the matching keys will also be included
#'
#'@examples
#'trie <- trie(keys = c("afford", "affair", "available", "binary", "bind", "blind"),
#'                    values = c("afford", "affair", "available", "binary", "bind", "blind"))
#'greedy_match(trie, c("avoid", "bring", "attack"))
#'
#'@seealso \code{\link{longest_match}} and \code{\link{prefix_match}}
#'for longest and prefix matching, respectively.
#'
#'@export
greedy_match <- function(trie, to_match, include_keys = FALSE){
  stopifnot("trie" %in% class(trie))
  stopifnot(!is.na(include_keys))
  UseMethod("greedy_match", trie)
}

#'@export
greedy_match.numeric_trie <- function(trie, to_match, include_keys = FALSE){
  return(greedy_numeric(trie, to_match, include_keys))
}


#'@export
greedy_match.integer_trie <- function(trie, to_match, include_keys = FALSE){
  return(greedy_integer(trie, to_match, include_keys))
}

#'@export
greedy_match.string_trie <- function(trie, to_match, include_keys = FALSE){
  return(greedy_string(trie, to_match, include_keys))
}

#'@export
greedy_match.logical_trie <- function(trie, to_match, include_keys = FALSE){
  return(greedy_logical(trie, to_match, include_keys))
}