Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object Utils {
- //random
- val r = util.Random
- //read data
- val data = io.Source.fromFile ("data/data.dat").getLines.toList.map
- {_ split "\t"} map(_.map(_.toInt))
- val ocrData = io.Source.fromFile ("data/ocr.dat").getLines.toList.map
- {_ split "\t"} map {a => ((a(0).toInt, a(1)(0)), a(2).toDouble) } toMap
- val transData = io.Source.fromFile ("data/trans.dat").getLines.toList.map
- {_ split "\t"} map {a => ((a(0)(0), a(1)(0)), a(2).toDouble) } toMap
- }
- //variables
- class Variable { var value = -1 }
- class CharVariable(var value: Char = 'z',
- val possibleValues: Array[Char] = Array('e','t','a','o','i','n','s','h','r','d')) extends Variable {
- def randVal = value = possibleValues(Utils.r.nextInt(possibleValues.length))
- }
- class IntVariable(var value: Int = -1, val possibleValues: Array[Int] = 1 to 999 toArray) extends Variable{
- def randVal = value = possibleValues(Utils.r.nextInt(possibleValues.length))
- }
- class LetterVariable extends CharVariable
- class ImageVariable extends IntVariable
- // model stuff
- abstract class Template
- class OCRTemplate extends Template {
- def unroll[t1, t2](v1: t1, v2: t2) = {
- new Factor[t1,t2](v1, v2, {(i: t1, l:t2) => Utils.ocrData((i.value, l.value))})
- }
- }
- // a factor scores the two variables it's connected to
- class Factor[T1 <: Variable,T2 <: Variable] (v1: T1, v2: T2, scoreFn: (T1,T2) => Double) {
- def score = scoreFn(v1, v2)
- }
- // a Model just holds templates
- class Model {
- var templates: List[Template] = Nil
- def +(t: Template) { templates = t :: templates }
- }
- //Inference
- class ExhaustiveInferencer(model: Model) {
- var norm = 1.0
- def infer(images: List[ImageVariable], words: List[LetterVariable]) {
- val factors = for(t <- model.templates; (i,l) <- images zip words) yield t.unroll(i,l)
- (factors.product) / norm
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement