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.
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
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.
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.
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.