Skip to content

Commit

Permalink
Add a basic test of the dependency graph serialization for ProtobufAn…
Browse files Browse the repository at this point in the history
…notationSerializer
  • Loading branch information
AngledLuffa committed Oct 14, 2023
1 parent 29f2f26 commit d54da9e
Showing 1 changed file with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package edu.stanford.nlp.pipeline;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.ling.IndexedWord;
import edu.stanford.nlp.semgraph.SemanticGraph;

/**
* A test of a few specific methods in the ProtobufAnnotationSerializer
*
* @author John Bauer
*/
public class ProtobufAnnotationSerializerTest {
/** Test serializing a graph and deserializing it */
@Test
public void testDependencySerialization() {
checkGraphReversible("[A/foo-3 obj> B/bar-1 obj> C-4 nsubj> [D-2 obj> E-0]]");
}

/** Test that it still works if one of the nodes has an emptyIndex */
@Test
public void testDependencySerializationWithEmpty() {
checkGraphReversible("[A/foo-3 obj> B/bar-1 obj> C-4 nsubj> [D-1.1 obj> E-0]]");
}

private void checkGraphReversible(String rawGraph) {
ProtobufAnnotationSerializer serializer = new ProtobufAnnotationSerializer();

// test some with tags and some without
SemanticGraph sg = SemanticGraph.valueOf(rawGraph);

CoreNLPProtos.DependencyGraph graphProto = serializer.toProto(sg, true);
List<CoreLabel> labels = new ArrayList<>();
for (CoreNLPProtos.Token tokenProto : graphProto.getTokenList()) {
CoreLabel nextToken = serializer.fromProto(tokenProto);
labels.add(nextToken);
}

SemanticGraph unpacked = serializer.fromProto(graphProto, labels, null);
Assert.assertEquals(sg, unpacked);
}

}

0 comments on commit d54da9e

Please sign in to comment.