
Sentiment analysis with RNN or transformer on tokens
Source:R/geron_w4d_native.R
morie_geron_sentiment_analysis.RdOrchestrates tokenise -> caller model -> softmax -> (optional) counted accuracy/confusion/precision/recall/F1.
Usage
morie_geron_sentiment_analysis(
texts,
model,
tokenizer = NULL,
y_true = NULL,
labels = NULL
)Arguments
- texts
Character vector of documents.
- model
Function(tokens) -> numeric scores, length K >= 2, constant across texts.
- tokenizer
Optional function(text) -> character vector of tokens; default lowercase+split.
- y_true
Optional integer gold labels in 0..K-1.
- labels
Optional character vector of class names, length K.
Value
list with probabilities, predicted, tokens, labels, accuracy, confusion, precision, recall, f1, macro_f1, n_classes, estimate, n, method.
Examples
model <- function(toks) {
s <- sum(toks %in% c("good", "great")) - sum(toks %in% c("bad", "awful"))
c(-s, s)
}
texts <- c("a good great film", "an awful bad mess")
r <- morie_geron_sentiment_analysis(texts, model, y_true = c(1L, 0L))
str(r, max.level = 1)
#> List of 14
#> $ probabilities: num [1:2, 1:2] 0.018 0.982 0.982 0.018
#> $ predicted : int [1:2] 1 0
#> $ tokens :List of 2
#> $ labels : NULL
#> $ accuracy : num 1
#> $ confusion : int [1:2, 1:2] 1 0 0 1
#> $ precision : num [1:2] 1 1
#> $ recall : num [1:2] 1 1
#> $ f1 : num [1:2] 1 1
#> $ macro_f1 : num 1
#> $ n_classes : int 2
#> $ estimate : num 1
#> $ n : int 2
#> $ method : chr "Tokenise -> model scores -> softmax, with counted accuracy / confusion / macro-F1"