Skip to content

Commit

Permalink
Update entailment classifier; tweak OpenIE demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabor Angeli authored and Stanford NLP committed May 15, 2015
1 parent ffabc1e commit 2684210
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
Expand Up @@ -58,6 +58,10 @@ public class ClauseSplitterSearchProblem {
add("clone_nsubj");
add("simple");
}});
put("vmod", new ArrayList<String>() {{
add("clone_nsubj");
add("simple");
}});
}});

/**
Expand Down
62 changes: 35 additions & 27 deletions src/edu/stanford/nlp/naturalli/OpenIEServlet.java
Expand Up @@ -36,6 +36,7 @@ public class OpenIEServlet extends HttpServlet {
public void init() throws ServletException {
Properties commonProps = new Properties() {{
setProperty("depparse.extradependencies", "ref_only_uncollapsed");
setProperty("parse.extradependencies", "ref_only_uncollapsed");
setProperty("openie.splitter.threshold", "0.10");
setProperty("openie.optimze_for", "GENERAL");
setProperty("openie.ignoreaffinity", "false");
Expand Down Expand Up @@ -73,23 +74,22 @@ public void init() throws ServletException {
/**
* Annotate a document (which is usually just a sentence).
*/
public void annotate(Annotation ann) {
pipeline.annotate(ann);
if (ann.get(CoreAnnotations.SentencesAnnotation.class).size() == 1) {
CoreMap sentence = ann.get(CoreAnnotations.SentencesAnnotation.class).get(0);
if (sentence.get(NaturalLogicAnnotations.RelationTriplesAnnotation.class) != null) {
if (sentence.get(NaturalLogicAnnotations.RelationTriplesAnnotation.class).isEmpty()) {
for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
token.remove(NaturalLogicAnnotations.OperatorAnnotation.class);
token.remove(NaturalLogicAnnotations.PolarityAnnotation.class);
}
sentence.remove(NaturalLogicAnnotations.RelationTriplesAnnotation.class);
sentence.remove(NaturalLogicAnnotations.EntailedSentencesAnnotation.class);
sentence.remove(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
sentence.remove(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class);
sentence.remove(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
backoff.annotate(ann);
public void annotate(StanfordCoreNLP pipeline, Annotation ann) {
if (ann.get(CoreAnnotations.SentencesAnnotation.class) == null) {
pipeline.annotate(ann);
} else {
if (ann.get(CoreAnnotations.SentencesAnnotation.class).size() == 1) {
CoreMap sentence = ann.get(CoreAnnotations.SentencesAnnotation.class).get(0);
for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
token.remove(NaturalLogicAnnotations.OperatorAnnotation.class);
token.remove(NaturalLogicAnnotations.PolarityAnnotation.class);
}
sentence.remove(NaturalLogicAnnotations.RelationTriplesAnnotation.class);
sentence.remove(NaturalLogicAnnotations.EntailedSentencesAnnotation.class);
sentence.remove(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
sentence.remove(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class);
sentence.remove(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
pipeline.annotate(ann);
}
}
}
Expand Down Expand Up @@ -153,6 +153,22 @@ public static String quote(String string) {
return sb.toString();
}

private void runWithPipeline(StanfordCoreNLP pipeline, Annotation ann, Set<String> triples, Set<String> entailments) {
// Annotate
annotate(pipeline, ann);
// Extract info
for (CoreMap sentence : ann.get(CoreAnnotations.SentencesAnnotation.class)) {
for (SentenceFragment fragment : sentence.get(NaturalLogicAnnotations.EntailedSentencesAnnotation.class)) {
entailments.add(quote(fragment.toString()));
}
for (RelationTriple fragment : sentence.get(NaturalLogicAnnotations.RelationTriplesAnnotation.class)) {
triples.add("[ " + quote(fragment.subjectGloss()) + ", " + quote(fragment.relationGloss()) + ", " + quote(fragment.objectGloss()) + " ]");
}
}

}


/**
* Actually perform the GET request, given all the relevant information (already sanity checked).
* This is the meat of the servlet code.
Expand All @@ -172,19 +188,11 @@ private void doGet(PrintWriter out, String q) {
// Annotate
Annotation ann = new Annotation(q);
try {
// Annotate
annotate(ann);
// Collect results
List<String> entailments = new ArrayList<>();
Set<String> entailments = new HashSet<>();
Set<String> triples = new LinkedHashSet<>();
for (CoreMap sentence : ann.get(CoreAnnotations.SentencesAnnotation.class)) {
for (SentenceFragment fragment : sentence.get(NaturalLogicAnnotations.EntailedSentencesAnnotation.class)) {
entailments.add(quote(fragment.toString()));
}
for (RelationTriple fragment : sentence.get(NaturalLogicAnnotations.RelationTriplesAnnotation.class)) {
triples.add("[ " + quote(fragment.subjectLemmaGloss()) + ", " + quote(fragment.relationLemmaGloss()) + ", " + quote(fragment.objectLemmaGloss()) + " ]");
}
}
runWithPipeline(pipeline, ann, triples, entailments); // pipeline must come before backoff
runWithPipeline(backoff, ann, triples, entailments); // backoff must come after pipeline
// Write results
out.println("{ " +
"\"ok\":true, " +
Expand Down
2 changes: 1 addition & 1 deletion src/edu/stanford/nlp/pipeline/AnnotatorFactories.java
Expand Up @@ -444,7 +444,7 @@ public String additionalSignature() {
//
// Parser
//
public static AnnotatorFactory parse(Properties properties, final AnnotatorImplementations annotatorImplementation) {
public static AnnotatorFactory parse(final Properties properties, final AnnotatorImplementations annotatorImplementation) {
return new AnnotatorFactory(properties, annotatorImplementation) {
private static final long serialVersionUID = 1L;

Expand Down

0 comments on commit 2684210

Please sign in to comment.