Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
manning authored and Stanford NLP committed May 2, 2016
1 parent d7f0bd2 commit 5a91fa7
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 90 deletions.
12 changes: 6 additions & 6 deletions liblocal/README
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ LAST UPDATE: 2015/10/5
LAST UPDATE BY: Keenon Werling

-----------------------------------------------------------------------
hamcrest-core.jar
java-hamcrest.jar

ORIGINAL JAR NAME: hamcrest-core-1.3.jar
ORIGINAL JAR NAME: java-hamcrest-2.0.0.0.jar

VERSION: 1.3
VERSION: 2.0.0.0

RELEASE DATE: Jul, 2010
RELEASE DATE: January 2015

SOURCE AVAILABLE: Maven Central

Expand All @@ -36,9 +36,9 @@ URL: http:https://www.hamcrest.org

USED BY: The JUnit library (not directly used in Stanford NLP code)

LAST UPDATE: 2015/10/5
LAST UPDATE: 2016-04-30

LAST UPDATE BY: Keenon Werling
LAST UPDATE BY: John Bauer

-----------------------------------------------------------------------
javaruntype.jar
Expand Down
Binary file removed liblocal/hamcrest-core.jar
Binary file not shown.
Binary file added liblocal/java-hamcrest.jar
Binary file not shown.
Binary file added libsrc/java-hamcrest-sources.jar
Binary file not shown.
153 changes: 103 additions & 50 deletions src/edu/stanford/nlp/graph/DirectedMultiGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.*;

import edu.stanford.nlp.semgraph.SemanticGraphEdge;
import edu.stanford.nlp.util.CollectionUtils;
import edu.stanford.nlp.util.Generics;
import edu.stanford.nlp.util.MapFactory;
Expand Down Expand Up @@ -41,8 +40,9 @@ public DirectedMultiGraph(MapFactory<V, Map<V, List<E>>> outerMapFactory, MapFac
}

