2017-11-21 7 views
0

나는 다음과 같은 더미 데이터가 :R은 : 검사 문서 기간 매트릭스 오류가 발생합니다 현재 허용되지 않습니다 반복 인덱스

final6 <- data.frame(docname = paste0("doc", 1:6), 
        articles = c("Catalonia independence in matter of days", 
           "Anger over Johnson Libya bodies comment", 
           "Man admits frenzied mum and son murder", 
           "The headache that changed my life", 
           "Las Vegas killer sick, demented - Trump", 
           "Instagram baby photo scammer banned") 
) 

을 그리고 나는 나중에 링크 수 (이름을 문서화을 참조하여 DocumentTermMatrix을 만들려면 원본 기사 텍스트로). 이를 위해, 나는 this post에서 지시를 따르

myReader <- readTabular(mapping=list(content="articles", id="docname")) 
text_corpus <- VCorpus(DataframeSource(final6), readerControl = list(reader = myReader)) 

# define function that replaces ounctuation with spaces 
replacePunctuation <- content_transformer(function(x) {return (gsub("[[:punct:]]"," ", x))}) # replaces punctuation with empty spaces 

# remove customised words 
myWords <- c("ok", "chat", 'okay', 'day', 'today', "might", "bye", "hello", "thank", "you", "please", "sorry", "hello", "hi") 

# clean text 
cleantext <- function(corpus){ 
    clean_corpus <- tm_map(corpus, removeNumbers) 
    clean_corpus <- tm_map(clean_corpus, tolower) 
    clean_corpus <- tm_map(clean_corpus, PlainTextDocument) 
    clean_corpus <- tm_map(clean_corpus, replacePunctuation) 
    clean_corpus <- tm_map(clean_corpus, removePunctuation) 
    clean_corpus <- tm_map(clean_corpus, removeWords, c(stopwords("english"), myWords, top_names)) 
    clean_corpus <- tm_map(clean_corpus, stripWhitespace) 
    clean_corpus <- tm_map(clean_corpus, stemDocument, language = "english") 

    clean_corpus 
} 

clean_corpus <- cleantext(text_corpus) 

# create dtm 
chat_DTM <- DocumentTermMatrix(clean_corpus, control = list(wordLengths = c(3, Inf))) 

을 지금, 나는이 행렬을 검사 할 때, 나는 오류 얻을 :

inspect(chat_DTM) 

Error in [.simple_triplet_matrix (x, docs, terms) : Repeated indices currently not allowed.

가 공정하게를,이 오류도 발생 텍스트 만 기반으로 코퍼스를 만들고 doc id를 애트리뷰트로 전달하지 않는다. 문제의 원인은 무엇입니까?

답변

0

코퍼스에서 메타 데이터를 제거하는 기능이 PlainTextDocument 인 경우 문제가있었습니다. 다음과 같이 clean_text 기능을 수정하는 경우,이 반환 된 오류없이 검사 할 수있는 깨끗한 DTM 결과 :

cleantext <- function(corpus){ 
    clean_corpus <- tm_map(corpus, removeNumbers) 
    clean_corpus <- tm_map(clean_corpus, content_transformer(tolower)) #!! modified 
    #clean_corpus <- tm_map(clean_corpus, PlainTextDocument) ### !!!! PlainTextDocument function erases metadata from corpus = document id! So this needs to be erased 
    clean_corpus <- tm_map(clean_corpus, replacePunctuation) 
    clean_corpus <- tm_map(clean_corpus, removePunctuation) 
    clean_corpus <- tm_map(clean_corpus, removeWords, c(stopwords("english"), myWords, top_names)) 
    clean_corpus <- tm_map(clean_corpus, stripWhitespace) 
    clean_corpus <- tm_map(clean_corpus, stemDocument, language = "english") 

    clean_corpus 
} 

clean_corpus <- cleantext(text_corpus) 


chat_DTM2 <- DocumentTermMatrix(clean_corpus) 
inspect(chat_DTM2) 

대답이 solution에서 영감을했다. 감사!

관련 문제