Embedding-based pairwise construct-redundancy screen
Source:R/embedding.R
tf_embedding_redundancy.RdFor every unordered pair of constructs, embeds each definition with the
supplied embedder and computes the cosine similarity (rounded to 6
decimals). The vectors of every compared pair must be of equal, nonzero
length, or the pair is refused. Returns a data frame sorted by descending
cosine then (a, b), flagging pairs at or above threshold for
review. This assistive screen complements the deterministic lexical
tf_redundancy_check(), and its results are only as reproducible as the
supplied embedder.
Arguments
- theory
A theory object (named list), e.g. from
tf_read().- embedder
A function mapping a definition string to a numeric vector.
- threshold
Cosine threshold for the
"review"flag; defaults to the checklist'sembedding_similarity_max(0.85). How high a cosine two unrelated definitions reach depends on the embedding model, so a threshold suited to one model may flag everything or nothing under another, and the value is best set for the model in use.
Examples
theory <- tf_theory("demo-1", "A demonstration theory") |>
tf_add_construct("c_arousal", "Arousal", "bodily activation") |>
tf_add_construct("c_threat", "Threat", "appraised danger")
# A toy deterministic embedder: bag-of-words counts over a fixed vocabulary.
vocab <- c("bodily", "activation", "appraised", "danger")
embedder <- function(def) {
words <- strsplit(tolower(def), "\\s+")[[1]]
vapply(vocab, function(w) sum(words == w), numeric(1))
}
tf_embedding_redundancy(theory, embedder)
#> a b cosine flag
#> 1 c_arousal c_threat 0 ok