Skip to content

Commit

Permalink
Merge pull request #654 from clulab/kwalcock/crash
Browse files Browse the repository at this point in the history
Prevent crash in Eisner algorithm
  • Loading branch information
kwalcock authored Jul 8, 2022
2 parents f4f5a45 + a606c62 commit 8850190
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions main/src/main/scala/org/clulab/dynet/Eisner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -237,24 +237,26 @@ class Eisner {
dep.head - 1
}
val label = dep.label
heads(dep.mod - 1) = Tuple2(head, label)
heads(dep.mod - 1) = (head, label)
}
} else {
// Eisner failed to produce a complete tree; revert to the greedy inference
for(i <- scores.indices) {
val relativeHead = scores(i).maxBy(_._2)._1.toInt
val depMod = i + 1
val depHead = if (relativeHead == 0) 0 else depMod + relativeHead
val label = dependencies(depMod)(depHead).label
// lift() checks the index, and Option(_) checks for nulls.
val valid = dependencies(depMod).lift(depHead).flatMap(Option(_)).isDefined
val label = if (valid) dependencies(depMod)(depHead).label else "root"
val head =
if(generateRelativeHeads) {
// we are storing *relative* head positions here
relativeHead
if (valid) relativeHead else 0
} else {
// we are storing absolute heads, starting at offset 0
depHead - 1
if (valid) depHead - 1 else -1
}
heads(i) = Tuple2(head, label)
heads(i) = (head, label)
}
}
heads
Expand Down

0 comments on commit 8850190

Please sign in to comment.