R/clean_source.R
, R/clean_sources.R
, R/search_and_replace_in_source.R
, and 1 more
cleaning_sources.Rd
These functions can be used to 'clean' one or more sources or perform search and replace taks. Cleaning consists of two operations: splitting the source at utterance markers, and conducting search and replaces using regular expressions.
clean_source( input, output = NULL, replacementsPre = rock::opts$get(replacementsPre), replacementsPost = rock::opts$get(replacementsPost), extraReplacementsPre = NULL, extraReplacementsPost = NULL, removeNewlines = FALSE, utteranceSplits = rock::opts$get(utteranceSplits), preventOverwriting = rock::opts$get(preventOverwriting), encoding = rock::opts$get(encoding), silent = rock::opts$get(silent) ) clean_sources( input, output, outputPrefix = "", outputSuffix = "_cleaned", recursive = TRUE, filenameRegex = ".*", replacementsPre = rock::opts$get(replacementsPre), replacementsPost = rock::opts$get(replacementsPost), extraReplacementsPre = NULL, extraReplacementsPost = NULL, removeNewlines = FALSE, utteranceSplits = rock::opts$get(utteranceSplits), preventOverwriting = rock::opts$get(preventOverwriting), encoding = rock::opts$get(encoding), silent = rock::opts$get(silent) ) search_and_replace_in_source( input, replacements = NULL, output = NULL, preventOverwriting = TRUE, encoding = "UTF-8", silent = FALSE ) search_and_replace_in_sources( input, output, replacements = NULL, outputPrefix = "", outputSuffix = "_postReplacing", preventOverwriting = rock::opts$get(preventOverwriting), recursive = TRUE, filenameRegex = ".*", encoding = rock::opts$get(encoding), silent = FALSE )
input | For |
---|---|
output | For |
replacementsPre, replacementsPost | Each is a list of two-element vectors,
where the first element in each vector contains a regular expression to search for
in the source(s), and the second element contains the replacement (these are passed
as |
extraReplacementsPre, extraReplacementsPost | To perform more replacements
than the default set, these can be conveniently specified in |
removeNewlines | Whether to remove all newline characters from the source before starting to clean them. |
utteranceSplits | This is a vector of regular expressions that specify where to
insert breaks between utterances in the source(s). Such breakes are specified using
|
preventOverwriting | Whether to prevent overwriting of output files. |
encoding | The encoding of the source(s). |
silent | Whether to suppress the warning about not editing the cleaned source. |
outputPrefix, outputSuffix | The prefix and suffix to add to the filenames when writing the processed files to disk. |
recursive | Whether to search all subdirectories ( |
filenameRegex | A regular expression to match against located files; only files matching this regular expression are processed. |
replacements | The strings to search & replace, as a list of two-element vectors,
where the first element in each vector contains a regular expression to search for
in the source(s), and the second element contains the replacement (these are passed
as |
A character vector for clean_source
, or a list of character vectors,
for clean_sources
.
The cleaning functions, when called with their default arguments, will do the following:
Double periods (..
) will be replaced with single periods (.
)
Four or more periods (...
or .....
) will be replaced with three periods
Three or more newline characters will be replaced by one newline character (which will become more, if the sentence before that character marks the end of an utterance)
All sentences will become separate utterances (in a semi-smart manner; specifically, breaks in speaking, if represented by three periods, are not considered sentence ends, wheread ellipses ("…" or unicode 2026, see the example) are.
If there are comma's without a space following them, a space will be inserted.
exampleSource <- "Do you like icecream? Well, that depends\u2026 Sometimes, when it's..... Nice. Then I do, but otherwise... not really, actually." ### Default settings: cat(clean_source(exampleSource));#> Do you like icecream? #> #> Well, that depends… #> Sometimes, when it's... Nice. #> Then I do, #> but otherwise... not really, actually.#> Do you like icecream? #> Well, that depends… #> Sometimes, when it's... Nice. #> Then I do, but otherwise... not really, actually.exampleSource <- "Do you like icecream? Well, that depends\u2026 Sometimes, when it's..... Nice. Then I do, but otherwise... not really, actually." ### Simple text replacements: cat(search_and_replace_in_source(exampleSource, replacements=list(c("\u2026", "..."), c("Nice", "Great"))));#> Do you like icecream? #> #> #> Well, that depends... Sometimes, when it's..... Great. Then I do, #> but otherwise... not really, actually.### Using a regular expression to capitalize all words following ### a period: cat(search_and_replace_in_source(exampleSource, replacements=list(c("\\.(\\s*)([a-z])", ".\\1\\U\\2"))));#> Do you like icecream? #> #> #> Well, that depends… Sometimes, when it's..... Nice. Then I do, #> but otherwise... Not really, actually.