Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Correct Start Probabilities #2

Merged
merged 1 commit into from

2 participants

@rjhall

This is a decent way to do it imho. Since this is a trigram model it makes sense to define a unique start state (which in this case is (start, start)), and then to generate from there.

@aconbere aconbere merged commit 0a2f962 into aconbere:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 1, 2013
  1. Correct start probabilities.

    rhall authored
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 22 deletions.
  1. +2 −22 src/main/scala/Markov.scala
View
24 src/main/scala/Markov.scala
@@ -3,17 +3,6 @@ package org.conbere.markov
import scala.util.Random
import scala.io.Source
-package object M {
- def random[K](l: List[K]): Option[K] = {
- l.length match {
- case 0 =>
- None
- case _ =>
- Some(l(new Random().nextInt(l.length)))
- }
- }
-}
-
class Frequency[C](val frequencies: Map[C,Int]) {
def this() = this(Map[C,Int]())
@@ -87,12 +76,9 @@ class MarkovChain[C](val start: C,
this(start, stop, new StateStorage[C]())
def insert(w: List[C]) = {
- new MarkovChain(start, stop, edges.insert((start :: w) :+ stop))
+ new MarkovChain(start, stop, edges.insert((List(start, start) ::: w) :+ stop))
}
- def randomKey =
- M.random(edges.keys.filter { case (x1, x2) => x1 == start }.toList)
-
def generate(maxLength: Int): Option[List[C]] = {
def inner(count: Int, acc: List[C], previous: (C,C)): List[C] = {
if (count > 0) {
@@ -108,12 +94,6 @@ class MarkovChain[C](val start: C,
acc
}
}
-
- for (k <- randomKey) yield {
- k match {
- case s @ (x1, x2) =>
- inner(maxLength, List(x2), s)
- }
- }
+ Some(inner(maxLength, List[C](), (start, start)))
}
}
Something went wrong with that request. Please try again.