项目作者: andreaferretti

项目描述 :
Latent Dirichlet Allocation
高级语言: Nim
项目地址: git://github.com/andreaferretti/lda.git
创建时间: 2018-05-18T17:15:45Z
项目社区:https://github.com/andreaferretti/lda

开源协议:Apache License 2.0

下载


LDA

This library implements a form of text clustering and topic modeling called
Latent Dirichlet Allocation.

In order to use it, you have to have a seq of documents, each one being itself
a seq of strings. These documents can then be indexed through the use of a
vocabulary, as follows:

  1. import sequtils, strutils
  2. import lda
  3. let
  4. rawDocs = @[
  5. "eat turkey on turkey day holiday",
  6. "i like to eat cake on holiday",
  7. "turkey trot race on thanksgiving holiday",
  8. "snail race the turtle",
  9. "time travel space race",
  10. "movie on thanksgiving",
  11. "movie at air and space museum is cool movie",
  12. "aspiring movie star"
  13. ]
  14. docWords = rawDocs.mapIt(it.split(' '))
  15. vocab = makeVocab(docWords)
  16. docs = makeDocs(docWords, vocab)

Once you have the vocabulary vocab , which is just the seq of all word appearing
through all documents, and the preoprocessed documents, which are a nested
sequence of integer indices, you can traing the model through Collapsed Gibbs
Sampling using

  1. let ldaResult = lda(docs, vocabLen = vocab.len, K = 3, iterations = 1000)

Here K denotes the number of desired topics and iterations the number of
rounds in the training phase. The result contains a document/topic matrix
and a word/topic matrix. These can be used to find the most descriptive
words for a topic:

  1. for t in 0 ..< 3:
  2. echo "TOPIC ", t
  3. echo bestWords(ldaResult, vocab, t)

or to find the most relevant topics for a document:

  1. for d in 0 ..< docs.len:
  2. echo "> ", rawDocs[d]
  3. echo "topic: ", ldaResult.bestTopic(d)

or even to generate text with the same topic distribution as a given document:

  1. echo sample(ldaResult, vocab, doc = 6)

TODO

  • parallel training
  • variational Bayes sampling
  • modified model to account for stop words