Skip to content

Latest commit

 

History

History
32 lines (28 loc) · 995 Bytes

README.md

File metadata and controls

32 lines (28 loc) · 995 Bytes

Markov4s

Simple Markov chains implementation using Scala. The markov chain is designed as an immutable data structure.

You can train a markov chain from a sequence of steps. For example here we are training a Markov chain starting from a txt file and then generating random text with a random walk:

Using(scala.io.Source.fromFile("test.txt")) { file =>
    val sequenceOfWords = file
      .mkString
      .split("\\s+")
      .toList
      .map(_.toLowerCase)
  
    val markovChain = Markov4s.fromSequenceOfSteps(sequenceOfWords)

    val numberOfSteps = 10
    val initialState = "scala"
    val randomPhrase = markovChain.randomWalk(initialState, numberOfSteps)
      .walk
      .mkString(" ")
  }

Otherwise you can build the Markov chain using the builder:

val chainBuilder = ChainBuilder[Int]()
      .linkTo(1, 2, new Probability(3, 10))
      .linkTo(1, 3, new Probability(7, 10))
      .linkTo(2, 1, Probability.one)
      .linkTo(3, 1, Probability.one)