How to remove words that start with digits from tokens in quanteda? Sample words: 21st, 80s, 8th, 5k, but they can be completely different and I don't know them in advance.
I have a data frame with english sentences. I transformed it to corpus by using quanteda. Next I transformed corpus to tokens and I did some cleaning like remove_punct
, remove_symbols
, remove_numbers
, etc. However, the remove_numbers
function does not delete words that start with digits. I would like to delete such words, but I don't know their exact form - it can be e.g. 21st, 22nd, etc.
library("quanteda")
data = data.frame(
text = c("R is free software and 2k comes with ABSOLUTELY NO WARRANTY.",
"You are welcome to redistribute it under 80s certain conditions.",
"Type 'license()' or 21st 'licence()' for distribution details.",
"R is a collaborative 6th project with many contributors.",
"Type 'contributors()' for more information and",
"'citation()' on how to cite R or R packages in publications."),
stringsAsFactors = FALSE
)
corp = corpus(data, text_field = "text")
toks = tokens(corp, remove_punct = TRUE, remove_symbols = TRUE, remove_numbers = TRUE,
remove_separators = TRUE, split_hyphens = TRUE)
dfmat = dfm(toks, tolower = TRUE, stem = TRUE, remove = stopwords("english"))
You just need to delete them explicitly since they are not managed by remove_numbers = TRUE
. Just use a simple regular expression which looks for some digits before a character. In the example below, I look for a sequence of digits between 1 and 5 (e.g. (?<=\\d{1,5}
). You can adjust the two numbers to fine tune your regular expression.
Here is the example which only uses quanteda but adds tokens_remove()
explicitly.
library("quanteda")
#> Package version: 2.0.0
#> Parallel computing: 2 of 8 threads used.
#> See https://quanteda.io for tutorials and examples.
#>
#> Attaching package: 'quanteda'
#> The following object is masked from 'package:utils':
#>
#> View
data = data.frame(
text = c("R is free software and 2k comes with ABSOLUTELY NO WARRANTY.",
"You are welcome to redistribute it under 80s certain conditions.",
"Type 'license()' or 21st 'licence()' for distribution details.",
"R is a collaborative 6th project with many contributors.",
"Type 'contributors()' for more information and",
"'citation()' on how to cite R or R packages in publications."),
stringsAsFactors = FALSE
)
corp = corpus(data, text_field = "text")
toks = tokens(corp, remove_punct = TRUE, remove_symbols = TRUE, remove_numbers = TRUE,
remove_separators = TRUE, split_hyphens = TRUE)
toks = tokens_remove(toks, pattern = "(?<=\\d{1,5})\\w+", valuetype = "regex" )
dfmat = dfm(toks, tolower = TRUE, stem = TRUE, remove = stopwords("english"))
Created on 2020-05-03 by the reprex package (v0.3.0)