## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  message = FALSE,
  warning = FALSE,
  comment = "#>"
)
source("cache-hook.R")

## ----libs---------------------------------------------------------------------
library(textpress)
library(dplyr)
library(DT)

## ----corpus, cache_rds = c("wiki_urls", "wiki_text_list")---------------------
# 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)

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

wiki_text |> distinct(doc_id, url)

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

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

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

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

## ----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)

## ----dict---------------------------------------------------------------------
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()

## ----search-------------------------------------------------------------------
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)

## ----counts-------------------------------------------------------------------
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))

