## ----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 <- textpress::fetch_wiki_urls("Strauss Howe generational theory", limit = 5) # # wiki_text_list <- textpress::read_urls(wiki_urls, exclude_wiki_refs = TRUE) ## ----corpus-rows-------------------------------------------------------------- corpus <- wiki_text_list$text nrow(corpus) ## ----split-sentences---------------------------------------------------------- web_ss <- corpus |> textpress::nlp_split_sentences(by = c("doc_id", "node_id")) web_ss |> slice(1:5) |> mutate(text = { words <- strsplit(text, "\\s+") sapply(words, function(w) paste(paste(w[seq_len(min(15, length(w)))], collapse = " "), "...")) }) |> DT::datatable(rownames = FALSE) ## ----tokenize----------------------------------------------------------------- tokens <- web_ss |> textpress::nlp_tokenize_text( by = c("doc_id", "node_id", "sentence_id"), method = 'biber', include_spans = TRUE ) # tokens list: first unit head(tokens$tokens[[1]]) # spans matrix: matching start/end positions head(tokens$spans[[1]]) ## ----cast--------------------------------------------------------------------- token_df <- tokens |> textpress::nlp_cast_tokens() token_df |> slice(1:20) |> DT::datatable(rownames = FALSE) ## ----pipeline----------------------------------------------------------------- corpus |> textpress::nlp_split_sentences(by = c("doc_id", "node_id")) |> textpress::nlp_tokenize_text( by = c("doc_id", "node_id", "sentence_id"), include_spans = TRUE ) |> textpress::nlp_cast_tokens()