项目作者: patrikeh

项目描述 :
Latent Dirichlet Allocation
高级语言: Go
项目地址: git://github.com/patrikeh/go-topics.git
创建时间: 2017-11-12T10:24:06Z
项目社区:https://github.com/patrikeh/go-topics

开源协议:

下载


go-topics

A very basic LDA (Latent Dirichlet Allocation) implementation. Not finished by any means, maybe useful as a starting point.

Usage

Create a processor from a set of transformations of the form func(word string) (new string, keep bool):

  1. processor := topics.NewProcessor(
  2. topics.Transformations{
  3. topics.ToLower,
  4. topics.Sanitize,
  5. topics.MinLen,
  6. topics.GetStopwordFilter("../stopwords/en")})

Read data and apply transformations to build a corpus:

  1. var docs = []string{
  2. "I like to eat broccoli and bananas.",
  3. "I ate a banana and spinach smoothie for breakfast.",
  4. "Chinchillas and kittens are cute.",
  5. "My sister adopted cute kittens yesterday.",
  6. "Look at this cute hamster munching on a piece of chinchillas.",
  7. }
  8. corpus, err := processor.AddStrings(topics.NewCorpus(), docs)

Run LDA and print the results:

  1. lda := topics.NewLDA(&topics.Configuration{Verbose: true, PrintInterval: 500, PrintNumWords: 8})
  2. err = lda.Init(corpus, 2, 0, 0) // K (number of topics), α, β (Dirichlet distribution smoothing factors)
  3. _, err = lda.Train(1000) // Number of iterations
  4. lda.PrintTopWords(5)

Resulting in something like:

  1. Topic Tokens Words
  2. 0 9 like(1) eat(1) broccoli(1) bananas(1) ate(1)
  3. 1 14 cute(3) kittens(2) chinchillas(2) piece(1) look(1)