If you want to stick in the udpipe framework, you can either use txt_nextgram
with txt_recode_ngram
or use the dependency parsing results if your 2 terms do not follow one another but you still want to find it.
library(udpipe)
library(data.table)
txt <- c("Les trous sont oblongs.",
"Les trous oblongs du systeme de montage des deux parties permettent un reglage facile et un alignement precis.")
## Annotate with udpipe to tokenise, obtain pos tags, lemmas, dependency parsing output
udmodel <- udpipe_download_model("french-sequoia", udpipe_model_repo = "bnosac/udpipe.models.ud")
udmodel <- udpipe_load_model(udmodel$file_model)
x <- udpipe_annotate(udmodel, txt)
x <- as.data.table(x)
## Situation 1: words are following one another
x <- x[, lemma_bigram := txt_nextgram(lemma, n = 2, sep = " "), by = list(doc_id, paragraph_id, sentence_id)]
subset(x, lemma_bigram %in% c("trous oblong"))
## Situation 2: words are not following one another - use dependency parsing results
x <- merge(x,
x[, c("doc_id", "paragraph_id", "sentence_id", "token_id", "token", "lemma", "upos", "xpos"), with = FALSE],
by.x = c("doc_id", "paragraph_id", "sentence_id", "head_token_id"),
by.y = c("doc_id", "paragraph_id", "sentence_id", "token_id"),
all.x = TRUE, all.y = FALSE,
suffixes = c("", "_parent"),
sort = FALSE)
subset(x, lemma_bigram %in% c("trous oblong") | (lemma %in% "trous" & lemma_parent %in% "oblong"))
If you want to recode keywords to 1 term (only covers situation 1):
x$term <- txt_recode_ngram(x$lemma, compound = "trous oblong", ngram = 2, sep = " ")