2016-07-05 90 views
1

我正在嘗試對推文進行情感分析。雖然這樣做的話的前處理,創造一個矩陣,我得到了以下錯誤:r中的詞幹:缺失值

Error in if (any(lens > lim)) stop("There is a limit of ", lim, "characters on the number of characters in a word being stemmed") : 
missing value where TRUE/FALSE needed 

從14215個鳴叫,我煮下來到產生錯誤的具體鳴叫,但都沒有得到線索如何防止再次發生此錯誤。 由於其出錯的鳴叫是(和代碼重現錯誤):

library(RTextTools) 
tweet<-"demonio leg edge sexy we get it u vape PLEASE COME TO NA SOON I HAVE A LUCIEL READY FOR U dominos" 
all_tweets= create_matrix(tweet, language="english", minWordLength = 3, 
         removeStopwords=TRUE, removeNumbers=TRUE, # we can also removeSparseTerms 
         stemWords=TRUE,removePunctuation = TRUE,removeSparseTerms = 0) 

首先,我想理解的錯誤 - 爲什麼會發生,然後我的願望是這將使我的方法防止發生此錯誤 - 通過選擇和刪除這樣的推文或通過編輯我的create_matrix函數?

+0

嗨,哪個版本你有(R/RTextTools包)。在Windows 64,R 3.2.2和RTextTools 1.4.2上,我無法在您提供的文本上重現錯誤。 –

+0

我正在使用R版本3.3.0和RTextTools 1.4.2 – user3109578

+0

也許是區域設置或編碼的問題。 @lukeA建議似乎也是一種可能性。無論如何,你的代碼在這裏很有魅力。 –

回答

1

這個錯誤來自執行

wordStem(
    c("demonio", "leg", "edge", "sexy", 
    "get", "u", "vape", "please", 
    "come", NA, "soon", "luciel", 
    "ready", "u", "dominos") 
) 
# Error in if (any(lens > lim)) stop("There is a limit of ", lim, "characters on the number of characters in a word being stemmed") : 
# missing value where TRUE/FALSE needed 

也許這是一個錯誤。字符串「NA」似乎被標記爲NA(缺失值)。

作爲一種變通方法,使用

library(tm) 
all_tweets <- DocumentTermMatrix(
    Corpus(VectorSource(tweet)), 
    control = list(
    wordLengths = c(3, Inf), 
    stopwords=TRUE, 
    removeNumbers=TRUE, 
    stemming=TRUE, 
    removePunctuation = TRUE 
) 
) 

sessionInfo()

R version 3.3.0 (2016-05-03) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 
Running under: Windows 7 x64 (build 7601) Service Pack 1 

locale: 
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 LC_MONETARY=German_Germany.1252 
[4] LC_NUMERIC=C     LC_TIME=German_Germany.1252  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] RTextTools_1.4.2 SparseM_1.7  

loaded via a namespace (and not attached): 
[1] Rcpp_0.12.5   splines_3.3.0  MASS_7.3-44   tau_0.0-18   prodlim_1.5.5  tm_0.6-2   
[7] lattice_0.20-33  foreach_1.4.3  caTools_1.17.1  tools_3.3.0   nnet_7.3-11   parallel_3.3.0  
[13] grid_3.3.0   ipred_0.9-5   glmnet_2.0-5  e1071_1.6-7   iterators_1.0.8  class_7.3-14  
[19] survival_2.39-4  randomForest_4.6-12 Matrix_1.2-6  NLP_0.1-9   lava_1.4.3   bitops_1.0-6  
[25] codetools_0.2-14 rsconnect_0.4.3  maxent_1.3.3.1  rpart_4.1-10  slam_0.1-32   tree_1.0-36 
+0

是的。鍛鍊完美(但需要庫(SnowballC))。 – user3109578