object Hello extends App {
import org.junit.Assert._
import cc.factorie._
import cc.factorie.la._
import cc.factorie.variable._
import scala.Tuple2
import cc.factorie.model._
object LabelDomain extends CategoricalDomain(List("politics", "sports", "arts"))
class Label(initialValue: String) extends CategoricalVariable(initialValue) { def domain = LabelDomain }
object WordDomain extends CategoricalDomain(List("beat", "beautiful", "election"))
// TODO Consider interface improvements to CategoricalVectorDomain initialization.
object ArticleDomain extends CategoricalVectorDomain[String] { override def dimensionDomain = WordDomain }
class Article(ws: Iterable[String]) extends BinaryFeatureVectorVariable[String](ws) {
def domain = ArticleDomain
}
class MyClassifier(label: Label, article: Article) extends DotFactorWithStatistics2(label, article) {
val weights = new DenseTensor2(LabelDomain.size, WordDomain.size)
weights(LabelDomain.index("politics"), WordDomain.index("beat")) = 3.0
weights(LabelDomain.index("politics"), WordDomain.index("beautiful")) = 2.0
weights(LabelDomain.index("politics"), WordDomain.index("election")) = 5.0
weights(LabelDomain.index("sports"), WordDomain.index("beat")) = 4.0
weights(LabelDomain.index("sports"), WordDomain.index("beautiful")) = 1.0
weights(LabelDomain.index("sports"), WordDomain.index("election")) = -1.0
weights(LabelDomain.index("arts"), WordDomain.index("beat")) = -2.0
weights(LabelDomain.index("arts"), WordDomain.index("beautiful")) = 5.0
weights(LabelDomain.index("arts"), WordDomain.index("election")) = 1.0
}
val a1 = new Article("beat election".split(" "))
val l1 = new Label("politics")
val cf = new MyClassifier(l1, a1)
// Any now we can do simple exhaustive inference
var maxScore = Double.NegativeInfinity
var maxLabeling = LabelDomain.head
for (labeling <- LabelDomain) {
l1.set(labeling)(null)
val score = cf.currentScore
if (score > maxScore) {
maxScore = score
maxLabeling = labeling
}
}
println("When scoring " + a1 + " the highest scoring label value is " + maxLabeling)
}