Created
February 22, 2013 11:41
-
-
Save kanshi/5012898 to your computer and use it in GitHub Desktop.
A basic memory model for Thoughtless
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.hashtree.stringmetric.similarity._ | |
case class Memory[T <: String](val items: Set[Mem[T]]) { | |
def rough(precision: Double) = precision - precision * 0.1 | |
def increased(precision: Double) = precision + precision / 2 | |
def compare(item1: T, item2: T, precision: Double): Boolean = | |
JaroWinklerMetric.compare(item1, item2) match { | |
case Some(similarity: Double) => (similarity > rough(precision)) | |
case None => false | |
} | |
def scan(depth: Int, ioi: T, precision: Double): Set[Mem[T]] = | |
if (depth < 1) Set[Mem[T]]() | |
else items | |
.filter(item => compare(item.ioi, ioi, precision)) | |
.flatMap(item => | |
item.related.scan(depth - 1, ioi, math.min(1, increased(precision))) + item) | |
def add(mem: Mem[T], precision: Double): Memory[T] = { | |
val (related, unrelated) = items.partition(item => compare(item.ioi, mem.ioi, precision)) | |
if (related.isEmpty == true) Memory(unrelated + mem) | |
else Memory(unrelated ++ { | |
for (item <- related) | |
yield Mem[T](item.ioi, item.emotion, Memory[T](item.related.add(mem, increased(precision)).items + mem)) | |
}.toSet[Mem[T]]) | |
} | |
} | |
case class Mem[T <: String](val ioi: T, val emotion: Emotion, val related: Memory[T] = Memory[T](Set())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment