2013-05-06 3 views
1

2 개의 텍스트 문서에 대해 주제 모델을 작성하기 위해 LDA를 사용했습니다. A와 B는 A와 B가 매우 관련이 있으며, B는 지리 정보 과학과 관련이 있습니다. 그런 다음이 명령을 사용하여 LDA 훈련 : R 주제 모델링 : lda 모델 레이블 기능

 text<- c(A,B) # introduced above 
    r <- Corpus(VectorSource(text)) # create corpus object 
    r <- tm_map(r, tolower) # convert all text to lower case 
    r <- tm_map(r, removePunctuation) 
    r <- tm_map(r, removeNumbers) 
    r <- tm_map(r, removeWords, stopwords("english")) 
    r.dtm <- TermDocumentMatrix(r, control = list(minWordLength = 3))  
    my_lda <- LDA(r.dtm,2) 

이제 내가 새 문서의 내용을 예측하기 my_lda를 사용하고자하는 C라고하고 나는 그것이 컴퓨터 과학 또는 지리 과학 관련이 있는지보고 싶어요. 내가 예측

에 대한
 x<-C# a new document (a long string) introduced above for prediction 
    rp <- Corpus(VectorSource(x)) # create corpus object 
    rp <- tm_map(rp, tolower) # convert all text to lower case 
    rp <- tm_map(rp, removePunctuation) 
    rp <- tm_map(rp, removeNumbers) 
    rp <- tm_map(rp, removeWords, stopwords("english")) 
    rp.dtm <- TermDocumentMatrix(rp, control = list(minWordLength = 3))  
    test.topics <- posterior(my_lda,rp.dtm) 

를이 코드를 사용하면 내가 어떻게 그것이 컴퓨터 과학을 의미하는 경우 실현할 수 있습니다 ... 그것은 나에게 라벨 1 또는 2를 줄 것이다 나는 1 또는 2가 무엇을 나타내는 지 어떤 생각이없는 알고 관련 또는 지리학 관련?

+0

어떤 패키지를 사용하고 있습니까? – Carson

+0

tm 및 topicmodels –

답변

1

LDA 주제 모델에서 가장 가능성있는 용어를 추출하고 원하는 블랙 박스 숫자 이름으로 바꿀 수 있습니다. 귀하의 예는 재현 할 수 없지만 여기에 어떻게 할 수 있는지 보여주는 예가 있습니다 :

> library(topicmodels) 
> data(AssociatedPress) 
> 
> train <- AssociatedPress[1:100] 
> test <- AssociatedPress[101:150] 
> 
> train.lda <- LDA(train,2) 
> 
> #returns those black box names 
> test.topics <- posterior(train.lda,test)$topics 
> head(test.topics) 
       1   2 
[1,] 0.57245696 0.427543038 
[2,] 0.56281568 0.437184320 
[3,] 0.99486888 0.005131122 
[4,] 0.45298547 0.547014530 
[5,] 0.72006712 0.279932882 
[6,] 0.03164725 0.968352746 
> #extract top 5 terms for each topic and assign as variable names 
> colnames(test.topics) <- apply(terms(train.lda,5),2,paste,collapse=",") 
> head(test.topics) 
    percent,year,i,new,last new,people,i,soviet,states 
[1,]    0.57245696    0.427543038 
[2,]    0.56281568    0.437184320 
[3,]    0.99486888    0.005131122 
[4,]    0.45298547    0.547014530 
[5,]    0.72006712    0.279932882 
[6,]    0.03164725    0.968352746 
> #round to one topic if you'd prefer 
> test.topics <- apply(test.topics,1,function(x) colnames(test.topics)[which.max(x)]) 
> head(test.topics) 
[1] "percent,year,i,new,last" "percent,year,i,new,last" "percent,year,i,new,last" 
[4] "new,people,i,soviet,states" "percent,year,i,new,last" "new,people,i,soviet,states" 
관련 문제