Skip to content

Commit

Permalink
Not working yet, but at least the error messages are a lot more meani…
Browse files Browse the repository at this point in the history
…ngful
  • Loading branch information
AngledLuffa authored and Stanford NLP committed Jun 7, 2019
1 parent df905ff commit 9857903
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
15 changes: 9 additions & 6 deletions itest/src/edu/stanford/nlp/naturalli/OpenIEITest.java
Expand Up @@ -5,12 +5,14 @@
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.util.CoreMap;
import edu.stanford.nlp.util.StringUtils;
import edu.stanford.nlp.util.Sets;
import org.junit.Ignore;
import org.junit.Test;

import java.util.*;

import java.util.stream.Collectors;

import static org.junit.Assert.*;

/**
Expand Down Expand Up @@ -53,11 +55,12 @@ public void assertExtracted(String expected, String text) {

public void assertExtracted(Set<String> expectedSet, String text) {
Collection<RelationTriple> extractions = annotate(text).get(NaturalLogicAnnotations.RelationTriplesAnnotation.class);
String actual = StringUtils.join(
extractions.stream().map(x -> x.toString().substring(x.toString().indexOf("\t") + 1).toLowerCase()).sorted(),
"\n");
String expected = StringUtils.join(expectedSet.stream().map(String::toLowerCase).sorted(), "\n");
assertEquals(expected, actual);
Set<String> actual =
extractions.stream().map(x -> x.toString().substring(x.toString().indexOf("\t") + 1).toLowerCase())
.collect(Collectors.toSet());
Set<String> expected = expectedSet.stream().map(String::toLowerCase).collect(Collectors.toSet());
Sets.assertEquals(expected, actual, "expected", "actual", true,
() -> "Unexpected results processing " + text);
}

public void assertEntailed(String expected, String text) {
Expand Down
55 changes: 55 additions & 0 deletions src/edu/stanford/nlp/util/Sets.java
Expand Up @@ -3,6 +3,7 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.function.Supplier;


/**
Expand Down Expand Up @@ -117,6 +118,60 @@ public static <E> Set<Set<E>> powerSet(Set<E> s) {
}
}

/**
* Tests whether two sets are equal. If not, throws an assertion
* and gives a detailed report on the differences. May be long
* depending on the sizes of the sets!
*
* @param first a set to compare
* @param second a set to compare against
* @param firstName the name of the first set, used if an error occurs
* @param secondName the name of the second set, used if an error occurs
* @param outputShared output the common values for the two sets
* @param errorMessage a Supplier of an error message, in case it is expensive to generate
*/
public static <E> void assertEquals(Set<E> first, Set<E> second,
String firstName, String secondName,
boolean outputShared, Supplier<String> errorMessage) {
if (first.equals(second)) {
return;
}

// now we know something is different. find out what and throw an assertion
Set<E> firstExtras = diff(first, second);
Set<E> secondExtras = diff(second, first);
StringBuilder builder = new StringBuilder();
builder.append(errorMessage.get());
builder.append("\n");
if (firstExtras.size() > 0) {
builder.append("-- Extra results in " + firstName + ": --\n");
for (E extra : firstExtras) {
builder.append(extra == null ? "(null)" : extra.toString());
builder.append("\n");
}
}
if (secondExtras.size() > 0) {
builder.append("-- Extra results in " + secondName + ": --\n");
for (E extra : secondExtras) {
builder.append(extra == null ? "(null)" : extra.toString());
builder.append("\n");
}
}

if (outputShared) {
Set<E> shared = intersection(first, second);
if (shared.size() > 0) {
builder.append("-- Common results in " + firstName + " and " + secondName + ": --\n");
for (E extra : shared) {
builder.append(extra == null ? "(null)" : extra.toString());
builder.append("\n");
}
}
}

throw new AssertionError(builder.toString());
}

public static void main(String[] args) {
Set<String> h = Generics.newHashSet();
h.add("a");
Expand Down

0 comments on commit 9857903

Please sign in to comment.