Abbreviations

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.

library(textpress)
library(dplyr)
library(DT)

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)

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. For a fixed vocabulary, see the dictionary search vignette.

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