Quanteda package provides the sparse document-feature matrix DFM and its methods contain removeFeatures. I have tried dfm(x, removeFeatures="\\b[a-z]{1-3}\\b")
to remove too short words as well as dfm(x, keptFeatures="\\b[a-z]{4-99}\\b")
to preserve sufficiently long words but not working, basically doing the same thing i.e. removing too short words.
How can I remove a regex match from a Quanteda DFM object?
Example.
myMatrix <-dfm(myData, ignoredFeatures = stopwords("english"),
stem = TRUE, toLower = TRUE, removeNumbers = TRUE,
removePunct = TRUE, removeSeparators = TRUE, language = "english")
#
#How to use keptFeatures/removeFeatures here?
#Instead of RemoveFeatures/keptFeatures methods, I tried it like this but not working
x<-unique(gsub("\\b[a-zA-Z0-9]{1,3}\\b", "", colnames(myMatrix)));
x<-x[x!=""];
mmyMatrix<-myMatrix;
colnames(mmyMatrix) <- x
Sample DFM
myData <- c("a aothu oat hoah huh huh huhhh h h h n", "hello h a b c d abc abcde", "hello hallo hei hej", "Hello my name is hhh.")
myMatrix <- dfm(myData)