Skip to contents

metric_exact_match() makes a metric that is TRUE when the prediction equals the expected value, compared as text, and FALSE otherwise.

Usage

metric_exact_match(field = NULL, ignore_case = FALSE, normalize = TRUE)

Arguments

field

Name of the output field to compare. evaluate(), optimize_grid() and compile() pass the whole data row as expected; when field is NULL, the metric compares the one prediction field that is also a column of that row, and errors if there is not exactly one.

ignore_case

If TRUE, compare without regard to case.

normalize

If TRUE (the default), trim white space at both ends and collapse runs of white space before comparing.

Value

A function function(prediction, expected) returning TRUE or FALSE, for evaluate(), compile() and the optimizers. field is stored in its "field" attribute.

Examples

match_sentiment <- metric_exact_match(field = "sentiment")

# How evaluate() calls it: the prediction, then the whole data row
row <- data.frame(text = "Great!", sentiment = "positive")
match_sentiment(list(sentiment = "positive"), row)
#> [1] TRUE
match_sentiment(list(sentiment = "negative"), row)
#> [1] FALSE

# Called directly on two values
metric_exact_match(ignore_case = TRUE)("Paris ", "paris")
#> [1] TRUE