R/utilities.R

Defines functions has_usable_data split_and_replace

# Copyright (C) 2018- Ioannis Kosmidis

## firstup <- function(x) {
##     stri_trans_totitle(x, opts_brkiter = stri_opts_brkiter(type = "sentence"))
## }

split_and_replace <- function(string, base_patterns, replacement,
                              nparts = ceiling(length(base_patterns)/5), fixed_word = TRUE) {
    ns <- length(base_patterns)
    np <- floor(ns/nparts)
    u <- c(rep(np, nparts - 1), ns - np * (nparts - 1))
    parts <- rep(seq.int(nparts), u)
        if (fixed_word) {
            out <- lapply(split(base_patterns, parts), function(x) {
                paste0("(?i)\\b", x, "\\b", collapse = "|")
            })
        }
        else {
            out <- lapply(split(base_patterns, parts), function(x) {
                paste0("(?i)", x, collapse = "|")
            })
        }
    for (j in seq_along(out)) {
        string <- str_replace_all(string, out[[j]], replacement)
        }
    string
}

has_usable_data <- function(x) {
    !(isTRUE(length(x$nodes) == 0) & isTRUE(length(x$edges) == 0))
}

Try the cranly package in your browser

Any scripts or data that you put into this service are public.

cranly documentation built on Aug. 27, 2022, 1:07 a.m.