2013-05-06 82 views
1

我使用LDA为2个文本文档建立主题模型,称为A和B.文档A与计算机科学高度相关,文档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 2013-05-06 14:47:12

+0

tm和topicmodels – 2013-05-06 19:44:07

回答

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"