美文网首页R for statistics
R文本挖掘:文本主题分析topic analysis

R文本挖掘:文本主题分析topic analysis

作者: Codewar | 来源:发表于2021-01-25 18:55 被阅读0次

    对于海量未知内容文本的挖掘,主题分析是一个常见的技巧,在主题模型中,主题表示一个概念、一个方面,表现为一系列相关的单词,是这些单词的条件概率。形象来说,主题就是一个桶,里面装了出现概率较高的单词,这些单词与这个主题有很强的相关性。

    今天要写的就是LDA(Latent Dirichlet Allocation)主题模型

    The basic assumption behind LDA is that each of the documents in a collection consist of a mixture of collection-wide topics. However, in reality we observe only documents and words, not topics – the latter are part of the hidden (or latent) structure of documents. The aim is to infer the latent topic structure given the words and document. LDA does this by recreating the documents in the corpus by adjusting the relative importance of topics in documents and words in topics iteratively.

    大家只要领会LDA就是给一大堆文档定主题的过程就行。今天主要看实战操作

    数据预处理

    写了好几篇文本挖掘的文章了,预处理基本都一样,本文只贴代码,详细请看之前的文章:

    library(tm)
    files <- readLines('C:/Users/hrd/Desktop/bootcamp/dataset/remarks of biden.txt')
    docs <- Corpus(VectorSource(files))
    
    docs <-tm_map(docs,content_transformer(tolower))
    toSpace <- content_transformer(function(x, pattern) { return (gsub(pattern, ” “, x))})
    docs <- tm_map(docs, toSpace, “-“)
    docs <- tm_map(docs, toSpace, “’”)
    docs <- tm_map(docs, toSpace, “‘”)
    docs <- tm_map(docs, toSpace, “•”)
    docs <- tm_map(docs, toSpace, “””)
    docs <- tm_map(docs, toSpace, ““”)
    docs <- tm_map(docs, removePunctuation)
    
    docs <- tm_map(docs, removeNumbers)
    docs <- tm_map(docs, removeWords, stopwords('english'))
    
    myStopwords <- c('can', 'say','one','way')#自己设置自定义停用词
    docs <- tm_map(docs, removeWords, myStopwords)
    
    docs <- tm_map(docs, stripWhitespace)
    writeLines(as.character(docs[[1]]))
    
    docs <- tm_map(docs,stemDocument)
    
    myStopwords <- c('can', 'say','one','way')
    docs <- tm_map(docs, removeWords, myStopwords)
    
    writeLines(as.character(docs[[1]]))#查看预处理结果
    
    dtm <- DocumentTermMatrix(docs)
    

    LDA主题建模

    进行主题分析要用到的包为topicmodels,但是之前的预处理步骤都是在tm包中完成的:

    tm provides infrastructure for constructing a corpus, e.g., by reading in text data from PDF files, and transforming a corpus to a document-term matrix which is the input data for topic models

    library(topicmodels)
    #Set parameters for Gibbs sampling
    burnin <- 4000
    iter <- 2000
    thin <- 500
    seed <-list(2003,5,63,100001,765)
    nstart <- 5
    best <- TRUE
    
    #Number of topics
    k <- 5
    #Run LDA using Gibbs sampling
    ldaOut <-LDA(dtm,k, method='Gibbs', 
                 control=list(nstart=nstart, seed = seed, best=best, burnin = burnin, iter = iter, thin=thin))
    

    看上面的代码,我们建立LDA模型用到的方法就是LDA,这个方法的输入对象为一个文档-词矩阵,这个矩阵是通过tm包得到的。

    在运行LDA时我们将method设置为'Gibbs',当然还可以设置别的比如VEM,具体的方法的原理我还不太懂,大家可以自己研究研究。

    建好LDA模型后,我们就可以查看每个文档在每个主题上的概率,和每个主题最紧密的词:

    #每个文档对应的主题
    ldaOut.topics <- as.matrix(topics(ldaOut))
    #每个主题对应的前6个单词
    ldaOut.terms <- as.matrix(terms(ldaOut,6))
    
    R文本挖掘:文本主题分析topic analysis

    然后我们可以通过词来给相应的主题进行命名,从而对所有的文档有一个宏观的了解。

    小结

    今天给大家写了英文LDA主题模型的建立方法,之后会给大家写中文的,其实这个自己还有很多不懂的地方,大家可以一起讨论,感谢大家耐心看完。发表这些东西的主要目的就是督促自己,希望大家关注评论指出不足,一起进步。内容我都会写的很细,用到的数据集也会在原文中给出链接,你只要按照文章中的代码自己也可以做出一样的结果,一个目的就是零基础也能懂,因为自己就是什么基础没有从零学Python和R的,加油。

    (站外链接发不了,请关注后私信回复“数据链接”获取本头条号所有使用数据)

    往期内容:

    R文本挖掘:文本聚类分析

    R文本挖掘:中文词云生成

    R文本挖掘:中文词云生成,以2021新年贺词为例

    R文本挖掘:手把手教你做词云图,小白教程

    R文本挖掘:词云图怎么做,worldcloud2初识

    相关文章

      网友评论

        本文标题:R文本挖掘:文本主题分析topic analysis

        本文链接:https://www.haomeiwen.com/subject/mqpizktx.html