2017-01-11 6 views
3

Quantedaパッケージは、疎なドキュメントフィーチャマトリックスDFMを提供し、そのメソッドにはremoveFeaturesが含まれています。私はdfm(x, removeFeatures="\\b[a-z]{1-3}\\b")と短い単語を削除するだけでなく、十分に長い単語を保存するのに、基本的には同じこと、つまり短すぎる単語を削除するように、dfm(x, keptFeatures="\\b[a-z]{4-99}\\b")を削除しようとしました。R:Quanteda DFM、希薄ドキュメントフィーチャマトリックス、オブジェクトからの正規表現の削除?

Quantuma 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) 
+0

たぶんdfm_select 'のようなもの(myMatrix、「^ [[:alnum: ]] {1,3} $ "、" remove "、valuetype =" regex ")'? –

答えて

1

それは> = v0.9.9で、dfm_selectです:

myMatrix 
## Document-feature matrix of: 4 documents, 22 features (70.5% sparse). 

dfm_select(myMatrix, "\\b[a-zA-Z0-9]{1,3}\\b", selection = "keep", valuetype = "regex") 
## kept 14 features, from 1 supplied (regex) feature types 
## Document-feature matrix of: 4 documents, 14 features (71.4% sparse). 
## 4 x 14 sparse Matrix of class "dfmSparse" 
##  features 
## docs a oat huh h n b c d abc hei hej my is hhh 
## text1 1 1 2 3 1 0 0 0 0 0 0 0 0 0 
## text2 1 0 0 1 0 1 1 1 1 0 0 0 0 0 
## text3 0 0 0 0 0 0 0 0 0 1 1 0 0 0 
## text4 0 0 0 0 0 0 0 0 0 0 0 1 1 1 
関連する問題