2017-12-05 18 views
1

tm 패키지를 사용하여 준비된 문서 용어 매트릭스에서 구조적 항목 모델 (stm 패키지 사용)을 실행하려고합니다. STM : tm에서 stm 문서 - 용어 행렬로 변환 할 때 메타 데이터를 유지하는 방법?

나는 다음과 같은 메타 데이터가 포함 tm 패키지의 코퍼스 구축 : 텍스트 청소를하고 (여전히 존재 메타 데이터) clean_corpus2로 결과를 저장 한 후

library(tm) 

myReader2 <- readTabular(mapping=list(content="text", id="id", sentiment = "sentiment")) 
text_corpus2 <- VCorpus(DataframeSource(bin_stm_df), readerControl = list(reader = myReader2)) 

meta(text_corpus2[[1]]) 
    id  : 11 
    sentiment: negative 
    language : en 

을, 나는 문서 용어로 변경 행렬 다음 stm 호환 매트릭스로 읽어, 그래서 좋은

library(stm) 

chat_DTM2 <- DocumentTermMatrix(clean_corpus2, control = list(wordLengths = c(3, Inf))) 
DTM2 <- removeSparseTerms(chat_DTM2 , 0.990) 
DTM_st <-readCorpus(DTM2, type = "slam") 

지금까지. 내가 stm 호환 데이터를 사용하여 메타 데이터를 지정하려고 할 때, 메타 데이터가 사라 :

docsTM <- DTM_st$documents # works fine 
vocabTM <- DTM_st$vocab # works fine 
metaTM <- DTM_st$meta # returns NULL 

> metaTM 
NULL 

가 어떻게 tm에서 메타 데이터 stm 호환 문서 장기 매트릭스에 코퍼스를 -generated 유지합니까? 어떤 제안이라도 환영합니다. 감사합니다.

답변

1

quanteda 패키지를 사용해 보는 것은 어떨까요? 개체에 액세스 할 수있는 기능없이

, 나는이 그대로 작동 보장 할 수 있지만, 그것은해야한다 :

library("quanteda") 

# creates the corpus with document variables except for the "text" 
text_corpus3 <- corpus(bin_stm_df, text_field = "text") 

# convert to document-feature matrix - cleaning options can be added 
# see ?tokens 
chat_DTM3 <- dfm(text_corpus3) 

# similar to tm::removeSparseTerms() 
DTM3 <- dfm_trim(chat_DTM3, sparsity = 0.990) 

# convert to STM format 
DTM_st <- convert(DTM3, to = "stm") 

# then it's all there 
docsTM <- DTM_st$documents 
vocabTM <- DTM_st$vocab  
metaTM <- DTM_st$meta  # should return the data.frame of document variables 
+1

안녕하세요, 내가 마지막에 그 알아 냈하지만, 여기에 좋은 답변을 게시 주셔서 감사합니다! –