File size: 17,773 Bytes
6f3ebfa | 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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 | package org.maltparser.concurrent.graph;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import org.maltparser.concurrent.graph.dataformat.ColumnDescription;
import org.maltparser.concurrent.graph.dataformat.DataFormat;
import org.maltparser.core.exception.MaltChainedException;
import org.maltparser.core.symbol.SymbolTable;
import org.maltparser.core.syntaxgraph.DependencyStructure;
import org.maltparser.core.syntaxgraph.node.DependencyNode;
/**
* Immutable and tread-safe dependency graph implementation.
*
* @author Johan Hall
*/
public final class ConcurrentDependencyGraph {
private static final String TAB_SIGN = "\t";
private final DataFormat dataFormat;
private final ConcurrentDependencyNode[] nodes;
/**
* Creates a copy of a dependency graph
*
* @param graph a dependency graph
* @throws ConcurrentGraphException
*/
public ConcurrentDependencyGraph(ConcurrentDependencyGraph graph) throws ConcurrentGraphException {
this.dataFormat = graph.dataFormat;
this.nodes = new ConcurrentDependencyNode[graph.nodes.length+1];
for (int i = 0; i < graph.nodes.length; i++) {
nodes[i] = new ConcurrentDependencyNode(this, (ConcurrentDependencyNode)graph.nodes[i]);
}
}
/**
* Creates a immutable dependency graph
*
* @param dataFormat a data format that describes the label types (or the columns in the input and output)
* @param inputTokens a string array of tokens. Each label is separated by a tab-character and must follow the order in the data format
* @throws ConcurrentGraphException
*/
public ConcurrentDependencyGraph(DataFormat dataFormat, String[] inputTokens) throws ConcurrentGraphException {
this.dataFormat = dataFormat;
this.nodes = new ConcurrentDependencyNode[inputTokens.length+1];
// Add nodes
nodes[0] = new ConcurrentDependencyNode(this, 0, null); // ROOT
for (int i = 0; i < inputTokens.length; i++) {
String[] columns = inputTokens[i].split(TAB_SIGN);
nodes[i+1] = new ConcurrentDependencyNode(this, i+1, columns);
}
// Check graph
for (int i = 0; i < nodes.length; i++) {
if (nodes[i].getHeadIndex() >= nodes.length) {
throw new ConcurrentGraphException("Not allowed to add a head node that doesn't exists");
}
}
}
/**
* Creates a immutable dependency graph
*
* @param dataFormat a data format that describes the label types (or the columns in the input and output)
* @param sourceGraph a dependency graph that implements the interface org.maltparser.core.syntaxgraph.DependencyStructure
* @param defaultRootLabel the default root label
* @throws MaltChainedException
*/
public ConcurrentDependencyGraph(DataFormat dataFormat, DependencyStructure sourceGraph, String defaultRootLabel) throws MaltChainedException {
this.dataFormat = dataFormat;
this.nodes = new ConcurrentDependencyNode[sourceGraph.nDependencyNode()];
// Add nodes
nodes[0] = new ConcurrentDependencyNode(this, 0, null); // ROOT
for (int index : sourceGraph.getTokenIndices()) {
final DependencyNode gnode = sourceGraph.getDependencyNode(index);
String[] columns = new String[dataFormat.numberOfColumns()];
for (int i = 0; i < dataFormat.numberOfColumns(); i++) {
ColumnDescription column = dataFormat.getColumnDescription(i);
if (!column.isInternal()) {
if (column.getCategory() == ColumnDescription.INPUT) {
columns[i] = gnode.getLabelSymbol(sourceGraph.getSymbolTables().getSymbolTable(column.getName()));
} else if (column.getCategory() == ColumnDescription.HEAD) {
if (gnode.hasHead()) {
columns[i] = Integer.toString(gnode.getHeadEdge().getSource().getIndex());
} else {
columns[i] = Integer.toString(-1);
}
} else if (column.getCategory() == ColumnDescription.DEPENDENCY_EDGE_LABEL) {
SymbolTable sourceTable = sourceGraph.getSymbolTables().getSymbolTable(column.getName());
if (gnode.getHeadEdge().hasLabel(sourceTable)) {
columns[i] = gnode.getHeadEdge().getLabelSymbol(sourceTable);
} else {
columns[i] = defaultRootLabel;
}
} else {
columns[i] = "_";
}
}
}
nodes[index] = new ConcurrentDependencyNode(this, index, columns);
}
}
protected ConcurrentDependencyGraph(DataFormat dataFormat, ConcurrentDependencyNode[] inputNodes) throws ConcurrentGraphException {
this.dataFormat = dataFormat;
this.nodes = new ConcurrentDependencyNode[inputNodes.length];
// Add nodes
for (int i = 0; i < inputNodes.length; i++) {
nodes[i] = inputNodes[i];
}
// Check graph
for (int i = 0; i < nodes.length; i++) {
if (nodes[i].getHeadIndex() >= nodes.length) {
throw new ConcurrentGraphException("Not allowed to add a head node that doesn't exists");
}
}
}
/**
* Returns the data format that describes the label types (or the columns in the input and output)
*
* @return the data format that describes the label types
*/
public DataFormat getDataFormat() {
return dataFormat;
}
/**
* Returns the root node
*
* @return the root node
*/
public ConcurrentDependencyNode getRoot() {
return nodes[0];
}
/**
* Returns a dependency node specified by the node index
*
* @param nodeIndex the index of the node
* @return a dependency node specified by the node index
*/
// public ConcurrentDependencyNode getNode(int nodeIndex) {
// if (nodeIndex < 0 || nodeIndex >= nodes.length) {
// return null;
// }
// return nodes[nodeIndex];
// }
/**
* Returns a dependency node specified by the node index. Index 0 equals the root node
*
* @param index the index of the node
* @return a dependency node specified by the node index, if out of range <i>null</i> is returned.
*/
public ConcurrentDependencyNode getDependencyNode(int index) {
if (index < 0 || index >= nodes.length) {
return null;
}
return nodes[index];
}
/**
* Returns a dependency node specified by the node index. If index is equals to 0 (the root node) then null will be returned because this node
* is not a token node.
*
* @param index the index of the node
* @return a dependency node specified by the node index, if out of range and root node then <i>null</i> is returned.
*/
public ConcurrentDependencyNode getTokenNode(int index) {
if (index <= 0 || index >= nodes.length) {
return null;
}
return nodes[index];
}
/**
* Returns the number of dependency nodes (including the root node)
*
* @return the number of dependency nodes
*/
public int nDependencyNodes() {
return nodes.length;
}
/**
* Returns the number of token nodes in the dependency graph (Number of dependency nodes - the root node).
*
* @return the number of token nodes in the dependency graph.
*/
public int nTokenNodes() {
return nodes.length - 1;
}
/**
* Returns the index of the last dependency node.
*
* @return the index of the last dependency node.
*/
public int getHighestDependencyNodeIndex() {
return nodes.length - 1;
}
/**
* Returns the index of the last token node.
*
* @return the index of the last token node. If there are no token nodes then -1 is returned.
*/
public int getHighestTokenIndex() {
if (nodes.length == 1) {
return - 1;
}
return nodes.length - 1;
}
/**
* Returns <i>true</i> if the dependency graph has any token nodes, otherwise <i>false</i>.
*
* @return <i>true</i> if the dependency graph has any token nodes, otherwise <i>false</i>.
*/
public boolean hasTokens() {
return nodes.length > 1;
}
/**
* Returns the number of edges
*
* @return the number of edges
*/
public int nEdges() {
int n = 0;
for (int i = 1; i < nodes.length; i++) {
if (nodes[i].hasHead()) {
n++;
}
}
return n;
}
/**
* Returns a sorted set of edges. If no edges are found an empty set is returned
*
* @return a sorted set of edges.
* @throws ConcurrentGraphException
*/
public SortedSet<ConcurrentDependencyEdge> getEdges() throws ConcurrentGraphException {
SortedSet<ConcurrentDependencyEdge> edges = Collections.synchronizedSortedSet(new TreeSet<ConcurrentDependencyEdge>());
for (int i = 1; i < nodes.length; i++) {
ConcurrentDependencyEdge edge = nodes[i].getHeadEdge();
if (edge != null) {
edges.add(edge);
}
}
return edges;
}
/**
* Returns a sorted set of integers {0,s,..n} , where each index i identifies a dependency node. Index 0
* should always be the root dependency node and index s is the first terminal node and index n is the
* last terminal node.
*
* @return a sorted set of integers
*/
public SortedSet<Integer> getDependencyIndices() {
SortedSet<Integer> indices = Collections.synchronizedSortedSet(new TreeSet<Integer>());
for (int i = 0; i < nodes.length; i++) {
indices.add(i);
}
return indices;
}
/**
* Returns a sorted set of integers {s,...,n}, where each index i identifies a token node. Index <i>s</i>
* is the first token node and index <i>n</i> is the last token node.
*
* @return a sorted set of integers {s,...,n}. If there are no token nodes then an empty set is returned.
*/
public SortedSet<Integer> getTokenIndices() {
SortedSet<Integer> indices = Collections.synchronizedSortedSet(new TreeSet<Integer>());
for (int i = 1; i < nodes.length; i++) {
indices.add(i);
}
return indices;
}
/**
* Returns <i>true</i> if the head edge of the dependency node with <i>index</i> is labeled, otherwise <i>false</i>.
*
* @param index the index of the dependency node
* @return <i>true</i> if the head edge of the dependency node with <i>index</i> is labeled, otherwise <i>false</i>.
*/
public boolean hasLabeledDependency(int index) {
if (index < 0 || index >= nodes.length) {
return false;
}
if (!nodes[index].hasHead()) {
return false;
}
return nodes[index].isHeadLabeled();
}
/**
* Returns <i>true</i> if all nodes in the dependency structure are connected, otherwise <i>false</i>.
*
* @return <i>true</i> if all nodes in the dependency structure are connected, otherwise <i>false</i>.
*/
public boolean isConnected() {
int[] components = findComponents();
int tmp = components[0];
for (int i = 1; i < components.length; i++) {
if (tmp != components[i]) {
return false;
}
}
return true;
}
/**
* Returns <i>true</i> if all edges in the dependency structure are projective, otherwise <i>false</i>.
*
* @return <i>true</i> if all edges in the dependency structure are projective, otherwise <i>false</i>.
*/
public boolean isProjective() {
for (int i = 1; i < nodes.length; i++) {
if (!nodes[i].isProjective()) {
return false;
}
}
return true;
}
/**
* Returns <i>true</i> if all dependency nodes have at most one incoming edge, otherwise <i>false</i>.
*
* Note: In this implementation this will always be <i>true</i>
*
* @return <i>true</i> if all dependency nodes have at most one incoming edge, otherwise <i>false</i>.
*/
public boolean isSingleHeaded() {
return true;
}
/**
* Returns <i>true</i> if the dependency structure are a tree (isConnected() && isSingleHeaded()), otherwise <i>false</i>.
*
* @return <i>true</i> if the dependency structure are a tree (isConnected() && isSingleHeaded()), otherwise <i>false</i>.
*/
public boolean isTree() {
return isConnected() && isSingleHeaded();
}
/**
* Returns the number of non-projective edges in the dependency structure.
*
* @return the number of non-projective edges in the dependency structure.
*/
public int nNonProjectiveEdges() {
int c = 0;
for (int i = 1; i < nodes.length; i++) {
if (!nodes[i].isProjective()) {
c++;
}
}
return c;
}
protected boolean hasDependent(int nodeIndex) {
for (int i = 1; i < nodes.length; i++) {
if (nodeIndex == nodes[i].getHeadIndex()) {
return true;
}
}
return false;
}
protected boolean hasLeftDependent(int nodeIndex) {
for (int i = 1; i < nodeIndex; i++) {
if (nodeIndex == nodes[i].getHeadIndex()) {
return true;
}
}
return false;
}
protected boolean hasRightDependent(int nodeIndex) {
for (int i = nodeIndex + 1; i < nodes.length; i++) {
if (nodeIndex == nodes[i].getHeadIndex()) {
return true;
}
}
return false;
}
protected List<ConcurrentDependencyNode> getListOfLeftDependents(int nodeIndex) {
List<ConcurrentDependencyNode> leftDependents = Collections.synchronizedList(new ArrayList<ConcurrentDependencyNode>());
for (int i = 1; i < nodeIndex; i++) {
if (nodeIndex == nodes[i].getHeadIndex()) {
leftDependents.add(nodes[i]);
}
}
return leftDependents;
}
protected SortedSet<ConcurrentDependencyNode> getSortedSetOfLeftDependents(int nodeIndex) {
SortedSet<ConcurrentDependencyNode> leftDependents = Collections.synchronizedSortedSet(new TreeSet<ConcurrentDependencyNode>());
for (int i = 1; i < nodeIndex; i++) {
if (nodeIndex == nodes[i].getHeadIndex()) {
leftDependents.add(nodes[i]);
}
}
return leftDependents;
}
protected List<ConcurrentDependencyNode> getListOfRightDependents(int nodeIndex) {
List<ConcurrentDependencyNode> rightDependents = Collections.synchronizedList(new ArrayList<ConcurrentDependencyNode>());
for (int i = nodeIndex + 1; i < nodes.length; i++) {
if (nodeIndex == nodes[i].getHeadIndex()) {
rightDependents.add(nodes[i]);
}
}
return rightDependents;
}
protected SortedSet<ConcurrentDependencyNode> getSortedSetOfRightDependents(int nodeIndex) {
SortedSet<ConcurrentDependencyNode> rightDependents = Collections.synchronizedSortedSet(new TreeSet<ConcurrentDependencyNode>());
for (int i = nodeIndex + 1; i < nodes.length; i++) {
if (nodeIndex == nodes[i].getHeadIndex()) {
rightDependents.add(nodes[i]);
}
}
return rightDependents;
}
protected List<ConcurrentDependencyNode> getListOfDependents(int nodeIndex) {
List<ConcurrentDependencyNode> dependents = Collections.synchronizedList(new ArrayList<ConcurrentDependencyNode>());
for (int i = 1; i < nodes.length; i++) {
if (nodeIndex == nodes[i].getHeadIndex()) {
dependents.add(nodes[i]);
}
}
return dependents;
}
protected SortedSet<ConcurrentDependencyNode> getSortedSetOfDependents(int nodeIndex) {
SortedSet<ConcurrentDependencyNode> dependents = Collections.synchronizedSortedSet(new TreeSet<ConcurrentDependencyNode>());
for (int i = 1; i < nodes.length; i++) {
if (nodeIndex == nodes[i].getHeadIndex()) {
dependents.add(nodes[i]);
}
}
return dependents;
}
protected int getRank(int nodeIndex) {
int[] components = new int[nodes.length];
int[] ranks = new int[nodes.length];
for (int i = 0; i < components.length; i++) {
components[i] = i;
ranks[i] = 0;
}
for (int i = 1; i < nodes.length; i++) {
if (nodes[i].hasHead()) {
int hcIndex = findComponent(nodes[i].getHead().getIndex(), components);
int dcIndex = findComponent(nodes[i].getIndex(), components);
if (hcIndex != dcIndex) {
link(hcIndex, dcIndex, components, ranks);
}
}
}
return ranks[nodeIndex];
}
protected ConcurrentDependencyNode findComponent(int nodeIndex) {
int[] components = new int[nodes.length];
int[] ranks = new int[nodes.length];
for (int i = 0; i < components.length; i++) {
components[i] = i;
ranks[i] = 0;
}
for (int i = 1; i < nodes.length; i++) {
if (nodes[i].hasHead()) {
int hcIndex = findComponent(nodes[i].getHead().getIndex(), components);
int dcIndex = findComponent(nodes[i].getIndex(), components);
if (hcIndex != dcIndex) {
link(hcIndex, dcIndex, components, ranks);
}
}
}
return nodes[findComponent(nodeIndex, components)];
}
private int[] findComponents() {
int[] components = new int[nodes.length];
int[] ranks = new int[nodes.length];
for (int i = 0; i < components.length; i++) {
components[i] = i;
ranks[i] = 0;
}
for (int i = 1; i < nodes.length; i++) {
if (nodes[i].hasHead()) {
int hcIndex = findComponent(nodes[i].getHead().getIndex(), components);
int dcIndex = findComponent(nodes[i].getIndex(), components);
if (hcIndex != dcIndex) {
link(hcIndex, dcIndex, components, ranks);
}
}
}
return components;
}
private int findComponent(int xIndex, int[] components) {
if (xIndex != components[xIndex]) {
components[xIndex] = findComponent(components[xIndex], components);
}
return components[xIndex];
}
private int link(int xIndex, int yIndex, int[] components, int[] ranks) {
if (ranks[xIndex] > ranks[yIndex]) {
components[yIndex] = xIndex;
} else {
components[xIndex] = yIndex;
if (ranks[xIndex] == ranks[yIndex]) {
ranks[yIndex]++;
}
return yIndex;
}
return xIndex;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dataFormat == null) ? 0 : dataFormat.hashCode());
result = prime * result + Arrays.hashCode(nodes);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ConcurrentDependencyGraph other = (ConcurrentDependencyGraph) obj;
if (dataFormat == null) {
if (other.dataFormat != null)
return false;
} else if (!dataFormat.equals(other.dataFormat))
return false;
if (!Arrays.equals(nodes, other.nodes))
return false;
return true;
}
public String toString() {
final StringBuilder sb = new StringBuilder();
for (int i = 1; i < nodes.length; i++) {
sb.append(nodes[i]);
sb.append('\n');
}
return sb.toString();
}
}
|