R/util.R

Defines functions remove_stopwords tokenize

Documented in remove_stopwords tokenize

## ceeboo 2008

tokenize <-
function(x, lines = FALSE, eol = "\n")
{
    if (!is.character(x))
        stop("'x' not a character")
    if (lines)
        x <- gsub("$", eol, x, perl = TRUE)
    # protect infix punctuation
    x <- unlist(strsplit(x, "\\b(?<!^|\\w[[:punct:]])(?![[:punct:]]\\w)",
                perl = TRUE))
    # split non-words
    i <- grep("^\\W+$", x, perl = TRUE)
    if (length(i)) {
        x <- as.list(x)
        x[i] <- lapply(x[i], strsplit, "\\B", perl = TRUE)
        return (unlist(x))
    }
    x
}

remove_stopwords <-
function(x, words, lines = FALSE)
{
    if (!is.character(x))
        stop("'x' not a character")
    if (!is.character(words))
        stop("'words' not a character")
    if (lines) {
        ## FIXME perl prefers partial matches. For now
	##       we protect hyphenated words and infix
	##       apostrophes.
        words <- paste("\\b(?<!-)(", paste(words, collapse = "|"),
            ")(?!(-|'\\b))\\b", sep = "", collapse = "")
        x <- gsub(words, "", x, ignore.case = TRUE, perl = TRUE)
    } else {
        i <- match(tolower(x), tolower(words))
        x <- x[is.na(i)]
    }
    x
}

Try the tau package in your browser

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

tau documentation built on July 21, 2021, 5:07 p.m.