epub_recombine: Recombine text sections

View source: R/recombine.R

epub_recombineR Documentation

Recombine text sections


Split and recombine EPUB text sections based on regular expression pattern matching.


epub_recombine(data, pattern, sift = NULL)



a data frame created by epub.


character, a regular expression.


NULL or a named list of parameters passed to epub_sift. See details.


This function takes a regular expression and uses it to determine new break points for the full e-book text. This is particularly useful when sections pulled from EPUB metadata have arbitrary breaks and the text contains meaningful breaks at random locations in various sections. epub_recombine collapses the text and then creates a new nested data frame containing new chapter/section labels, word counts and character counts, associated with the text based on the new break points.

Usefulness depends on the quality of the e-book. While this function exists to improve the data structure of e-book content parsed from e-books with poor metadata formatting, it still requires original formatting that will at least allow such an operation to be successful, specifically a consistent, non-ambiguous regular expression pattern. See examples below using the built in e-book dataset.

When used in conjunction with epub_sift via the sift argument, recombining and resifting is done recursively. This is because it is possible that sifting can create a need to rerun the recombine step in order to regenerate proper chapter indexing for the section column. However, recombining a second time does not lead to a need to resift, so recursion ends after one round regardless.

This is a convenient way to avoid the syntax:

epub_recombine([args]) %>% epub_sift([args]) %>% epub_recombine([args]).


a data frame

See Also



file <- system.file("dracula.epub", package = "epubr")
x <- epub(file) # parse entire e-book
x$data[[1]] # note arbitrary section breaks (not between chapters)

pat <- "CHAPTER [IVX]+" # but a reliable pattern exists for new breaks
epub_recombine(x, pat) # not as expected; pattern also in table of contents

epub_recombine(x, pat, sift = list(n = 1000)) # sift low word-count sections

epubr documentation built on Sept. 10, 2023, 9:06 a.m.