This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
KBestListMarginal.scala
63 lines (49 loc) · 2.48 KB
/
KBestListMarginal.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package epic.parser.kbest
/*
Copyright 2012 David Hall
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import epic.parser._
import breeze.linalg._
import breeze.numerics._
case class KBestListMarginal[L, W](anchoring: GrammarAnchoring[L, W],
marginals: IndexedSeq[ParseMarginal[L, W]]) extends ParseMarginal[L, W] {
def isMaxMarginal: Boolean = false
def this(marg: ParseMarginal[L, W]) = this(marg.anchoring, IndexedSeq(marg))
val probsPerTree = DenseVector(marginals.map(_.logPartition):_*)
val logPartition: Double = softmax(probsPerTree)
probsPerTree -= logPartition
exp.inPlace(probsPerTree)
/**
* Forest traversal that visits spans in a "bottom up" order.
* @param spanVisitor
*/
def visitPostorder(spanVisitor: AnchoredVisitor[L], spanThreshold: Double) {
for ((m, i) <- marginals.zipWithIndex if probsPerTree(i) >= math.exp(spanThreshold)) {
m.visitPostorder(new AnchoredVisitor[L] {
def visitUnaryRule(begin: Int, end: Int, rule: Int, ref: Int, score: Double) {
spanVisitor.visitUnaryRule(begin, end, rule, ref, score * probsPerTree(i))
}
def visitSpan(begin: Int, end: Int, tag: Int, ref: Int, score: Double) {
spanVisitor.visitSpan(begin, end, tag, ref, score * probsPerTree(i))
}
def visitBinaryRule(begin: Int, split: Int, end: Int, rule: Int, ref: Int, score: Double) {
spanVisitor.visitBinaryRule(begin, split, end, rule, ref, score * probsPerTree(i))
}
}, spanThreshold - math.log(probsPerTree(i)))
}
}
override def insideBotScore(begin: Int, end: Int, sym: Int, ref: Int): Double = ???
override def insideTopScore(begin: Int, end: Int, sym: Int, ref: Int): Double = ???
override def feasibleSplitPoints(begin: Int, end: Int, leftChild: Int, leftChildRef: Int, rightChild: Int, rightChildRef: Int): IndexedSeq[Int] = {
marginals.flatMap(_.feasibleSplitPoints(begin, end, leftChild, leftChildRef, rightChild, rightChildRef)).toSet.toIndexedSeq.sorted
}
}