Find words in a corpus based on lemma

733 Views Asked by At

I am doing text mining with R and I get an "issue" I would like to solve... In order to find the reports in corpus that contain the most a given word or expression, I use kwicfunction from quantedapackage like this :

result <- kwic (corp2,c(phrase("trous oblongs")))

where corp2is a corpus. trous oblongsis in french and it is a plural. When I do this however, I will only get the reports containing the expression at the plural. I would also like to take into account the occurences of the singular form trou oblong(and vice versa if I initially put in the code trou oblong, get the plural also).

I know that udpipepackage, thanks to its udpipe_annotate function :https://www.rdocumentation.org/packages/udpipe/versions/0.3/topics/udpipe_annotate, is able to extract the lemma of the words in the text.

So I would like to know if udpipe has a function that could manage to find all the occurences of the words having the same lemma in a corpus, or if it possible to do that with kwic.

Thanks in advance

2

There are 2 best solutions below

0
On BEST ANSWER

Quanteda has tokens_wordstem() which uses SnoballC's stemmer:

toks <- tokens(corp2)
toks_stem <- tokens_wordstem(toks, "french")
kwic(toks_stem, phrase("trous oblong"))

Alternatively, you can also use * wildcard to search for stems:

toks <- tokens(corp2)
kwic(toks, phrase("trou* oblong*"))
0
On

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 = " ")