/**
* Creates a copy of the given graph. This will copy the entire data structure (this may be slow!), but will not copy
* any of the edge or vertex objects.
* Creates a copy of the given graph. This will copy the entire data
* structure (this may be slow!), but will not copy any of the edge
* or vertex objects.
*
* @param graph The graph to copy into this object.
*/
Expand Down Expand Up @@ -477,39 +477,38 @@ public List<Set<V>> getConnectedComponents() {
/**
* Deletes all duplicate edges.
*/

public void deleteDuplicateEdges() {
for (V vertex : getAllVertices()) {
for (V vertex2 : outgoingEdges.get(vertex).keySet()) {
List<E> data = outgoingEdges.get(vertex).get(vertex2);
Set<E> deduplicatedData = new TreeSet<>(data);
data.clear();
data.addAll(deduplicatedData);
}
for (V vertex2 : incomingEdges.get(vertex).keySet()) {
List<E> data = incomingEdges.get(vertex).get(vertex2);
Set<E> deduplicatedData = new TreeSet<>(data);
data.clear();
data.addAll(deduplicatedData);
}
}
}
public void deleteDuplicateEdges() {
for (V vertex : getAllVertices()) {
for (V vertex2 : outgoingEdges.get(vertex).keySet()) {
List<E> data = outgoingEdges.get(vertex).get(vertex2);
Set<E> deduplicatedData = new TreeSet<>(data);
data.clear();
data.addAll(deduplicatedData);
}
for (V vertex2 : incomingEdges.get(vertex).keySet()) {
List<E> data = incomingEdges.get(vertex).get(vertex2);
Set<E> deduplicatedData = new TreeSet<>(data);
data.clear();
data.addAll(deduplicatedData);
}
}
}


public Iterator<E> incomingEdgeIterator(final V vertex) {
return new EdgeIterator<>(incomingEdges, vertex);
return new EdgeIterator<>(vertex, incomingEdges, outgoingEdges);
}

public Iterable<E> incomingEdgeIterable(final V vertex) {
return () -> new EdgeIterator<>(incomingEdges, vertex);
return () -> new EdgeIterator<>(vertex, incomingEdges, outgoingEdges);
}

public Iterator<E> outgoingEdgeIterator(final V vertex) {
return new EdgeIterator<>(outgoingEdges, vertex);
return new EdgeIterator<>(vertex, outgoingEdges, incomingEdges);
}

public Iterable<E> outgoingEdgeIterable(final V vertex) {
return () -> new EdgeIterator<>(outgoingEdges, vertex);
return () -> new EdgeIterator<>(vertex, outgoingEdges, incomingEdges);
}

public Iterator<E> edgeIterator() {
Expand All @@ -520,28 +519,40 @@ public Iterable<E> edgeIterable() {
return () -> new EdgeIterator<>(DirectedMultiGraph.this);
}



/**
* This class handles either iterating over a single vertex's
* connections or over all connections in a graph.
*/
static class EdgeIterator<V, E> implements Iterator<E> {
private final Map<V, Map<V, List<E>>> incomingEdges;
private Iterator<Map<V, List<E>>> vertexIterator;
private Iterator<List<E>> connectionIterator;
private final Map<V, Map<V, List<E>>> reverseEdges;
/** when iterating over the whole graph, this iterates over nodes */
private Iterator<Map.Entry<V, Map<V, List<E>>>> vertexIterator;
/** for a given node, this iterates over its neighbors */
private Iterator<Map.Entry<V, List<E>>> connectionIterator;
/** given the neighbor of a node, this iterates over all its connections */
private Iterator<E> edgeIterator;
private E lastRemoved = null;

private V currentSource = null;
private V currentTarget = null;
private E currentEdge = null;
private boolean hasNext = true;


public EdgeIterator(DirectedMultiGraph<V, E> graph) {
vertexIterator = graph.outgoingEdges.values().iterator();
incomingEdges = graph.incomingEdges;
vertexIterator = graph.outgoingEdges.entrySet().iterator();
reverseEdges = graph.incomingEdges;
}

public EdgeIterator(Map<V, Map<V, List<E>>> source, V startVertex) {
public EdgeIterator(V startVertex, Map<V, Map<V, List<E>>> source,
Map<V, Map<V, List<E>>> reverseEdges) {
currentSource = startVertex;
Map<V, List<E>> neighbors = source.get(startVertex);
if (neighbors != null) {
vertexIterator = null;
connectionIterator = neighbors.values().iterator();
connectionIterator = neighbors.entrySet().iterator();
}
incomingEdges = null;
this.reverseEdges = reverseEdges;
}

@Override
Expand All @@ -555,18 +566,22 @@ public E next() {
if (!hasNext()) {
throw new NoSuchElementException("Graph edge iterator exhausted.");
}
lastRemoved = edgeIterator.next();
return lastRemoved;
currentEdge = edgeIterator.next();
return currentEdge;
}

private void primeIterator() {
if (edgeIterator != null && edgeIterator.hasNext()) {
hasNext = true; // technically, we shouldn't need to put this here, but let's be safe
} else if (connectionIterator != null && connectionIterator.hasNext()) {
edgeIterator = connectionIterator.next().iterator();
Map.Entry<V, List<E>> nextConnection = connectionIterator.next();
edgeIterator = nextConnection.getValue().iterator();
currentTarget = nextConnection.getKey();
primeIterator();
} else if (vertexIterator != null && vertexIterator.hasNext()) {
connectionIterator = vertexIterator.next().values().iterator();
Map.Entry<V, Map<V, List<E>>> nextVertex = vertexIterator.next();
connectionIterator = nextVertex.getValue().entrySet().iterator();
currentSource = nextVertex.getKey();
primeIterator();
} else {
hasNext = false;
Expand All @@ -575,22 +590,60 @@ private void primeIterator() {

@Override
public void remove() {
if (incomingEdges == null) {
throw new UnsupportedOperationException("remove() is only valid if iterating over entire graph (Gabor was too lazy to implement the general case...sorry!)");
if (currentEdge != null) {
reverseEdges.get(currentTarget).get(currentSource).remove(currentEdge);
edgeIterator.remove();

if (reverseEdges.get(currentTarget).get(currentSource) != null &&
reverseEdges.get(currentTarget).get(currentSource).size() == 0) {
connectionIterator.remove();
reverseEdges.get(currentTarget).remove(currentSource);
// TODO: may not be necessary to set this to null
edgeIterator = null;
}
}
if (lastRemoved != null) {
if (lastRemoved instanceof SemanticGraphEdge) {
SemanticGraphEdge edge = (SemanticGraphEdge) lastRemoved;
//noinspection unchecked
incomingEdges.get((V) edge.getDependent()).get((V) edge.getGovernor()).remove((E) edge);
edgeIterator.remove();
} else {
// TODO(from gabor): This passes the tests, but actually leaves the graph in an inconsistent state.
edgeIterator.remove();
// throw new UnsupportedOperationException("remove() is only valid if iterating over semantic graph edges (Gabor was too lazy to implement the general case...sorry!)");
}
}

/**
* Topological sort of the graph.
* <br>
* This method uses the depth-first search implementation of
* topological sort.
* Topological sorting only works if the graph is acyclic.
*
* @return A sorted list of the vertices
* @throws IllegalStateException if this graph is not a DAG
*/
public List<V> topologicalSort() {
List<V> result = Generics.newArrayList();
Set<V> temporary = outerMapFactory.newSet();
Set<V> permanent = outerMapFactory.newSet();
for (V vertex : getAllVertices()) {
if (!temporary.contains(vertex)) {
topologicalSortHelper(vertex, temporary, permanent, result);
}
}
Collections.reverse(result);
return result;
}

private void topologicalSortHelper(V vertex, Set<V> temporary, Set<V> permanent, List<V> result) {
temporary.add(vertex);
Map<V, List<E>> neighborMap = outgoingEdges.get(vertex);
if (neighborMap != null) {
for (V neighbor : neighborMap.keySet()) {
if (permanent.contains(neighbor)) {
continue;
}
if (temporary.contains(neighbor)) {
throw new IllegalStateException("This graph has cycles. Topological sort not possible: " + this.toString());
}
topologicalSortHelper(neighbor, temporary, permanent, result);
}
}
result.add(vertex);
permanent.add(vertex);
}

/**
Expand Down
13 changes: 7 additions & 6 deletions src/edu/stanford/nlp/pipeline/demo/corenlp-brat.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Takes Stanford CoreNLP JSON output (var data = ... in data.js)
// and uses brat to render everything.

//var serverAddress = 'http:https://localhost:9000/'
var serverAddress = 'http:https://corenlp.run'
//var serverAddress = 'http:https://localhost:9000';
//var serverAddress = 'http:https://corenlp.run';
var serverAddress = '';

// Load Brat libraries
var bratLocation = 'https://storage.googleapis.com/corenlp/js/brat';
Expand Down Expand Up @@ -120,7 +121,7 @@ function nerColor(nerTag) {
*/
function annotators() {
var annotators = "tokenize,ssplit"
$('#annotators option:selected').each(function () {
$('#annotators').find('option:selected').each(function () {
annotators += "," + $(this).val();
});
return annotators;
Expand Down Expand Up @@ -668,7 +669,7 @@ function renderSemgrex(data) {
*/
$(document).ready(function() {
// Some initial styling
$(".chosen-select").chosen();
$('.chosen-select').chosen();
$('.chosen-container').css('width', '100%');

// Submit on shift-enter
Expand Down Expand Up @@ -794,7 +795,7 @@ $(document).ready(function() {
// Make ajax call
$.ajax({
type: 'POST',
url: serverAddress + 'tokensregex?pattern=' + encodeURIComponent(pattern.replace("&", "\\&").replace('+', '\\+')),
url: serverAddress + '/tokensregex?pattern=' + encodeURIComponent(pattern.replace("&", "\\&").replace('+', '\\+')),
data: currentQuery,
success: function(data) {
$('.tokensregex_error').remove(); // Clear error messages
Expand Down Expand Up @@ -826,7 +827,7 @@ $(document).ready(function() {
// Make ajax call
$.ajax({
type: 'POST',
url: serverAddress + 'semgrex?pattern=' + encodeURIComponent(pattern.replace("&", "\\&").replace('+', '\\+')),
url: serverAddress + '/semgrex?pattern=' + encodeURIComponent(pattern.replace("&", "\\&").replace('+', '\\+')),
data: currentQuery,
success: function(data) {
$('.semgrex_error').remove(); // Clear error messages
Expand Down
28 changes: 1 addition & 27 deletions src/edu/stanford/nlp/semgraph/SemanticGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -875,35 +875,9 @@ public void setRoots(Collection<IndexedWord> words) {
* @throws IllegalStateException if this graph is not a DAG
*/
public List<IndexedWord> topologicalSort() {
List<IndexedWord> result = Generics.newArrayList();
Set<IndexedWord> temporary = wordMapFactory.newSet();
Set<IndexedWord> permanent = wordMapFactory.newSet();
for (IndexedWord vertex : vertexSet()) {
if (!temporary.contains(vertex)) {
topologicalSortHelper(vertex, temporary, permanent, result);
}
}
Collections.reverse(result);
return result;
return graph.topologicalSort();
}

private void topologicalSortHelper(IndexedWord vertex, Set<IndexedWord> temporary, Set<IndexedWord> permanent, List<IndexedWord> result) {
temporary.add(vertex);
for (SemanticGraphEdge edge : outgoingEdgeIterable(vertex)) {
IndexedWord target = edge.getTarget();
if (permanent.contains(target)) {
continue;
}
if (temporary.contains(target)) {
throw new IllegalStateException("This graph has cycles. Topological sort not possible: " + this.toString());
}
topologicalSortHelper(target, temporary, permanent, result);
}
result.add(vertex);
permanent.add(vertex);
}


/**
* Does the given <code>vertex</code> have at least one child with the given {@code reln} and the lemma <code>childLemma</code>?
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public void calculate(double[] theta) {
// The splits are then summed in order.
// This different sum order results in slightly different numbers.
MulticoreWrapper<List<Tree>, ModelDerivatives> wrapper =
new MulticoreWrapper<>(model.op.trainOptions.nThreads, new ScoringProcessor());
new MulticoreWrapper<>(model.op.trainOptions.nThreads, new ScoringProcessor());
// use wrapper.nThreads in case the number of threads was automatically changed
for (List<Tree> chunk : CollectionUtils.partitionIntoFolds(trainingBatch, wrapper.nThreads())) {
wrapper.put(chunk);
Expand Down
Loading

0 comments on commit 5a91fa7

Please sign in to comment.