Created
October 23, 2016 18:23
-
-
Save ixaxaar/5c47d96bcefeb649aec154dbd1fd49a6 to your computer and use it in GitHub Desktop.
This file contains 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
def beam[T]( | |
data:List[List[T]], | |
perplexityFn: List[T] => Double, | |
startNode:T, | |
beamSize:Int = 10) | |
:List[(List[T], Double)] = { | |
var candidates = List((List(startNode), 0d)) | |
data.foreach{ case datum:List[T] => | |
candidates = datum.map{ case d:T => | |
candidates.map{ case (c:List[T], p:Double) => | |
val x = c :+ d | |
(x, perplexityFn(x)) | |
} | |
}.toList.flatten | |
.sortWith{ case(a:(List[T], Double), b:(List[T], Double)) => | |
a._2 > b._2 | |
}.take(beamSize) | |
} | |
candidates | |
} | |
def beamTest:Unit = { | |
val testData = List( | |
List("some", "random", "algorithm","q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"), | |
List("that", "prefers", "to", "have","q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"), | |
List("more", "longer","q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"), | |
List("words","q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m") | |
) | |
val res = beam[String](testData, (x:List[String]) => x.mkString(" ").length.toDouble, "", 5) | |
res.map{ r => println(r._1.mkString(" ") +"\t\t->\t"+r._2) } | |
} |
Author
ixaxaar
commented
Oct 23, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment