-
-
Save Libardo1/4a8748e9b13509129bda to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ngrams.tokenizer <- function(x, n = 2) { | |
| trim <- function(x) gsub("(^\\s+|\\s+$)", "", x) | |
| terms <- strsplit(trim(x), split = "\\s+")[[1]] | |
| ngrams <- vector() | |
| if (length(terms) >= n) { | |
| for (i in n:length(terms)) { | |
| ngram <- paste(terms[(i-n+1):i], collapse = " ") | |
| ngrams <- c(ngrams,ngram) | |
| } | |
| } | |
| ngrams | |
| } | |
| ngrams.tokenizer(" this is a sentense to be ngrammized", 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment