-
Notifications
You must be signed in to change notification settings - Fork 101
/
EisnerEnsembleParser.scala
265 lines (222 loc) · 8.39 KB
/
EisnerEnsembleParser.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package org.clulab.processors.clu
import org.clulab.scala.WrappedArray._
import org.clulab.scala.WrappedListBuffer._
import scala.collection.mutable.ListBuffer
import EisnerEnsembleParser._
import scala.collection.mutable
/**
* Stores one dependency for the Eisner algorithm
* Indexes for head and mod start at 1 for the first word in the sentence; 0 is reserved for root
*/
class Span(val dependencies: Seq[Dependency], val head: Int, val score: Float) {
def this() = {
this(List[Dependency](), Dependency.ROOT, 0f)
}
override def toString: String = {
val sb = new StringBuilder()
for(dep <- dependencies) {
sb.append(s"\t$dep\n")
}
sb.toString()
}
def contains(mod: Int, head: Int): Boolean = {
dependencies.exists { dep => dep.mod == mod && dep.head == head }
}
def isEmpty: Boolean = dependencies.isEmpty
}
object Span {
def apply(left: Span, right: Span, dep: Dependency, head: Int): Span = {
// product of probabilities, in log space
val score = left.score + right.score + (if(dep != null) math.log(dep.score).toFloat else 0f)
// aggregate all dependencies for this span
val deps = new ListBuffer[Dependency]
val allNodes = new mutable.HashSet[Int]
val modNodes = new mutable.HashSet[Int]
if(dep != null) {
addDep(dep, deps, allNodes, modNodes)
}
for(dep <- left.dependencies) {
addDep(dep, deps, allNodes, modNodes)
}
for(dep <- right.dependencies) {
addDep(dep, deps, allNodes, modNodes)
}
new Span(deps, head, score)
}
private def addDep(dep: Dependency,
deps: ListBuffer[Dependency],
allNodes: mutable.HashSet[Int],
modNodes: mutable.HashSet[Int]): Unit = {
deps += dep
allNodes += dep.head
allNodes += dep.mod
modNodes += dep.mod
}
}
class Chart(val dimension: Int) {
val chart: Array[Array[Array[Span]]] = mkChart()
private def mkChart(): Array[Array[Array[Span]]] = {
val c = Array.tabulate(dimension) { i =>
Array.tabulate(dimension) { j =>
if (i == j) Array(new Span(), new Span())
else new Array[Span](2) // These are left null.
}
}
c
}
def get(start: Int, end: Int, spanType: Int): Span = {
chart(start)(end)(spanType)
}
def set(start: Int, end: Int, spanType: Int, span: Span): (Int, Float) = {
if(chart(start)(end)(spanType) == null) {
chart(start)(end)(spanType) = span
(1, span.score) // the first value in the tuple just indicates the type of action taken
} else if(chart(start)(end)(spanType).score < span.score) {
chart(start)(end)(spanType) = span
(2, span.score)
} else {
(0, span.score)
}
}
override def toString: String = {
val sb = new StringBuilder()
for(mod <- 0 until dimension) {
for(head <- 0 until dimension) {
for(spanType <- 0 until 2) {
val spanTypeAsString = if(spanType == HEAD_LEFT) "left" else "right"
val span = chart(mod)(head)(spanType)
if(span != null && ! span.isEmpty) {
sb.append(s"[$mod -- $head] (head $spanTypeAsString)\n")
sb.append(span)
}
}
}
}
sb.toString()
}
}
class EisnerEnsembleParser {
def parse(startingDependencies: Array[Array[Dependency]]): Option[Span] = {
// for(i <- startingDependencies.indices) println(s"Index $i: " + startingDependencies(i).mkString(", "))
val length = startingDependencies.length
val chart = new Chart(length)
for(spanLen <- 2 to length) {
for(start <- 0 to length - spanLen) {
val end = start + spanLen - 1 // inclusive
pl(s"Span: [$start, $end]")
for(split <- start until end) {
val ll = chart.get(start, split, HEAD_LEFT)
val rr = chart.get(split + 1, end, HEAD_RIGHT)
if(ll != null && rr != null) {
// merge [start(m), split] and [split + 1, end(h)]
var d = startingDependencies(start)(end)
if(d != null) {
chart.set(start, end, HEAD_RIGHT, Span(ll, rr, d, rr.head))
}
// merge [start(m), split] and [split + 1(h), end]
d = startingDependencies(start)(split + 1)
if(d != null) {
chart.set(start, end, HEAD_RIGHT, Span(ll, rr, d, rr.head))
}
// merge [start(h), split] and [split + 1, end(m)]
d = startingDependencies(end)(start)
if(d != null) {
chart.set(start, end, HEAD_LEFT, Span(ll, rr, d, ll.head))
}
// merge [start, split(h)] and [split + 1, end(m)]
d = startingDependencies(end)(split)
if(d != null) {
chart.set(start, end, HEAD_LEFT, Span(ll, rr, d, ll.head))
}
}
val lr = chart.get(start, split, HEAD_RIGHT)
if(lr != null && rr != null) {
// merge [start, split(m)] and [split + 1(h), end]
var d = startingDependencies(split)(split + 1)
if(d != null) {
chart.set(start, end, HEAD_RIGHT, Span(lr, rr, d, rr.head))
}
// merge [start, split(m)] and [split + 1, end(h)]
d = startingDependencies(split)(end)
if(d != null) {
chart.set(start, end, HEAD_RIGHT, Span(lr, rr, d, rr.head))
}
}
val rl = chart.get(split + 1, end, HEAD_LEFT)
if(ll != null && rl != null) {
// merge [start, split(h)] and [split + 1(m), end]
var d = startingDependencies(split + 1)(split)
if(d != null) {
chart.set(start, end, HEAD_LEFT, Span(ll, rl, d, ll.head))
}
// merge [start(h), split] and [split + 1(m), end]
d = startingDependencies(split + 1)(start)
if(d != null) {
chart.set(start, end, HEAD_LEFT, Span(ll, rl, d, ll.head))
}
}
//
// merge [start, split] and [split, end] in both directions
//
val leftRightComplete = chart.get(start, split, HEAD_RIGHT)
val leftLeftIncomplete = chart.get(start, split, HEAD_LEFT)
val rightRightIncomplete = chart.get(split, end, HEAD_RIGHT)
val rightLeftComplete = chart.get(split, end, HEAD_LEFT)
// merge [start(h), split] and [split(h), end]
if(leftLeftIncomplete != null && rightLeftComplete != null) {
chart.set(start, end, HEAD_LEFT,
Span(leftLeftIncomplete, rightLeftComplete, null, leftLeftIncomplete.head))
}
// merge [start, split(h)] and [split, end(h)]
if(leftRightComplete != null && rightRightIncomplete != null) {
chart.set(start, end, HEAD_RIGHT,
Span(leftRightComplete, rightRightIncomplete, null, rightRightIncomplete.head))
}
}
}
//pl(s"Chart after spanLen = $spanLen")
//pl(chart.toString())
}
val top = chart.get(0, length - 1, HEAD_LEFT)
Option(top)
}
def generateOutput(top: Span): Array[Dependency] = {
Dependency.sort(top.dependencies)
}
/** Converts the top K predictions from an unlabeled parser into a matrix of Dependency (rows are mods; columns are heads) */
def toDependencyTable(sentDependencies: Array[Array[Dependency]], topK: Int): Array[Array[Dependency]] = {
val extendedSentLength = sentDependencies.length + Dependency.offset
// WARNING: Untouched values will be null!
val dependencies = Array.fill(extendedSentLength)(new Array[Dependency](extendedSentLength))
sentDependencies.foreach { wordDependencies =>
val bestDependencies = wordDependencies.take(topK)
bestDependencies.foreach { dependency =>
dependencies(dependency.mod)(dependency.head) = dependency
}
}
// printDependencyTable(dependencies)
dependencies
}
def printDependencyTable(deps: Array[Array[Dependency]]): Unit = {
p(s"Dependency table of length ${deps.length}:")
for(i <- deps.indices) {
p(s"$i:")
for(j <- deps(i).indices) {
val dep = deps(i)(j)
p("\t")
if(dep != null) p(dep.score.toString)
else p("-")
}
pl()
}
}
}
object EisnerEnsembleParser {
// prints for debugging
val DEBUG = false
def p(s: String): Unit = { if(DEBUG) print(s) }
def pl(s: String = ""): Unit = { if(DEBUG) println(s) }
// types of spans in the chart
val HEAD_LEFT = 0
val HEAD_RIGHT = 1
}