util_find_abbreviations() finds abbreviations that a text defines for itself and pairs each short form with its long form. Parenthetical definitions – “tumor necrosis factor (TNF)” or “TNF (tumor necrosis factor)” – are found with the Schwartz & Hearst (2003) algorithm; taxonomic abbreviations like “E. coli” are matched to “Escherichia coli” elsewhere in the same document. This vignette runs it over a set of biomedical Wikipedia articles, looks at short forms shared across documents, and then uses the pairs as a dictionary so that a concept is counted whether it is written out or abbreviated.

Load packages.

Corpus

Fetch Wikipedia articles on inflammation and gut bacteria, then scrape the article bodies.

wiki_urls <- c(
  textpress::fetch_wiki_urls("tumor necrosis factor inflammation", limit = 4),
  textpress::fetch_wiki_urls("Escherichia coli gut microbiota", limit = 4)
)

wiki_text_list <- textpress::read_urls(wiki_urls, exclude_wiki_refs = TRUE)

read_urls() returns one row per text node. Give each article an integer doc_id.

wiki_text <- wiki_text_list$text |>
  mutate(doc_id = match(url, unique(url))) |>
  relocate(doc_id, .before = 1)

wiki_text |> distinct(doc_id, url)
#>    doc_id
#>     <int>
#> 1:      1
#> 2:      2
#> 3:      3
#> 4:      4
#> 5:      5
#> 6:      6
#> 7:      7
#> 8:      8
#>                                                                                url
#>                                                                             <char>
#> 1:                             https://en.wikipedia.org/wiki/Tumor_necrosis_factor
#> 2:                 https://en.wikipedia.org/wiki/Tumor_necrosis_factor_superfamily
#> 3:                          https://en.wikipedia.org/wiki/TNF_receptor_superfamily
#> 4:                                      https://en.wikipedia.org/wiki/Inflammation
#> 5:                                    https://en.wikipedia.org/wiki/Gut_microbiota
#> 6:                                  https://en.wikipedia.org/wiki/Escherichia_coli
#> 7: https://en.wikipedia.org/wiki/Shigatoxigenic_and_verotoxigenic_Escherichia_coli
#> 8:                                       https://en.wikipedia.org/wiki/Escherichia

Find abbreviations

util_find_abbreviations() pools all rows that share the by values into one unit, so with by = "doc_id" each article is searched whole: a definition in one paragraph pairs with a full name in another. It returns one row per unique short_form/long_form pair per unit, along with the method that found it.

abbrevs <- wiki_text |>
  textpress::util_find_abbreviations(by = "doc_id")

abbrevs |>
  DT::datatable(rownames = FALSE, options = list(pageLength = 10))

Run one method on its own with methods. The taxonomic pairs read as a species list for the corpus.

wiki_text |>
  textpress::util_find_abbreviations(by = "doc_id", methods = "taxonomic") |>
  DT::datatable(rownames = FALSE, options = list(pageLength = 10))

Shared short forms

Short forms defined in more than one article.

abbrevs |>
  distinct(doc_id, short_form) |>
  count(short_form, name = "n_docs", sort = TRUE) |>
  filter(n_docs > 1) |>
  DT::datatable(rownames = FALSE)

The same short form does not always get the same long form. Different authors spell out SCFAs as “short-chain”, “short chain” or “small chain fatty acids”, and STEC appears as both “Shiga toxin-producing E. coli” and “Shigatoxigenic Escherichia coli”. Listing every short form with more than one long form is a quick way to find these variants.

abbrevs |>
  distinct(short_form, long_form) |>
  group_by(short_form) |>
  filter(n_distinct(tolower(long_form)) > 1) |>
  arrange(short_form) |>
  DT::datatable(rownames = FALSE)

Once an article defines TNF, it rarely writes out “tumor necrosis factor” again, so a search for the long form alone undercounts. Stack the parenthetical pairs into a dictionary in which both forms point to one concept. Where an article spells out a short form more than one way, the first long form becomes the concept and the others become variants of it.

pairs <- abbrevs |>
  filter(method == "parenthetical") |>
  group_by(doc_id, short_form) |>
  mutate(concept = tolower(first(long_form))) |>
  ungroup()

abbrev_dict <- bind_rows(
  pairs |> transmute(doc_id, form = "short_form", variant = short_form,
                     concept),
  pairs |> transmute(doc_id, form = "long_form", variant = long_form,
                     concept)
) |>
  distinct()

Split into sentences and run one search_dict() pass over every variant. search_dict() matches without regard to case, which is right for long forms but not for short forms: IS (insertion sequence) would match every “is”. Keep short-form hits only where the case matches, and join each hit to the definitions from its own document.

abbrev_ss <- wiki_text |>
  textpress::nlp_split_sentences(by = c("doc_id", "node_id"))

hits <- abbrev_ss |>
  textpress::search_dict(
    by    = c("doc_id", "node_id", "sentence_id"),
    terms = unique(abbrev_dict$variant)
  ) |>
  mutate(doc_id = as.integer(sub("\\..*$", "", id))) |>
  inner_join(abbrev_dict |> mutate(term = tolower(variant)),
             by = c("doc_id", "term"), relationship = "many-to-many") |>
  filter(form == "long_form" | ngram == variant)

Mentions per concept, split by how they were written. Across the corpus, short forms account for most of the mentions.

hits |>
  distinct(id, start, doc_id, concept, form) |>
  group_by(doc_id, concept) |>
  summarise(
    long_form  = sum(form == "long_form"),
    short_form = sum(form == "short_form"),
    total      = n(),
    .groups    = "drop"
  ) |>
  arrange(desc(total)) |>
  DT::datatable(rownames = FALSE, options = list(pageLength = 10))

Summary

util_find_abbreviations() turns the definitions a text already contains into a short_form/long_form table, using only the text itself and no external dictionary. Listing the pairs by short form exposes spelling variants and noisy matches to clean up; stacking them into a dictionary for search_dict() lets a concept be counted whether it is written out or abbreviated.

Schwartz, A. S., & Hearst, M. A. (2003). A simple algorithm for identifying abbreviation definitions in biomedical text. Pacific Symposium on Biocomputing, 451-462.