Quanteda 包提供稀疏文档特征矩阵 DFM,其方法包含removeFeatures。我试图dfm(x, removeFeatures="\\b[a-z]{1-3}\\b")
删除太短的单词以及dfm(x, keptFeatures="\\b[a-z]{4-99}\\b")
保留足够长的单词但不起作用,基本上是在做同样的事情,即删除太短的单词。
如何从 Quanteda DFM 对象中删除正则表达式匹配?
例子。
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
示例 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)