fork(1) download
  1. import java.util.Random
  2.  
  3. trait MarkovChain[T <: MarkovChain[T]] { self: MarkovChain[T] =>
  4. type State
  5.  
  6. protected def random: Random
  7.  
  8. protected def possibleTransitions: Iterable[(Double, State)]
  9.  
  10. def state: State
  11.  
  12. protected def withState(state: State): T
  13.  
  14. def transition = {
  15. val target = random.nextDouble()
  16. val iterator = possibleTransitions.iterator
  17. def go(n: Double, lastState: => State): State =
  18. if (n >= target) {
  19. lastState
  20. } else {
  21. assert(iterator.hasNext)
  22. val (probability, state) = iterator.next()
  23. go(n + probability, state)
  24. }
  25. withState(go(0.0, ???))
  26. }
  27. }
  28.  
  29. case class NonsenseGenerator(table: Map[String, Set[String]], state: String) extends MarkovChain[NonsenseGenerator] {
  30. override type State = String
  31.  
  32. protected override val random = new Random()
  33.  
  34. protected override def possibleTransitions = {
  35. val states = table(state)
  36. states.map{ state => (1.0 / states.size, state) }
  37. }
  38.  
  39. protected override def withState(state: State) =
  40. copy(state = state)
  41. }
  42.  
  43. object NonsenseGenerator {
  44. def apply(input: String): NonsenseGenerator = {
  45. var table = Map[String, Set[String]]().withDefaultValue(Set.empty)
  46. val words = input.split(" ")
  47. for ((word, nextWord) <- words zip (words.tail :+ words(0))) {
  48. table = table + (word -> (table(word) + nextWord))
  49. }
  50. new NonsenseGenerator(table, words(0))
  51. }
  52. }
  53.  
  54. object Main extends App {
  55. val input = """A spark plug (sometimes, in British English, a sparking plug, and, colloquially, a plug) is a device for delivering electric current from an ignition system to the combustion chamber of a spark-ignition engine to ignite the compressed fuel/air mixture by an electric spark, while containing combustion pressure within the engine. A spark plug has a metal threaded shell, electrically isolated from a central electrode by a porcelain insulator. The central electrode, which may contain a resistor, is connected by a heavily insulated wire to the output terminal of an ignition coil or magneto. The spark plug's metal shell is screwed into the engine's cylinder head and thus electrically grounded. The central electrode protrudes through the porcelain insulator into the combustion chamber, forming one or more spark gaps between the inner end of the central electrode and usually one or more protuberances or structures attached to the inner end of the threaded shell and designated the side, earth, or ground electrode(s)."""
  56. val ng = NonsenseGenerator(input)
  57. val result = new StringBuilder
  58. (1 to 200).foldLeft(ng) { case (ng, _) =>
  59. val newNG = ng.transition
  60. result.append(' ')
  61. result.append(newNG.state)
  62. newNG
  63. }
  64. println(result.result())
  65. }
  66.  
Success #stdin #stdout 0.46s 382144KB
stdin
Standard input is empty
stdout
 spark gaps between the side, earth, or magneto. The central electrode, which may contain a heavily insulated wire to ignite the combustion chamber, forming one or magneto. The central electrode, which may contain a sparking plug, and, colloquially, a heavily insulated wire to ignite the side, earth, or magneto. The spark plug (sometimes, in British English, a porcelain insulator into the side, earth, or structures attached to the engine. A spark gaps between the inner end of the compressed fuel/air mixture by a resistor, is connected by an electric current from a device for delivering electric spark, while containing combustion chamber, forming one or ground electrode(s). A spark plug (sometimes, in British English, a resistor, is a device for delivering electric spark, while containing combustion chamber, forming one or ground electrode(s). A spark gaps between the threaded shell, electrically isolated from a plug) is a spark-ignition engine to the compressed fuel/air mixture by a central electrode, which may contain a resistor, is a plug) is screwed into the combustion pressure within the inner end of the threaded shell, electrically grounded. The spark gaps between the side, earth, or magneto. The central electrode protrudes through the engine's cylinder head and designated