Skip to content

aconbere/scala-markov

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Scala-Markov

Some expirements in markov chains

Use

import scala.io.Source
import org.conbere.markov._

object Markov extends App {
  def main(args: Array[String]) = {
    // \2 and \3 are the ascii stop and start characters
    val stop = '\2'
    val start = '\3'

    // we'll use lists of length 2
    val chainLength = 2

    // build a markov chain from an input file where each line
    // consists of a planet name from Star Trek
    val m = Source.fromFile("./src/test/resources/planets.txt")
                  .getLines
                  // MarkovChainMap is a markov chain that stores the chain
                  // options in a Map[C,Int]. This map is working over Chars
                  .foldLeft(new MarkovChain[Char](start, stop))((acc, n) =>
                    acc.insert(n.toLowerCase.toList)
                  ))

    // Generate a new planet name based on the corpus with a maximum length
    // of 10
    println(m.generate(10))
  }
}

About

A Markov Chain Generator In Scala

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages