From 5c80a3498cfa16a9911b37a3261de49f7de7f462 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Thu, 13 Apr 2017 22:54:44 -0500 Subject: [PATCH 01/28] Add an SRL Annotator. --- .../SemanticRoleLabeling/SRLAnnotator.scala | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala new file mode 100644 index 00000000..e32b77b0 --- /dev/null +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala @@ -0,0 +1,141 @@ +/** This software is released under the University of Illinois/Research and Academic Use License. See + * the LICENSE file in the root folder for details. Copyright (c) 2016 + * + * Developed by: The Cognitive Computations Group, University of Illinois at Urbana-Champaign + * http://cogcomp.cs.illinois.edu/ + */ +package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling + +import edu.illinois.cs.cogcomp.annotation.{Annotator, AnnotatorConfigurator, AnnotatorException} +import edu.illinois.cs.cogcomp.core.datastructures.{IntPair, ViewNames} +import edu.illinois.cs.cogcomp.core.datastructures.textannotation._ +import edu.illinois.cs.cogcomp.core.utilities.configuration.ResourceManager +import edu.illinois.cs.cogcomp.edison.annotators.ClauseViewGenerator +import edu.illinois.cs.cogcomp.saul.classifier.ClassifierUtils +import edu.illinois.cs.cogcomp.saulexamples.nlp.CommonSensors + +import scala.collection.JavaConversions._ + +class SRLAnnotatorConfigurator extends AnnotatorConfigurator + +class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: ResourceManager = new SRLAnnotatorConfigurator().getDefaultConfig) + extends Annotator(finalViewName, SRLAnnotator.requiredViews, resourceManager) { + val requiredViewSet: Set[String] = getRequiredViews.toSet + lazy val clauseViewGenerator = ClauseViewGenerator.STANFORD + + override def addView(ta: TextAnnotation): Unit = { + + + SRLApps.srlDataModelObject.clearInstances() + + val finalView = new PredicateArgumentView(getViewName, SRLAnnotator.getClass.getCanonicalName, ta, 1.0) + + // Get Predicates in the sentence. + val predicates = getPredicates(ta) + + predicates.foreach({ predicate: Constituent => + + // Get arguments for each predicate detected. + val argumentList = getArguments(ta, predicate) + finalView.addPredicateArguments( + predicate, + argumentList.map(_.getTarget).toList, + argumentList.map(_.getRelationName).toArray, + argumentList.map(_.getScore).toArray) + }) + + assert(finalView.getConstituents.forall(_.getViewName == getViewName), "Verify correct constituent view names.") + ta.addView(getViewName, finalView) + + SRLApps.srlDataModelObject.clearInstances() + } + + override def initialize(rm: ResourceManager): Unit = { + // Load models and other things + ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_dTr/", + SRLClassifiers.predicateClassifier) + ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_bTr/", + SRLClassifiers.argumentXuIdentifierGivenApredicate) + ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_aTr/", + SRLClassifiers.argumentTypeLearner) + } + + def checkPrequisites(ta: TextAnnotation): Unit = { + val missingRequirements = requiredViewSet.diff(ta.getAvailableViews) + if (missingRequirements.nonEmpty) { + throw new AnnotatorException(s"Document ${ta.getId} is missing required views: $missingRequirements") + } + + clauseViewGenerator.addView(ta) + assert(ta.hasView(ViewNames.CLAUSES_STANFORD)) + } + + /** + * @param ta Input Text Annotation instance. + * @return Constituents that are not attached to any view yet. + */ + private def getPredicates(ta: TextAnnotation): Iterable[Constituent] = { + SRLApps.srlDataModelObject.clearInstances() + + SRLApps.srlDataModelObject.sentences.populate(Seq(ta), train = false, populateEdge = false) + SRLApps.srlDataModelObject.tokens.populate(CommonSensors.textAnnotationToTokens(ta), train = false, populateEdge = false) + SRLApps.srlDataModelObject.stringTree.populate(Seq(SRLSensors.textAnnotationToStringTree(ta)), train = false, populateEdge = false) + + val predicateCandidates = ta.getView(ViewNames.TOKENS).map(_.cloneForNewView(getViewName)) + SRLApps.srlDataModelObject.predicates.populate(predicateCandidates, train = false, populateEdge = false) + + // Figure out the constants in Boolean Property + // TODO - Constant for Predicate label + predicateCandidates.filter(SRLClassifiers.predicateClassifier(_) == "true").map({ candidate: Constituent => + candidate.cloneForNewViewWithDestinationLabel(getViewName, "Predicate") + }) + } + + /** + * @param ta Input Text Annotation instance. + * @param predicate Input Predicate instance. + * @return Relation between unattached predicate and arguments. + */ + private def getArguments(ta: TextAnnotation, predicate: Constituent): Iterable[Relation] = { + SRLApps.srlDataModelObject.clearInstances() + + val stringTree = SRLSensors.textAnnotationToStringTree(ta) + + // Prevent duplicate clearing of graphs. + SRLApps.srlDataModelObject.sentences.populate(Seq(ta), train = false, populateEdge = false) + SRLApps.srlDataModelObject.tokens.populate(CommonSensors.textAnnotationToTokens(ta), train = false, populateEdge = false) + SRLApps.srlDataModelObject.stringTree.populate(Seq(stringTree), train = false, populateEdge = false) + SRLApps.srlDataModelObject.predicates.populate(Seq(predicate), train = false, populateEdge = false) + + val candidateRelations = SRLSensors.xuPalmerCandidate(predicate, stringTree) + SRLApps.srlDataModelObject.arguments.populate(candidateRelations.map(_.getTarget), train = false, populateEdge = false) + SRLApps.srlDataModelObject.relations.populate(candidateRelations, train = false, populateEdge = false) + + val finalRelationList = candidateRelations.filter({ candidate: Relation => + SRLClassifiers.argumentXuIdentifierGivenApredicate(candidate) == "true" + }) + + SRLApps.srlDataModelObject.arguments.clear() + SRLApps.srlDataModelObject.arguments.populate(finalRelationList.map(_.getTarget), train = false, populateEdge = false) + + SRLApps.srlDataModelObject.relations.clear() + SRLApps.srlDataModelObject.relations.populate(finalRelationList, train = false, populateEdge = false) + + finalRelationList.flatMap { relation: Relation => + val label = SRLClassifiers.argumentTypeLearner(relation) + if (label == "candidate") None else Some(SRLAnnotator.cloneRelationWithNewLabel(relation, label)) + } + } +} + +object SRLAnnotator { + private val requiredViews = Array(ViewNames.POS, ViewNames.LEMMA, ViewNames.SHALLOW_PARSE, ViewNames.PARSE_STANFORD) + + private def cloneRelationWithNewLabel(sourceRelation: Relation, label: String): Relation = { + val newRelation = new Relation(label, sourceRelation.getSource, sourceRelation.getTarget, sourceRelation.getScore) + sourceRelation.getAttributeKeys.foreach({ key: String => + newRelation.setAttribute(key, sourceRelation.getAttribute(key)) + }) + newRelation + } +} From 246fb334f4ec27d0032859bfda43e8167ed7150f Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Thu, 13 Apr 2017 23:02:48 -0500 Subject: [PATCH 02/28] Fix prerequisites. --- .../saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala index e32b77b0..b2bc7f7c 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala @@ -24,7 +24,7 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: lazy val clauseViewGenerator = ClauseViewGenerator.STANFORD override def addView(ta: TextAnnotation): Unit = { - + checkPrequisites(ta) SRLApps.srlDataModelObject.clearInstances() From c429ea6cfc04b8c1143d3cba510e887cdbe85f79 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Fri, 14 Apr 2017 00:22:33 -0500 Subject: [PATCH 03/28] SRL DataModel change to object instead of a class. --- .../SemanticRoleLabeling/SRLConfigurator.java | 77 ------------ .../PopulateSRLDataModel.scala | 114 +++++++++--------- .../PredArgViewGenerator.scala | 5 +- .../SemanticRoleLabeling/SRLAnnotator.scala | 42 +++---- .../nlp/SemanticRoleLabeling/SRLApps.scala | 25 ++-- .../SemanticRoleLabeling/SRLClassifiers.scala | 3 +- .../SRLConstrainedClassifiers.scala | 3 +- .../SemanticRoleLabeling/SRLConstraints.scala | 4 +- .../SRLMultiGraphDataModel.scala | 13 +- .../data/SRLFrameManagerTest.java | 6 +- .../SemanticRoleLabeling/DataModelTest.scala | 7 +- .../nlp/SemanticRoleLabeling/ModelsTest.scala | 12 +- 12 files changed, 121 insertions(+), 190 deletions(-) delete mode 100644 saul-examples/src/main/java/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConfigurator.java diff --git a/saul-examples/src/main/java/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConfigurator.java b/saul-examples/src/main/java/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConfigurator.java deleted file mode 100644 index a1ad3b03..00000000 --- a/saul-examples/src/main/java/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConfigurator.java +++ /dev/null @@ -1,77 +0,0 @@ -/** This software is released under the University of Illinois/Research and Academic Use License. See - * the LICENSE file in the root folder for details. Copyright (c) 2016 - * - * Developed by: The Cognitive Computations Group, University of Illinois at Urbana-Champaign - * http://cogcomp.cs.illinois.edu/ - */ -package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling; - -import edu.illinois.cs.cogcomp.core.datastructures.ViewNames; -import edu.illinois.cs.cogcomp.core.utilities.configuration.Configurator; -import edu.illinois.cs.cogcomp.core.utilities.configuration.Property; -import edu.illinois.cs.cogcomp.core.utilities.configuration.ResourceManager; - -/** - * The default properties used for all the examples - * - * @author Parisa Kordjamshidi - * @author Christos Christodoulopoulos - */ -public class SRLConfigurator extends Configurator { - - public static final Property TREEBANK_HOME = new Property("treebankHome", "../saul-examples/src/test/resources/SRLToy/treebank"); - public static final Property PROPBANK_HOME = new Property("propbankHome","../saul-examples/src/test/resources/SRLToy/propbank"); - public static final Property TEST_SECTION = new Property("testSection","00"); - - public static final Property MODELS_DIR = new Property("modelsDir", "../models"); - public static final Property USE_CURATOR = new Property("useCurator", Configurator.FALSE); - - // The running mode of the program. Can be "true" for only testing, or "false" for training - public static final Property RUN_MODE = new Property("runMode", Configurator.TRUE); - - // The training mode for the examples. Can be "pipeline", "joint", "jointLoss" or "other" - public static final Property TRAINING_MODE = new Property("trainingMode", "joint"); - - /*********** SRL PROPERTIES ***********/ - // The (sub)directory to store and retrieve the trained SRL models (to be used with MODELS_DIR) - public static final Property SRL_MODEL_DIR = new Property("srlModelDir", "srl"); - - public static final Property SRL_JAR_MODEL_PATH = new Property("jarModelPath","models"); - - // This is used to determine the parse view in SRL experiments (can be ViewNames.GOLD or ViewNames.STANFORD) - // For replicating the published experiments this needs to be GOLD - public static final Property SRL_PARSE_VIEW = new Property("srlParseView", ViewNames.PARSE_GOLD); - - // A file to store the predictions of the SRL classifier (for argument types only) - public static final Property SRL_OUTPUT_FILE = new Property("srlOutputFile", "srl-predictions.txt"); - - // Whether to use gold predicates (if FALSE, predicateClassifier will be used instead) - public static final Property SRL_GOLD_PREDICATES = new Property("srlGoldPredicates", Configurator.TRUE); - - // Whether to use gold argument boundaries (if FALSE, argumentXuIdentifierGivenApredicate will be used instead) - public static final Property SRL_GOLD_ARG_BOUNDARIES = new Property("srlGoldArgBoundaries", Configurator.TRUE); - - // Should we use the pipeline during testing - public static final Property SRL_TEST_PIPELINE = new Property("srlTestPipeLine", Configurator.FALSE); - - // Should we use constraints during testing - public static final Property SRL_TEST_CONSTRAINTS = new Property("srlTestConstraints", Configurator.FALSE); - - // Should we train a predicate classifier given predicate candidates - public static final Property SRL_TRAIN_PREDICATES = new Property("srlTrainPredicates", Configurator.FALSE); - - // Should we train an argument identifier given the XuPalmer argument candidates - public static final Property SRL_TRAIN_ARG_IDENTIFIERS = new Property("srlArgIdentifier", Configurator.FALSE); - - // Should we train an argument type classifier given the XuPalmer argument candidates - public static final Property SRL_TRAIN_ARG_TYPE = new Property("srlArgIdentifier", Configurator.FALSE); - - @Override - public ResourceManager getDefaultConfig() { - Property[] properties = {TREEBANK_HOME, PROPBANK_HOME, MODELS_DIR, USE_CURATOR, TRAINING_MODE, - SRL_MODEL_DIR, SRL_PARSE_VIEW, SRL_OUTPUT_FILE, SRL_GOLD_PREDICATES, SRL_GOLD_ARG_BOUNDARIES, - SRL_TEST_PIPELINE, SRL_TEST_CONSTRAINTS,SRL_JAR_MODEL_PATH, RUN_MODE, SRL_TRAIN_PREDICATES, - SRL_TRAIN_ARG_IDENTIFIERS,SRL_TRAIN_ARG_TYPE,TEST_SECTION}; - return new ResourceManager(generateProperties(properties)); - } -} diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala index cb706447..cad45e69 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala @@ -10,34 +10,34 @@ import java.util.Properties import edu.illinois.cs.cogcomp.annotation.AnnotatorException import edu.illinois.cs.cogcomp.core.datastructures.ViewNames -import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{ Constituent, TextAnnotation, TreeView } +import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{Constituent, TextAnnotation, TreeView} import edu.illinois.cs.cogcomp.core.datastructures.trees.Tree -import edu.illinois.cs.cogcomp.core.utilities.configuration.ResourceManager import edu.illinois.cs.cogcomp.curator.CuratorConfigurator._ import edu.illinois.cs.cogcomp.edison.annotators.ClauseViewGenerator import edu.illinois.cs.cogcomp.nlp.common.PipelineConfigurator._ import edu.illinois.cs.cogcomp.nlp.utilities.ParseUtils import edu.illinois.cs.cogcomp.saul.util.Logging -import edu.illinois.cs.cogcomp.saulexamples.data.{ SRLDataReader, SRLFrameManager } -import edu.illinois.cs.cogcomp.saulexamples.nlp.CommonSensors._ +import edu.illinois.cs.cogcomp.saulexamples.data.SRLDataReader import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLSensors._ -import edu.illinois.cs.cogcomp.saulexamples.nlp.TextAnnotationFactory import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLscalaConfigurator._ +import edu.illinois.cs.cogcomp.saulexamples.nlp.TextAnnotationFactory + import scala.collection.JavaConversions._ +import SRLMultiGraphDataModel._ + /** Created by Parisa on 1/17/16. */ object PopulateSRLDataModel extends Logging { def apply[T <: AnyRef]( testOnly: Boolean = false, useGoldPredicate: Boolean = false, - useGoldArgBoundaries: Boolean = false, - rm: ResourceManager = new SRLConfigurator().getDefaultConfig - ): SRLMultiGraphDataModel = { - val frameManager: SRLFrameManager = new SRLFrameManager(PROPBANK_HOME) - val useCurator = rm.getBoolean(SRLConfigurator.USE_CURATOR) - val parseViewName = rm.getString(SRLConfigurator.SRL_PARSE_VIEW) - val graphs = new SRLMultiGraphDataModel(parseViewName, frameManager) + useGoldArgBoundaries: Boolean = false + ): Unit = { + + val useCurator = SRLscalaConfigurator.USE_CURATOR + val parseViewName = SRLscalaConfigurator.SRL_PARSE_VIEW + val annotatorService = useCurator match { case true => val nonDefaultProps = new Properties() @@ -50,12 +50,14 @@ object PopulateSRLDataModel extends Logging { TextAnnotationFactory.disableSettings(nonDefaultProps, USE_POS, USE_STANFORD_PARSE) TextAnnotationFactory.createPipelineAnnotatorService(nonDefaultProps) } + val clauseViewGenerator = parseViewName match { case ViewNames.PARSE_GOLD => new ClauseViewGenerator(parseViewName, "CLAUSES_GOLD") case ViewNames.PARSE_STANFORD => ClauseViewGenerator.STANFORD } + def addViewAndFilter(taAll: Iterable[TextAnnotation]): Iterable[TextAnnotation] = { - taAll.map { ta => + taAll.flatMap({ ta => try { annotatorService.addView(ta, ViewNames.LEMMA) annotatorService.addView(ta, ViewNames.SHALLOW_PARSE) @@ -65,18 +67,20 @@ object PopulateSRLDataModel extends Logging { } // Add a clause view (needed for the clause relative position feature) clauseViewGenerator.addView(ta) + + // Clean up the trees + val tree: Tree[String] = ta.getView(parseViewName).asInstanceOf[TreeView].getTree(0) + val parseView = new TreeView(parseViewName, ta) + parseView.setParseTree(0, ParseUtils.stripFunctionTags(ParseUtils.snipNullNodes(tree))) + ta.addView(parseViewName, parseView) + + Some(ta) } catch { case e: AnnotatorException => logger.warn(s"Annotation failed for sentence ${ta.getId}; removing it from the list.") - taAll.remove(ta) + None } - // Clean up the trees - val tree: Tree[String] = ta.getView(parseViewName).asInstanceOf[TreeView].getTree(0) - val parseView = new TreeView(parseViewName, ta) - parseView.setParseTree(0, ParseUtils.stripFunctionTags(ParseUtils.snipNullNodes(tree))) - ta.addView(parseViewName, parseView) - ta - } + }) } def printNumbers(reader: SRLDataReader, readerType: String) = { @@ -86,7 +90,33 @@ object PopulateSRLDataModel extends Logging { logger.debug(s"Number of $readerType data arguments: $numArguments") } - var gr: SRLMultiGraphDataModel = null + def populateDocument(a: TextAnnotation, isTrainingInstance: Boolean): Unit = { + if (!useGoldPredicate) { + sentences.populate(Seq(a), train = isTrainingInstance) + + val predicateTrainCandidates = (sentences(a) ~> sentencesToTokens).collect({ + case x: Constituent if posTag(x).startsWith("VB") => x.cloneForNewView(ViewNames.SRL_VERB) + }) + + predicates.populate(predicateTrainCandidates, train = isTrainingInstance) + } else { + sentences.populate(Seq(a), train = isTrainingInstance) + } + logger.debug("gold relations for this train:" + (sentences(a) ~> sentencesToRelations).size) + + if (!useGoldArgBoundaries) { + val XuPalmerCandidateArgsTraining = (sentences(a) ~> sentencesToRelations ~> relationsToPredicates).flatMap({ + x => xuPalmerCandidate(x, (sentences(x.getTextAnnotation) ~> sentencesToStringTree).head) + }) + + relations.populate(XuPalmerCandidateArgsTraining, train = isTrainingInstance) + } + + logger.debug("all relations for this test:" + (sentences(a) ~> sentencesToRelations).size) + + if (sentences().size % 1000 == 0) logger.info("loaded graphs in memory:" + sentences().size) + } + if (!testOnly) { logger.info(s"Reading training data from sections $TRAIN_SECTION_S to $TRAIN_SECTION_E") val trainReader = new SRLDataReader(TREEBANK_HOME, PROPBANK_HOME, @@ -97,25 +127,7 @@ object PopulateSRLDataModel extends Logging { printNumbers(trainReader, "training") logger.info("Populating SRLDataModel with training data.") - filteredTa.foreach { a => - gr = new SRLMultiGraphDataModel(parseViewName, frameManager) - if (!useGoldPredicate) { - gr.sentences.populate(Seq(a)) - val predicateTrainCandidates = gr.tokens.getTrainingInstances. - collect { case x: Constituent if gr.posTag(x).startsWith("VB") => x.cloneForNewView(ViewNames.SRL_VERB) } - gr.predicates.populate(predicateTrainCandidates) - } else { - gr.sentences.populate(Seq(a)) - } - logger.debug("gold relations for this train:" + gr.relations().size) - if (!useGoldArgBoundaries) { - val XuPalmerCandidateArgsTraining = gr.predicates.getTrainingInstances.flatMap(x => xuPalmerCandidate(x, (gr.sentences(x.getTextAnnotation) ~> gr.sentencesToStringTree).head)) - gr.relations.populate(XuPalmerCandidateArgsTraining) - } - logger.debug("all relations for this test:" + gr.relations().size) - graphs.addFromModel(gr) - if (graphs.sentences().size % 1000 == 0) logger.info("loaded graphs in memory:" + graphs.sentences().size) - } + filteredTa.foreach(populateDocument(_, isTrainingInstance = true)) } val testReader = new SRLDataReader(TREEBANK_HOME, PROPBANK_HOME, TEST_SECTION, TEST_SECTION) @@ -128,24 +140,6 @@ object PopulateSRLDataModel extends Logging { printNumbers(testReader, "test") logger.info("Populating SRLDataModel with test data.") - filteredTest.foreach { a => - gr = new SRLMultiGraphDataModel(parseViewName, frameManager) - if (!useGoldPredicate) { - gr.sentences.populate(Seq(a), train = false) - val predicateTestCandidates = gr.tokens.getTestingInstances. - collect { case x: Constituent if gr.posTag(x).startsWith("VB") => x.cloneForNewView(ViewNames.SRL_VERB) } - gr.predicates.populate(predicateTestCandidates, train = false) - } else { - gr.sentences.populate(Seq(a), train = false) - } - logger.debug("gold relations for this test:" + gr.relations().size) - if (!useGoldArgBoundaries) { - val XuPalmerCandidateArgsTesting = gr.predicates.getTestingInstances.flatMap(x => xuPalmerCandidate(x, (gr.sentences(x.getTextAnnotation) ~> gr.sentencesToStringTree).head)) - gr.relations.populate(XuPalmerCandidateArgsTesting, train = false) - } - logger.debug("all relations for this test:" + gr.relations().size) - graphs.addFromModel(gr) - } - graphs + filteredTest.foreach(populateDocument(_, isTrainingInstance = false)) } } diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PredArgViewGenerator.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PredArgViewGenerator.scala index 9abf8a81..d94b66d2 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PredArgViewGenerator.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PredArgViewGenerator.scala @@ -18,8 +18,9 @@ import scala.collection.JavaConversions._ */ object PredArgViewGenerator { - def toPredArgList(graph: SRLMultiGraphDataModel, labelProp: TypedProperty[Relation, String]): Iterable[PredicateArgumentView] = { - import graph._ + import SRLMultiGraphDataModel._ + + def toPredArgList(labelProp: TypedProperty[Relation, String]): Iterable[PredicateArgumentView] = { sentences().map { ta => val predArgView: PredicateArgumentView = new PredicateArgumentView(ViewNames.SRL_VERB, ta) (sentences(ta) ~> sentencesToRelations ~> relationsToPredicates).foreach { pred => diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala index b2bc7f7c..9506867a 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala @@ -26,7 +26,7 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: override def addView(ta: TextAnnotation): Unit = { checkPrequisites(ta) - SRLApps.srlDataModelObject.clearInstances() + SRLMultiGraphDataModel.clearInstances() val finalView = new PredicateArgumentView(getViewName, SRLAnnotator.getClass.getCanonicalName, ta, 1.0) @@ -47,16 +47,16 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: assert(finalView.getConstituents.forall(_.getViewName == getViewName), "Verify correct constituent view names.") ta.addView(getViewName, finalView) - SRLApps.srlDataModelObject.clearInstances() + SRLMultiGraphDataModel.clearInstances() } override def initialize(rm: ResourceManager): Unit = { // Load models and other things - ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_dTr/", + ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_dTr/", SRLClassifiers.predicateClassifier) - ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_bTr/", + ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_bTr/", SRLClassifiers.argumentXuIdentifierGivenApredicate) - ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_aTr/", + ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_aTr/", SRLClassifiers.argumentTypeLearner) } @@ -75,14 +75,14 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: * @return Constituents that are not attached to any view yet. */ private def getPredicates(ta: TextAnnotation): Iterable[Constituent] = { - SRLApps.srlDataModelObject.clearInstances() + SRLMultiGraphDataModel.clearInstances() - SRLApps.srlDataModelObject.sentences.populate(Seq(ta), train = false, populateEdge = false) - SRLApps.srlDataModelObject.tokens.populate(CommonSensors.textAnnotationToTokens(ta), train = false, populateEdge = false) - SRLApps.srlDataModelObject.stringTree.populate(Seq(SRLSensors.textAnnotationToStringTree(ta)), train = false, populateEdge = false) + SRLMultiGraphDataModel.sentences.populate(Seq(ta), train = false, populateEdge = false) + SRLMultiGraphDataModel.tokens.populate(CommonSensors.textAnnotationToTokens(ta), train = false, populateEdge = false) + SRLMultiGraphDataModel.stringTree.populate(Seq(SRLSensors.textAnnotationToStringTree(ta)), train = false, populateEdge = false) val predicateCandidates = ta.getView(ViewNames.TOKENS).map(_.cloneForNewView(getViewName)) - SRLApps.srlDataModelObject.predicates.populate(predicateCandidates, train = false, populateEdge = false) + SRLMultiGraphDataModel.predicates.populate(predicateCandidates, train = false, populateEdge = false) // Figure out the constants in Boolean Property // TODO - Constant for Predicate label @@ -97,29 +97,29 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: * @return Relation between unattached predicate and arguments. */ private def getArguments(ta: TextAnnotation, predicate: Constituent): Iterable[Relation] = { - SRLApps.srlDataModelObject.clearInstances() + SRLMultiGraphDataModel.clearInstances() val stringTree = SRLSensors.textAnnotationToStringTree(ta) // Prevent duplicate clearing of graphs. - SRLApps.srlDataModelObject.sentences.populate(Seq(ta), train = false, populateEdge = false) - SRLApps.srlDataModelObject.tokens.populate(CommonSensors.textAnnotationToTokens(ta), train = false, populateEdge = false) - SRLApps.srlDataModelObject.stringTree.populate(Seq(stringTree), train = false, populateEdge = false) - SRLApps.srlDataModelObject.predicates.populate(Seq(predicate), train = false, populateEdge = false) + SRLMultiGraphDataModel.sentences.populate(Seq(ta), train = false, populateEdge = false) + SRLMultiGraphDataModel.tokens.populate(CommonSensors.textAnnotationToTokens(ta), train = false, populateEdge = false) + SRLMultiGraphDataModel.stringTree.populate(Seq(stringTree), train = false, populateEdge = false) + SRLMultiGraphDataModel.predicates.populate(Seq(predicate), train = false, populateEdge = false) val candidateRelations = SRLSensors.xuPalmerCandidate(predicate, stringTree) - SRLApps.srlDataModelObject.arguments.populate(candidateRelations.map(_.getTarget), train = false, populateEdge = false) - SRLApps.srlDataModelObject.relations.populate(candidateRelations, train = false, populateEdge = false) + SRLMultiGraphDataModel.arguments.populate(candidateRelations.map(_.getTarget), train = false, populateEdge = false) + SRLMultiGraphDataModel.relations.populate(candidateRelations, train = false, populateEdge = false) val finalRelationList = candidateRelations.filter({ candidate: Relation => SRLClassifiers.argumentXuIdentifierGivenApredicate(candidate) == "true" }) - SRLApps.srlDataModelObject.arguments.clear() - SRLApps.srlDataModelObject.arguments.populate(finalRelationList.map(_.getTarget), train = false, populateEdge = false) + SRLMultiGraphDataModel.arguments.clear() + SRLMultiGraphDataModel.arguments.populate(finalRelationList.map(_.getTarget), train = false, populateEdge = false) - SRLApps.srlDataModelObject.relations.clear() - SRLApps.srlDataModelObject.relations.populate(finalRelationList, train = false, populateEdge = false) + SRLMultiGraphDataModel.relations.clear() + SRLMultiGraphDataModel.relations.populate(finalRelationList, train = false, populateEdge = false) finalRelationList.flatMap { relation: Relation => val label = SRLClassifiers.argumentTypeLearner(relation) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala index 9970b93b..51218de2 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala @@ -9,8 +9,9 @@ package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling import java.io.File import edu.illinois.cs.cogcomp.core.datastructures.ViewNames -import edu.illinois.cs.cogcomp.saul.classifier.{ ClassifierUtils, JointTrainSparseNetwork } +import edu.illinois.cs.cogcomp.saul.classifier.{ClassifierUtils, JointTrainSparseNetwork} import edu.illinois.cs.cogcomp.saul.util.Logging +import edu.illinois.cs.cogcomp.saulexamples.data.SRLFrameManager import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLClassifiers._ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLConstrainedClassifiers.argTypeConstraintClassifier @@ -66,6 +67,7 @@ object SRLscalaConfigurator { // Should we train an argument type classifier val SRL_TRAIN_ARG_TYPE = true + lazy val SRL_FRAME_MANAGER: SRLFrameManager = new SRLFrameManager(PROPBANK_HOME) } object SRLApps extends Logging { @@ -93,9 +95,9 @@ object SRLApps extends Logging { logger.info("population starts.") // Here, the data is loaded into the graph - val srlDataModelObject = PopulateSRLDataModel(testOnly = TEST_MODE, SRL_GOLD_PREDICATES, SRL_GOLD_ARG_BOUNDARIES) + PopulateSRLDataModel(testOnly = TEST_MODE, SRL_GOLD_PREDICATES, SRL_GOLD_ARG_BOUNDARIES) - import srlDataModelObject._ + import SRLMultiGraphDataModel._ logger.info("all relations number after population:" + relations().size) logger.info("all sentences number after population:" + sentences().size) @@ -106,8 +108,9 @@ object SRLApps extends Logging { object RunningApps extends App with Logging { import SRLApps._ - import SRLApps.srlDataModelObject._ import SRLscalaConfigurator._ + import SRLMultiGraphDataModel._ + // TRAINING if (!TEST_MODE) { expName match { @@ -160,7 +163,7 @@ object RunningApps extends App with Logging { argumentTypeLearner.save() case "pTr" => - ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_bTr/", argumentXuIdentifierGivenApredicate) + ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_bTr/", argumentXuIdentifierGivenApredicate) val training = relations.getTrainingInstances.filter(x => argumentXuIdentifierGivenApredicate(x).equals("true")) argumentTypeLearner.modelDir = modelDir argumentTypeLearner.learn(100, training) @@ -197,27 +200,27 @@ object RunningApps extends App with Logging { (SRL_TEST_PIPELINE, SRL_TEST_CONSTRAINTS) match { case (true, true) => - ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_bTr/", argumentXuIdentifierGivenApredicate) - ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_aTr/", argumentTypeLearner) + ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_bTr/", argumentXuIdentifierGivenApredicate) + ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_aTr/", argumentTypeLearner) argumentTypeLearner.test( prediction = typeArgumentPipeGivenGoldPredicateConstrained, groundTruth = argumentLabelGold, exclude = "candidate" ) case (true, false) => - ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_bTr/", argumentXuIdentifierGivenApredicate) - ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_aTr/", argumentTypeLearner) + ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_bTr/", argumentXuIdentifierGivenApredicate) + ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_aTr/", argumentTypeLearner) argumentTypeLearner.test( prediction = typeArgumentPipeGivenGoldPredicate, groundTruth = argumentLabelGold, exclude = "candidate" ) case (false, true) => - ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_aTr/", argumentTypeLearner) + ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_aTr/", argumentTypeLearner) argTypeConstraintClassifier.test(outputGranularity = 100, exclude = "candidate") case (false, false) => - ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_aTr/", argumentTypeLearner) + ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_aTr/", argumentTypeLearner) argumentTypeLearner.test(exclude = "candidate") } } diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLClassifiers.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLClassifiers.scala index a4d4155a..d98480de 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLClassifiers.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLClassifiers.scala @@ -14,7 +14,8 @@ import edu.illinois.cs.cogcomp.saul.datamodel.property.Property /** Created by Parisa on 12/30/15. */ object SRLClassifiers { - import SRLApps.srlDataModelObject._ + import SRLMultiGraphDataModel._ + //TODO This needs to be overriden by the user; change it to be dynamic val parameters = new SparseAveragedPerceptron.Parameters() object predicateClassifier extends Learnable[Constituent](predicates, parameters) { diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala index cab81f68..755d5c73 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala @@ -15,7 +15,8 @@ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLConstrai /** Created by Parisa on 12/27/15. */ object SRLConstrainedClassifiers { - import SRLApps.srlDataModelObject._ + import SRLMultiGraphDataModel._ + val erSolver = new OJalgoHook object argTypeConstraintClassifier extends ConstrainedClassifier[Relation, TextAnnotation](argumentTypeLearner) { diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstraints.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstraints.scala index 5a494d02..219bd63f 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstraints.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstraints.scala @@ -12,10 +12,12 @@ import edu.illinois.cs.cogcomp.lbjava.infer.{ FirstOrderConstant, FirstOrderCons import edu.illinois.cs.cogcomp.saul.classifier.ConstrainedClassifier import edu.illinois.cs.cogcomp.saul.constraint.ConstraintTypeConversion._ import edu.illinois.cs.cogcomp.saulexamples.data.XuPalmerCandidateGenerator -import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLApps.srlDataModelObject._ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLClassifiers.{ argumentTypeLearner, argumentXuIdentifierGivenApredicate, predicateClassifier } import scala.collection.JavaConversions._ + +import SRLMultiGraphDataModel._ + /** Created by Parisa on 12/23/15. */ object SRLConstraints { diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala index 3945d63d..0bfcc053 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala @@ -13,7 +13,6 @@ import edu.illinois.cs.cogcomp.edison.features.factory._ import edu.illinois.cs.cogcomp.nlp.corpusreaders.AbstractSRLAnnotationReader import edu.illinois.cs.cogcomp.saul.datamodel.DataModel import edu.illinois.cs.cogcomp.saul.datamodel.property.PairwiseConjunction -import edu.illinois.cs.cogcomp.saulexamples.data.SRLFrameManager import edu.illinois.cs.cogcomp.saulexamples.nlp.CommonSensors import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLSensors._ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLClassifiers._ @@ -21,7 +20,11 @@ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLConstrai import scala.collection.JavaConversions._ -class SRLMultiGraphDataModel(parseViewName: String = null, frameManager: SRLFrameManager = null) extends DataModel { +object SRLMultiGraphDataModel extends DataModel { + val parseViewName = SRLscalaConfigurator.SRL_PARSE_VIEW + val frameManager = SRLscalaConfigurator.SRL_FRAME_MANAGER + + // Nodes val predicates = node[Constituent]((x: Constituent) => x.getTextAnnotation.getCorpusId + ":" + x.getTextAnnotation.getId + ":" + x.getSpan) @@ -36,12 +39,16 @@ class SRLMultiGraphDataModel(parseViewName: String = null, frameManager: SRLFram val tokens = node[Constituent]((x: Constituent) => x.getTextAnnotation.getCorpusId + ":" + x.getTextAnnotation.getId + ":" + x.getSpan) + // Edges + val sentencesToStringTree = edge(sentences, stringTree) val sentencesToTokens = edge(sentences, tokens) val sentencesToRelations = edge(sentences, relations) val relationsToPredicates = edge(relations, predicates) val relationsToArguments = edge(relations, arguments) + // Sensors + sentencesToTokens.addSensor(CommonSensors.textAnnotationToTokens _) sentencesToRelations.addSensor(textAnnotationToRelation _) sentencesToRelations.addSensor(textAnnotationToRelationMatch _) @@ -49,6 +56,8 @@ class SRLMultiGraphDataModel(parseViewName: String = null, frameManager: SRLFram relationsToPredicates.addSensor(relToPredicate _) sentencesToStringTree.addSensor(textAnnotationToStringTree _) + // Properties + /** This can be applied to both predicates and arguments */ val address = property(predicates, "add") { x: Constituent => x.getTextAnnotation.getCorpusId + ":" + x.getTextAnnotation.getId + ":" + x.getSpan diff --git a/saul-examples/src/test/java/edu/illinois/cs/cogcomp/saulexamples/data/SRLFrameManagerTest.java b/saul-examples/src/test/java/edu/illinois/cs/cogcomp/saulexamples/data/SRLFrameManagerTest.java index 58db6ea2..081030f7 100644 --- a/saul-examples/src/test/java/edu/illinois/cs/cogcomp/saulexamples/data/SRLFrameManagerTest.java +++ b/saul-examples/src/test/java/edu/illinois/cs/cogcomp/saulexamples/data/SRLFrameManagerTest.java @@ -6,8 +6,7 @@ */ package edu.illinois.cs.cogcomp.saulexamples.data; -import edu.illinois.cs.cogcomp.core.utilities.configuration.ResourceManager; -import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLConfigurator; +import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLscalaConfigurator; import org.junit.Before; import org.junit.Test; @@ -24,8 +23,7 @@ public class SRLFrameManagerTest { @Before public void setUp() throws Exception { - ResourceManager rm = new SRLConfigurator().getDefaultConfig(); - frameManager = new SRLFrameManager(rm.getString(SRLConfigurator.PROPBANK_HOME.key)); + frameManager = new SRLFrameManager(SRLscalaConfigurator.PROPBANK_HOME()); } @Test diff --git a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/DataModelTest.scala b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/DataModelTest.scala index b2e69b30..90d1b5a7 100644 --- a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/DataModelTest.scala +++ b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/DataModelTest.scala @@ -11,14 +11,13 @@ import edu.illinois.cs.cogcomp.core.utilities.DummyTextAnnotationGenerator import org.scalatest.{ FlatSpec, Matchers } class DataModelTest extends FlatSpec with Matchers { - val rm = new SRLConfigurator().getDefaultConfig - val parseViewName = rm.getString(SRLConfigurator.SRL_PARSE_VIEW) - val SRLDataModel = new SRLMultiGraphDataModel(parseViewName) - import SRLDataModel._ + import SRLMultiGraphDataModel._ + val viewsToAdd = Array( ViewNames.LEMMA, ViewNames.POS, ViewNames.SHALLOW_PARSE, ViewNames.PARSE_GOLD, ViewNames.SRL_VERB ) + val ta = { val taTmp = DummyTextAnnotationGenerator.generateAnnotatedTextAnnotation(viewsToAdd, false, 1) // included here, in order to make sure population is done before making any queries diff --git a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala index 452878d5..12511633 100644 --- a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala +++ b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala @@ -13,7 +13,7 @@ import org.scalatest.{ FlatSpec, Matchers } class ModelsTest extends FlatSpec with Matchers { "argument type classifier (aTr)" should "work." in { - ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_aTr/", argumentTypeLearner) + ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_aTr/", argumentTypeLearner) val results = argumentTypeLearner.test(exclude = "candidate") results.perLabel .filter(!_.f1.isNaN) @@ -29,7 +29,7 @@ class ModelsTest extends FlatSpec with Matchers { } "predicate identifier (dTr)" should "perform higher than 0.98." in { - ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_dTr/", predicateClassifier) + ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_dTr/", predicateClassifier) val results = predicateClassifier.test() results.perLabel.foreach { result => @@ -39,7 +39,7 @@ class ModelsTest extends FlatSpec with Matchers { "L+I argument type classifier (aTr)" should "work." in { //TODO solve the test problem with Gurobi licencing vs. OJalgoHook inefficiency - // ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_aTr/", argumentTypeLearner) + // ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_aTr/", argumentTypeLearner) // val scores = argTypeConstraintClassifier.test(exclude = "candidate") // scores.foreach { // case (label, score) => { @@ -54,7 +54,7 @@ class ModelsTest extends FlatSpec with Matchers { } "argument identifier (bTr)" should "perform higher than 0.95." in { - ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_bTr/", argumentXuIdentifierGivenApredicate) + ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_bTr/", argumentXuIdentifierGivenApredicate) val results = argumentXuIdentifierGivenApredicate.test() results.perLabel.foreach { result => @@ -63,7 +63,7 @@ class ModelsTest extends FlatSpec with Matchers { } "argument identifier (cTr) trained with XuPalmer" should "perform higher than 0.9." in { - ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_cTr/", argumentTypeLearner) + ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_cTr/", argumentTypeLearner) val results = argumentTypeLearner.test() results.perLabel.foreach { result => @@ -77,7 +77,7 @@ class ModelsTest extends FlatSpec with Matchers { } "argument identifier (fTr) trained with XuPalmer and candidate predicates" should "work." in { - ClassifierUtils.LoadClassifier(SRLConfigurator.SRL_JAR_MODEL_PATH.value + "/models_fTr/", argumentTypeLearner) + ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_fTr/", argumentTypeLearner) val results = argumentTypeLearner.test(exclude = "candidate") results.perLabel.foreach { result => From 02a0e967bcf90ac4c5d4dce9126782cb3de49d36 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Fri, 14 Apr 2017 00:25:25 -0500 Subject: [PATCH 04/28] Formatting fixes. --- .../PopulateSRLDataModel.scala | 2 +- .../SemanticRoleLabeling/SRLAnnotator.scala | 31 +++++++++++-------- .../nlp/SemanticRoleLabeling/SRLApps.scala | 2 +- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala index cad45e69..16c6f30b 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala @@ -10,7 +10,7 @@ import java.util.Properties import edu.illinois.cs.cogcomp.annotation.AnnotatorException import edu.illinois.cs.cogcomp.core.datastructures.ViewNames -import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{Constituent, TextAnnotation, TreeView} +import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{ Constituent, TextAnnotation, TreeView } import edu.illinois.cs.cogcomp.core.datastructures.trees.Tree import edu.illinois.cs.cogcomp.curator.CuratorConfigurator._ import edu.illinois.cs.cogcomp.edison.annotators.ClauseViewGenerator diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala index 9506867a..b0466ad9 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala @@ -6,8 +6,8 @@ */ package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling -import edu.illinois.cs.cogcomp.annotation.{Annotator, AnnotatorConfigurator, AnnotatorException} -import edu.illinois.cs.cogcomp.core.datastructures.{IntPair, ViewNames} +import edu.illinois.cs.cogcomp.annotation.{ Annotator, AnnotatorConfigurator, AnnotatorException } +import edu.illinois.cs.cogcomp.core.datastructures.{ IntPair, ViewNames } import edu.illinois.cs.cogcomp.core.datastructures.textannotation._ import edu.illinois.cs.cogcomp.core.utilities.configuration.ResourceManager import edu.illinois.cs.cogcomp.edison.annotators.ClauseViewGenerator @@ -41,7 +41,8 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: predicate, argumentList.map(_.getTarget).toList, argumentList.map(_.getRelationName).toArray, - argumentList.map(_.getScore).toArray) + argumentList.map(_.getScore).toArray + ) }) assert(finalView.getConstituents.forall(_.getViewName == getViewName), "Verify correct constituent view names.") @@ -52,12 +53,18 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: override def initialize(rm: ResourceManager): Unit = { // Load models and other things - ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_dTr/", - SRLClassifiers.predicateClassifier) - ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_bTr/", - SRLClassifiers.argumentXuIdentifierGivenApredicate) - ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_aTr/", - SRLClassifiers.argumentTypeLearner) + ClassifierUtils.LoadClassifier( + SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_dTr/", + SRLClassifiers.predicateClassifier + ) + ClassifierUtils.LoadClassifier( + SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_bTr/", + SRLClassifiers.argumentXuIdentifierGivenApredicate + ) + ClassifierUtils.LoadClassifier( + SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_aTr/", + SRLClassifiers.argumentTypeLearner + ) } def checkPrequisites(ta: TextAnnotation): Unit = { @@ -70,8 +77,7 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: assert(ta.hasView(ViewNames.CLAUSES_STANFORD)) } - /** - * @param ta Input Text Annotation instance. + /** @param ta Input Text Annotation instance. * @return Constituents that are not attached to any view yet. */ private def getPredicates(ta: TextAnnotation): Iterable[Constituent] = { @@ -91,8 +97,7 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: }) } - /** - * @param ta Input Text Annotation instance. + /** @param ta Input Text Annotation instance. * @param predicate Input Predicate instance. * @return Relation between unattached predicate and arguments. */ diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala index 51218de2..f32e6de4 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala @@ -9,7 +9,7 @@ package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling import java.io.File import edu.illinois.cs.cogcomp.core.datastructures.ViewNames -import edu.illinois.cs.cogcomp.saul.classifier.{ClassifierUtils, JointTrainSparseNetwork} +import edu.illinois.cs.cogcomp.saul.classifier.{ ClassifierUtils, JointTrainSparseNetwork } import edu.illinois.cs.cogcomp.saul.util.Logging import edu.illinois.cs.cogcomp.saulexamples.data.SRLFrameManager import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLClassifiers._ From b28e070e97151ebefda988abb8b09a8bef94dddc Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Fri, 14 Apr 2017 00:41:57 -0500 Subject: [PATCH 05/28] Fix some unit tests. --- .../nlp/SemanticRoleLabeling/DataModelTest.scala | 11 ++++------- .../nlp/SemanticRoleLabeling/ModelsTest.scala | 9 +++++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/DataModelTest.scala b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/DataModelTest.scala index 90d1b5a7..2e0796e9 100644 --- a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/DataModelTest.scala +++ b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/DataModelTest.scala @@ -7,6 +7,7 @@ package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling import edu.illinois.cs.cogcomp.core.datastructures.ViewNames +import edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation import edu.illinois.cs.cogcomp.core.utilities.DummyTextAnnotationGenerator import org.scalatest.{ FlatSpec, Matchers } @@ -18,12 +19,9 @@ class DataModelTest extends FlatSpec with Matchers { ViewNames.PARSE_GOLD, ViewNames.SRL_VERB ) - val ta = { - val taTmp = DummyTextAnnotationGenerator.generateAnnotatedTextAnnotation(viewsToAdd, false, 1) - // included here, in order to make sure population is done before making any queries - sentences.populate(List(taTmp)) - taTmp - } + SRLMultiGraphDataModel.clearInstances() + val taTmp: TextAnnotation = DummyTextAnnotationGenerator.generateAnnotatedTextAnnotation(viewsToAdd, false, 1) + sentences.populate(List(taTmp), train = false) "graph population" should "be correct" in { sentences().size should be(1) @@ -104,5 +102,4 @@ class DataModelTest extends FlatSpec with Matchers { (relations() prop containsNEG).toSet should be(Set("", "")) (relations() prop containsMOD).toSet should be(Set("", "")) } - } diff --git a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala index 12511633..0338b179 100644 --- a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala +++ b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala @@ -6,12 +6,21 @@ */ package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling +import edu.illinois.cs.cogcomp.core.datastructures.ViewNames import edu.illinois.cs.cogcomp.saul.classifier.ClassifierUtils import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLClassifiers._ import org.scalatest.{ FlatSpec, Matchers } class ModelsTest extends FlatSpec with Matchers { + val viewsToAdd = Array( + ViewNames.LEMMA, ViewNames.POS, ViewNames.SHALLOW_PARSE, + ViewNames.PARSE_GOLD, ViewNames.SRL_VERB + ) + + SRLMultiGraphDataModel.clearInstances() + PopulateSRLDataModel(testOnly = true, useGoldPredicate = true, useGoldArgBoundaries = true) + "argument type classifier (aTr)" should "work." in { ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_aTr/", argumentTypeLearner) val results = argumentTypeLearner.test(exclude = "candidate") From 78ca26c6304d0f8d95fd8fa32988a008ac534066 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Fri, 14 Apr 2017 01:56:29 -0500 Subject: [PATCH 06/28] Add a unit test for the annotator. --- .../SemanticRoleLabeling/SRLAnnotator.scala | 57 ++++++++++++++----- .../SemanticRoleLabeling/AnnotatorTest.scala | 38 +++++++++++++ 2 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/AnnotatorTest.scala diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala index b0466ad9..09f1cf6c 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala @@ -6,11 +6,12 @@ */ package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling -import edu.illinois.cs.cogcomp.annotation.{ Annotator, AnnotatorConfigurator, AnnotatorException } -import edu.illinois.cs.cogcomp.core.datastructures.{ IntPair, ViewNames } +import edu.illinois.cs.cogcomp.annotation.{Annotator, AnnotatorConfigurator, AnnotatorException} +import edu.illinois.cs.cogcomp.core.datastructures.ViewNames import edu.illinois.cs.cogcomp.core.datastructures.textannotation._ import edu.illinois.cs.cogcomp.core.utilities.configuration.ResourceManager import edu.illinois.cs.cogcomp.edison.annotators.ClauseViewGenerator +import edu.illinois.cs.cogcomp.nlp.corpusreaders.AbstractSRLAnnotationReader import edu.illinois.cs.cogcomp.saul.classifier.ClassifierUtils import edu.illinois.cs.cogcomp.saulexamples.nlp.CommonSensors @@ -21,7 +22,13 @@ class SRLAnnotatorConfigurator extends AnnotatorConfigurator class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: ResourceManager = new SRLAnnotatorConfigurator().getDefaultConfig) extends Annotator(finalViewName, SRLAnnotator.requiredViews, resourceManager) { val requiredViewSet: Set[String] = getRequiredViews.toSet - lazy val clauseViewGenerator = ClauseViewGenerator.STANFORD + + lazy val clauseViewGenerator: ClauseViewGenerator = { + SRLscalaConfigurator.SRL_PARSE_VIEW match { + case ViewNames.PARSE_GOLD => new ClauseViewGenerator(ViewNames.PARSE_GOLD, "CLAUSES_GOLD") + case ViewNames.PARSE_STANFORD => ClauseViewGenerator.STANFORD + } + } override def addView(ta: TextAnnotation): Unit = { checkPrequisites(ta) @@ -31,9 +38,9 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: val finalView = new PredicateArgumentView(getViewName, SRLAnnotator.getClass.getCanonicalName, ta, 1.0) // Get Predicates in the sentence. - val predicates = getPredicates(ta) + val allPredicates = getPredicates(ta) - predicates.foreach({ predicate: Constituent => + allPredicates.foreach({ predicate: Constituent => // Get arguments for each predicate detected. val argumentList = getArguments(ta, predicate) @@ -43,6 +50,16 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: argumentList.map(_.getRelationName).toArray, argumentList.map(_.getScore).toArray ) + + // Add additional attributes + val lemmaOrToken = ta.getView(ViewNames.LEMMA) + .getConstituentsCovering(predicate) + .headOption + .orElse(ta.getView(ViewNames.TOKENS).getConstituentsCovering(predicate).headOption) + + // TODO - Fix this with correct Sense identifier + predicate.addAttribute(AbstractSRLAnnotationReader.SenseIdentifier, "xx") + predicate.addAttribute(AbstractSRLAnnotationReader.LemmaIdentifier, lemmaOrToken.map(_.getLabel).getOrElse("")) }) assert(finalView.getConstituents.forall(_.getViewName == getViewName), "Verify correct constituent view names.") @@ -74,7 +91,6 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: } clauseViewGenerator.addView(ta) - assert(ta.hasView(ViewNames.CLAUSES_STANFORD)) } /** @param ta Input Text Annotation instance. @@ -87,10 +103,14 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: SRLMultiGraphDataModel.tokens.populate(CommonSensors.textAnnotationToTokens(ta), train = false, populateEdge = false) SRLMultiGraphDataModel.stringTree.populate(Seq(SRLSensors.textAnnotationToStringTree(ta)), train = false, populateEdge = false) - val predicateCandidates = ta.getView(ViewNames.TOKENS).map(_.cloneForNewView(getViewName)) + // TODO - Shim to select only Verb for now. + val predicateCandidates = ta.getView(ViewNames.POS) + .getConstituents + .filter(_.getLabel.startsWith("VB")) + .map(_.cloneForNewView(getViewName)) SRLMultiGraphDataModel.predicates.populate(predicateCandidates, train = false, populateEdge = false) - // Figure out the constants in Boolean Property + // TODO - Figure out the constants in Boolean Property // TODO - Constant for Predicate label predicateCandidates.filter(SRLClassifiers.predicateClassifier(_) == "true").map({ candidate: Constituent => candidate.cloneForNewViewWithDestinationLabel(getViewName, "Predicate") @@ -128,16 +148,27 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: finalRelationList.flatMap { relation: Relation => val label = SRLClassifiers.argumentTypeLearner(relation) - if (label == "candidate") None else Some(SRLAnnotator.cloneRelationWithNewLabel(relation, label)) + if (label == "candidate") + None + else + Some(SRLAnnotator.cloneRelationWithNewLabelAndArgument(relation, label, getViewName)) } } } object SRLAnnotator { - private val requiredViews = Array(ViewNames.POS, ViewNames.LEMMA, ViewNames.SHALLOW_PARSE, ViewNames.PARSE_STANFORD) - - private def cloneRelationWithNewLabel(sourceRelation: Relation, label: String): Relation = { - val newRelation = new Relation(label, sourceRelation.getSource, sourceRelation.getTarget, sourceRelation.getScore) + private val requiredViews = Array( + ViewNames.POS, + ViewNames.LEMMA, + ViewNames.SHALLOW_PARSE, + SRLscalaConfigurator.SRL_PARSE_VIEW + ) + + private def cloneRelationWithNewLabelAndArgument(sourceRelation: Relation, + label: String, + targetViewName: String): Relation = { + val newTargetConstituent = sourceRelation.getTarget.cloneForNewView(targetViewName) + val newRelation = new Relation(label, sourceRelation.getSource, newTargetConstituent, sourceRelation.getScore) sourceRelation.getAttributeKeys.foreach({ key: String => newRelation.setAttribute(key, sourceRelation.getAttribute(key)) }) diff --git a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/AnnotatorTest.scala b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/AnnotatorTest.scala new file mode 100644 index 00000000..75aabeab --- /dev/null +++ b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/AnnotatorTest.scala @@ -0,0 +1,38 @@ +/** This software is released under the University of Illinois/Research and Academic Use License. See + * the LICENSE file in the root folder for details. Copyright (c) 2016 + * + * Developed by: The Cognitive Computations Group, University of Illinois at Urbana-Champaign + * http://cogcomp.cs.illinois.edu/ + */ +package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling + +import edu.illinois.cs.cogcomp.core.datastructures.ViewNames +import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{PredicateArgumentView, TextAnnotation} +import edu.illinois.cs.cogcomp.core.utilities.DummyTextAnnotationGenerator +import edu.illinois.cs.cogcomp.nlp.corpusreaders.AbstractSRLAnnotationReader +import org.scalatest.{FlatSpec, Matchers} + +class AnnotatorTest extends FlatSpec with Matchers { + val textAnnotation: TextAnnotation = DummyTextAnnotationGenerator.generateAnnotatedTextAnnotation( + Array(ViewNames.POS, ViewNames.LEMMA, ViewNames.SHALLOW_PARSE, ViewNames.PARSE_GOLD), + false, + 1 + ) + + "SRLAnnotator" should "work" in { + val annotator = new SRLAnnotator(ViewNames.SRL_VERB) + annotator.addView(textAnnotation) + + assert(textAnnotation.hasView(ViewNames.SRL_VERB), "SRL_VERB view should exist after annotation.") + + val srlView = textAnnotation.getView(ViewNames.SRL_VERB).asInstanceOf[PredicateArgumentView] + assert(srlView.getPredicates.size() == 1) + + val verbPredicate = srlView.getPredicates.get(0) + assert(srlView.getArguments(verbPredicate).size() >= 1) + + // Required attributes are populated. + assert(verbPredicate.hasAttribute(AbstractSRLAnnotationReader.LemmaIdentifier)) + assert(verbPredicate.hasAttribute(AbstractSRLAnnotationReader.SenseIdentifier)) + } +} From 0abd4d43d9c5059070d97e27ba19fe249f150f20 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Sat, 15 Apr 2017 20:54:32 -0500 Subject: [PATCH 07/28] Update dependencies. --- build.sbt | 12 ++++++------ .../cs/cogcomp/saulexamples/nlp/CommonSensors.scala | 5 ++--- .../QuestionTypeClassificationSensors.scala | 2 +- .../SemanticRoleLabeling/PopulateSRLDataModel.scala | 2 +- .../nlp/SemanticRoleLabeling/SRLAnnotator.scala | 2 +- .../SpatialRoleLabeling/SpRLDataModelReader.scala | 2 +- .../saulexamples/nlp/TextAnnotationFactory.scala | 4 ++-- 7 files changed, 14 insertions(+), 15 deletions(-) diff --git a/build.sbt b/build.sbt index 3a188a46..3ab90a4d 100644 --- a/build.sbt +++ b/build.sbt @@ -3,8 +3,8 @@ import sbtrelease.ReleaseStateTransformations._ scalaVersion in ThisBuild := "2.11.7" -val cogcompNLPVersion = "3.0.71" -val cogcompPipelineVersion = "0.1.25" +val cogcompNLPVersion = "3.1.7" +val lbjavaVersion = "1.2.27" val ccgGroupId = "edu.illinois.cs.cogcomp" val headerMsg = """/** This software is released under the University of Illinois/Research and Academic Use License. See | * the LICENSE file in the root folder for details. Copyright (c) 2016 @@ -61,12 +61,12 @@ lazy val commonSettings = Seq( ), javaOptions ++= List("-Xmx11g"), libraryDependencies ++= Seq( - ccgGroupId % "LBJava" % "1.2.25" withSources, + ccgGroupId % "LBJava" % lbjavaVersion withSources, ccgGroupId % "illinois-core-utilities" % cogcompNLPVersion withSources, - "com.gurobi" % "gurobi" % "6.0", + "com.gurobi" % "gurobi" % "7.0.1", "org.apache.commons" % "commons-math3" % "3.0", "org.scalatest" % "scalatest_2.11" % "2.2.4", - "ch.qos.logback" % "logback-classic" % "1.1.7" + "ch.qos.logback" % "logback-classic" % "1.2.3" ), fork := true, connectInput in run := true, @@ -95,7 +95,7 @@ lazy val saulExamples = (project in file("saul-examples")). settings( name := "saul-examples", libraryDependencies ++= Seq( - ccgGroupId % "illinois-nlp-pipeline" % cogcompPipelineVersion withSources, + ccgGroupId % "illinois-nlp-pipeline" % cogcompNLPVersion withSources, ccgGroupId % "illinois-curator" % cogcompNLPVersion, ccgGroupId % "illinois-edison" % cogcompNLPVersion, ccgGroupId % "illinois-corpusreaders" % cogcompNLPVersion, diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/CommonSensors.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/CommonSensors.scala index 19d4c902..0b9c8892 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/CommonSensors.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/CommonSensors.scala @@ -12,7 +12,7 @@ import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{ Constituent, import edu.illinois.cs.cogcomp.curator.CuratorFactory import edu.illinois.cs.cogcomp.edison.features.factory.WordFeatureExtractorFactory import edu.illinois.cs.cogcomp.edison.features.{ FeatureExtractor, FeatureUtilities } -import edu.illinois.cs.cogcomp.nlp.pipeline.IllinoisPipelineFactory +import edu.illinois.cs.cogcomp.pipeline.main.PipelineFactory import edu.illinois.cs.cogcomp.saul.util.Logging import edu.illinois.cs.cogcomp.saulexamples.data.Document @@ -87,9 +87,8 @@ object CommonSensors extends Logging { } def annotateWithPipeline(content: String, id: String): TextAnnotation = { - val annotatorService = IllinoisPipelineFactory.buildPipeline() + val annotatorService = PipelineFactory.buildPipeline() processDocumentWith(annotatorService, "corpus", id, content) } - } diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/QuestionTypeClassification/QuestionTypeClassificationSensors.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/QuestionTypeClassification/QuestionTypeClassificationSensors.scala index 6271a5cd..adc7732f 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/QuestionTypeClassification/QuestionTypeClassificationSensors.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/QuestionTypeClassification/QuestionTypeClassificationSensors.scala @@ -10,7 +10,7 @@ import java.io.File import java.util.Properties import edu.illinois.cs.cogcomp.core.datastructures.ViewNames -import edu.illinois.cs.cogcomp.nlp.common.PipelineConfigurator._ +import edu.illinois.cs.cogcomp.pipeline.common.PipelineConfigurator._ import edu.illinois.cs.cogcomp.saulexamples.nlp.TextAnnotationFactory import scala.io.Source diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala index 16c6f30b..ef0d80d3 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala @@ -14,7 +14,7 @@ import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{ Constituent, import edu.illinois.cs.cogcomp.core.datastructures.trees.Tree import edu.illinois.cs.cogcomp.curator.CuratorConfigurator._ import edu.illinois.cs.cogcomp.edison.annotators.ClauseViewGenerator -import edu.illinois.cs.cogcomp.nlp.common.PipelineConfigurator._ +import edu.illinois.cs.cogcomp.pipeline.common.PipelineConfigurator._ import edu.illinois.cs.cogcomp.nlp.utilities.ParseUtils import edu.illinois.cs.cogcomp.saul.util.Logging import edu.illinois.cs.cogcomp.saulexamples.data.SRLDataReader diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala index 09f1cf6c..8e2f5b76 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala @@ -170,7 +170,7 @@ object SRLAnnotator { val newTargetConstituent = sourceRelation.getTarget.cloneForNewView(targetViewName) val newRelation = new Relation(label, sourceRelation.getSource, newTargetConstituent, sourceRelation.getScore) sourceRelation.getAttributeKeys.foreach({ key: String => - newRelation.setAttribute(key, sourceRelation.getAttribute(key)) + newRelation.addAttribute(key, sourceRelation.getAttribute(key)) }) newRelation } diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SpatialRoleLabeling/SpRLDataModelReader.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SpatialRoleLabeling/SpRLDataModelReader.scala index 1ca7e312..ba17d733 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SpatialRoleLabeling/SpRLDataModelReader.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SpatialRoleLabeling/SpRLDataModelReader.scala @@ -11,7 +11,7 @@ import java.util.Properties import edu.illinois.cs.cogcomp.annotation.AnnotatorService import edu.illinois.cs.cogcomp.core.datastructures.IntPair import edu.illinois.cs.cogcomp.core.datastructures.textannotation._ -import edu.illinois.cs.cogcomp.nlp.common.PipelineConfigurator._ +import edu.illinois.cs.cogcomp.pipeline.common.PipelineConfigurator._ import edu.illinois.cs.cogcomp.saul.util.Logging import edu.illinois.cs.cogcomp.saulexamples.nlp.SpatialRoleLabeling.SpRL2013.SpRL2013Document import edu.illinois.cs.cogcomp.saulexamples.nlp.TextAnnotationFactory diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/TextAnnotationFactory.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/TextAnnotationFactory.scala index 2620b50a..58d78e63 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/TextAnnotationFactory.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/TextAnnotationFactory.scala @@ -12,7 +12,7 @@ import edu.illinois.cs.cogcomp.annotation.AnnotatorService import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{ TextAnnotation, TokenLabelView } import edu.illinois.cs.cogcomp.core.utilities.configuration.{ Configurator, Property, ResourceManager } import edu.illinois.cs.cogcomp.curator.{ CuratorConfigurator, CuratorFactory } -import edu.illinois.cs.cogcomp.nlp.pipeline.IllinoisPipelineFactory +import edu.illinois.cs.cogcomp.pipeline.main.PipelineFactory /** Created by taher on 7/30/16. */ @@ -36,7 +36,7 @@ object TextAnnotationFactory { as.createBasicTextAnnotation(corpusId, textId, text) def createPipelineAnnotatorService(settings: Properties): AnnotatorService = { - IllinoisPipelineFactory.buildPipeline( + PipelineFactory.buildPipeline( new CuratorConfigurator().getConfig(new ResourceManager(settings)) ) } From 26e8907afbd590879fc4531978b84e050f77aad6 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Mon, 17 Apr 2017 19:15:51 -0500 Subject: [PATCH 08/28] Some fixes to SRLApps and Pipeline usage. --- .../QuestionTypeClassificationSensors.scala | 4 +++- .../PopulateSRLDataModel.scala | 7 ++++--- .../nlp/SemanticRoleLabeling/SRLApps.scala | 14 ++++++-------- .../SpatialRoleLabeling/SpRLDataModelReader.scala | 3 ++- .../saulexamples/nlp/TextAnnotationFactory.scala | 9 +++++---- .../nlp/SemanticRoleLabeling/ConstraintsTest.scala | 11 +++++++---- 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/QuestionTypeClassification/QuestionTypeClassificationSensors.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/QuestionTypeClassification/QuestionTypeClassificationSensors.scala index adc7732f..a69a55c0 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/QuestionTypeClassification/QuestionTypeClassificationSensors.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/QuestionTypeClassification/QuestionTypeClassificationSensors.scala @@ -23,7 +23,9 @@ object QuestionTypeClassificationSensors { lazy val pipeline = { val settings = new Properties() - TextAnnotationFactory.disableSettings(settings, USE_SRL_NOM) + TextAnnotationFactory.enableSettings(settings, USE_POS, USE_DEP, USE_LEMMA, USE_SHALLOW_PARSE, USE_NER_CONLL, + USE_NER_ONTONOTES, USE_STANFORD_DEP, USE_STANFORD_PARSE, USE_SRL_VERB, USE_SRL_PREP, USE_SRL_COMMA, + USE_QUANTIFIER) TextAnnotationFactory.createPipelineAnnotatorService(settings) } diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala index ef0d80d3..667b22e8 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala @@ -45,9 +45,10 @@ object PopulateSRLDataModel extends Logging { TextAnnotationFactory.createCuratorAnnotatorService(nonDefaultProps) case false => val nonDefaultProps = new Properties() - TextAnnotationFactory.disableSettings(nonDefaultProps, USE_NER_CONLL, USE_NER_ONTONOTES, USE_SRL_VERB, USE_SRL_NOM, USE_STANFORD_DEP) - if (parseViewName.equals(ViewNames.PARSE_GOLD)) - TextAnnotationFactory.disableSettings(nonDefaultProps, USE_POS, USE_STANFORD_PARSE) + TextAnnotationFactory.enableSettings(nonDefaultProps, USE_LEMMA, USE_SHALLOW_PARSE) + if (!parseViewName.equals(ViewNames.PARSE_GOLD)) { + TextAnnotationFactory.enableSettings(nonDefaultProps, USE_POS, USE_STANFORD_PARSE) + } TextAnnotationFactory.createPipelineAnnotatorService(nonDefaultProps) } diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala index f32e6de4..14c67286 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala @@ -9,11 +9,15 @@ package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling import java.io.File import edu.illinois.cs.cogcomp.core.datastructures.ViewNames -import edu.illinois.cs.cogcomp.saul.classifier.{ ClassifierUtils, JointTrainSparseNetwork } +import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{Sentence, TreeView} +import edu.illinois.cs.cogcomp.saul.classifier.{ClassifierUtils, JointTrainSparseNetwork} import edu.illinois.cs.cogcomp.saul.util.Logging import edu.illinois.cs.cogcomp.saulexamples.data.SRLFrameManager import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLClassifiers._ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLConstrainedClassifiers.argTypeConstraintClassifier +import edu.illinois.cs.cogcomp.pipeline.server.ServerClientAnnotator + +import scala.collection.JavaConverters._ object SRLscalaConfigurator { @@ -70,7 +74,7 @@ object SRLscalaConfigurator { lazy val SRL_FRAME_MANAGER: SRLFrameManager = new SRLFrameManager(PROPBANK_HOME) } -object SRLApps extends Logging { +object RunningApps extends App with Logging { import SRLscalaConfigurator._ @@ -104,12 +108,6 @@ object SRLApps extends Logging { logger.info("all predicates number after population:" + predicates().size) logger.info("all arguments number after population:" + arguments().size) logger.info("all tokens number after population:" + tokens().size) -} - -object RunningApps extends App with Logging { - import SRLApps._ - import SRLscalaConfigurator._ - import SRLMultiGraphDataModel._ // TRAINING if (!TEST_MODE) { diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SpatialRoleLabeling/SpRLDataModelReader.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SpatialRoleLabeling/SpRLDataModelReader.scala index ba17d733..a34f2019 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SpatialRoleLabeling/SpRLDataModelReader.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SpatialRoleLabeling/SpRLDataModelReader.scala @@ -26,7 +26,8 @@ object SpRLDataModelReader extends Logging { def read(path: String, version: String): List[SpRLSentence] = { val settings = new Properties() - TextAnnotationFactory.disableSettings(settings, USE_SRL_NOM, USE_NER_ONTONOTES) + TextAnnotationFactory.enableSettings(settings, USE_POS, USE_DEP, USE_LEMMA, USE_SHALLOW_PARSE, USE_NER_CONLL, + USE_STANFORD_DEP, USE_STANFORD_PARSE, USE_SRL_VERB, USE_SRL_PREP, USE_SRL_COMMA, USE_QUANTIFIER) val as = TextAnnotationFactory.createPipelineAnnotatorService(settings) val reader = new SpRLDataReader(path, classOf[SpRL2013Document]) reader.readData() diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/TextAnnotationFactory.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/TextAnnotationFactory.scala index 58d78e63..cea15bce 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/TextAnnotationFactory.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/TextAnnotationFactory.scala @@ -9,9 +9,10 @@ package edu.illinois.cs.cogcomp.saulexamples.nlp import java.util.Properties import edu.illinois.cs.cogcomp.annotation.AnnotatorService -import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{ TextAnnotation, TokenLabelView } -import edu.illinois.cs.cogcomp.core.utilities.configuration.{ Configurator, Property, ResourceManager } -import edu.illinois.cs.cogcomp.curator.{ CuratorConfigurator, CuratorFactory } +import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{TextAnnotation, TokenLabelView} +import edu.illinois.cs.cogcomp.core.utilities.configuration.{Configurator, Property, ResourceManager} +import edu.illinois.cs.cogcomp.curator.{CuratorConfigurator, CuratorFactory} +import edu.illinois.cs.cogcomp.pipeline.common.PipelineConfigurator import edu.illinois.cs.cogcomp.pipeline.main.PipelineFactory /** Created by taher on 7/30/16. @@ -37,7 +38,7 @@ object TextAnnotationFactory { def createPipelineAnnotatorService(settings: Properties): AnnotatorService = { PipelineFactory.buildPipeline( - new CuratorConfigurator().getConfig(new ResourceManager(settings)) + new PipelineConfigurator().getConfig(new ResourceManager(settings)) ) } diff --git a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ConstraintsTest.scala b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ConstraintsTest.scala index db244ae2..2391725f 100644 --- a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ConstraintsTest.scala +++ b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ConstraintsTest.scala @@ -7,18 +7,21 @@ package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling import edu.illinois.cs.cogcomp.core.datastructures.ViewNames -import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{ Constituent, Relation, TextAnnotation } +import edu.illinois.cs.cogcomp.core.datastructures.textannotation._ import edu.illinois.cs.cogcomp.core.datastructures.trees.Tree import edu.illinois.cs.cogcomp.core.utilities.DummyTextAnnotationGenerator -import edu.illinois.cs.cogcomp.lbjava.infer.{ FirstOrderConstant, FirstOrderConstraint } +import edu.illinois.cs.cogcomp.lbjava.infer.{FirstOrderConstant, FirstOrderConstraint} import edu.illinois.cs.cogcomp.lbjava.learn.SparseNetworkLearner -import edu.illinois.cs.cogcomp.saul.classifier.{ ConstrainedClassifier, Learnable } +import edu.illinois.cs.cogcomp.pipeline.server.ServerClientAnnotator +import edu.illinois.cs.cogcomp.saul.classifier.{ConstrainedClassifier, Learnable} import edu.illinois.cs.cogcomp.saul.constraint.ConstraintTypeConversion._ import edu.illinois.cs.cogcomp.saul.datamodel.DataModel import edu.illinois.cs.cogcomp.saulexamples.nlp.CommonSensors._ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLClassifiers.argumentTypeLearner import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLSensors._ -import org.scalatest.{ FlatSpec, Matchers } +import org.scalatest.{FlatSpec, Matchers} + +import scala.collection.JavaConverters._ import scala.collection.JavaConversions._ class ConstraintsTest extends FlatSpec with Matchers { From 0a11468d3df272cd340f22f962aa02eb716b9c11 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Sun, 30 Apr 2017 23:08:08 -0500 Subject: [PATCH 09/28] Some fixes. --- .../nlp/SemanticRoleLabeling/SRLAnnotator.scala | 14 ++++++++------ .../nlp/SemanticRoleLabeling/SRLApps.scala | 4 ++-- .../SRLConstrainedClassifiers.scala | 4 ++-- .../SRLMultiGraphDataModel.scala | 4 +++- .../saulexamples/nlp/TextAnnotationFactory.scala | 6 +++--- .../nlp/SemanticRoleLabeling/AnnotatorTest.scala | 4 ++-- .../nlp/SemanticRoleLabeling/ConstraintsTest.scala | 6 +++--- 7 files changed, 23 insertions(+), 19 deletions(-) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala index 8e2f5b76..b20cc4b4 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala @@ -6,7 +6,7 @@ */ package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling -import edu.illinois.cs.cogcomp.annotation.{Annotator, AnnotatorConfigurator, AnnotatorException} +import edu.illinois.cs.cogcomp.annotation.{ Annotator, AnnotatorConfigurator, AnnotatorException } import edu.illinois.cs.cogcomp.core.datastructures.ViewNames import edu.illinois.cs.cogcomp.core.datastructures.textannotation._ import edu.illinois.cs.cogcomp.core.utilities.configuration.ResourceManager @@ -79,7 +79,7 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: SRLClassifiers.argumentXuIdentifierGivenApredicate ) ClassifierUtils.LoadClassifier( - SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_aTr/", + SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_cTr/", SRLClassifiers.argumentTypeLearner ) } @@ -147,7 +147,7 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: SRLMultiGraphDataModel.relations.populate(finalRelationList, train = false, populateEdge = false) finalRelationList.flatMap { relation: Relation => - val label = SRLClassifiers.argumentTypeLearner(relation) + val label = SRLConstrainedClassifiers.argTypeConstraintClassifier(relation) if (label == "candidate") None else @@ -164,9 +164,11 @@ object SRLAnnotator { SRLscalaConfigurator.SRL_PARSE_VIEW ) - private def cloneRelationWithNewLabelAndArgument(sourceRelation: Relation, - label: String, - targetViewName: String): Relation = { + private def cloneRelationWithNewLabelAndArgument( + sourceRelation: Relation, + label: String, + targetViewName: String + ): Relation = { val newTargetConstituent = sourceRelation.getTarget.cloneForNewView(targetViewName) val newRelation = new Relation(label, sourceRelation.getSource, newTargetConstituent, sourceRelation.getScore) sourceRelation.getAttributeKeys.foreach({ key: String => diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala index 14c67286..a65c7558 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala @@ -9,8 +9,8 @@ package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling import java.io.File import edu.illinois.cs.cogcomp.core.datastructures.ViewNames -import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{Sentence, TreeView} -import edu.illinois.cs.cogcomp.saul.classifier.{ClassifierUtils, JointTrainSparseNetwork} +import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{ Sentence, TreeView } +import edu.illinois.cs.cogcomp.saul.classifier.{ ClassifierUtils, JointTrainSparseNetwork } import edu.illinois.cs.cogcomp.saul.util.Logging import edu.illinois.cs.cogcomp.saulexamples.data.SRLFrameManager import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLClassifiers._ diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala index 755d5c73..2c313d58 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala @@ -7,7 +7,7 @@ package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{ Relation, TextAnnotation } -import edu.illinois.cs.cogcomp.infer.ilp.OJalgoHook +import edu.illinois.cs.cogcomp.infer.ilp.{ GurobiHook, OJalgoHook } import edu.illinois.cs.cogcomp.saul.classifier.ConstrainedClassifier import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLClassifiers.{ argumentTypeLearner, argumentXuIdentifierGivenApredicate } import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLConstraints._ @@ -17,7 +17,7 @@ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLConstrai object SRLConstrainedClassifiers { import SRLMultiGraphDataModel._ - val erSolver = new OJalgoHook + val erSolver = new GurobiHook object argTypeConstraintClassifier extends ConstrainedClassifier[Relation, TextAnnotation](argumentTypeLearner) { def subjectTo = r_and_c_args diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala index 0bfcc053..cb7874d5 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala @@ -51,7 +51,9 @@ object SRLMultiGraphDataModel extends DataModel { sentencesToTokens.addSensor(CommonSensors.textAnnotationToTokens _) sentencesToRelations.addSensor(textAnnotationToRelation _) - sentencesToRelations.addSensor(textAnnotationToRelationMatch _) + + // TODO - Verify if we really need this matching sensor. This is very inefficient. + // sentencesToRelations.addSensor(textAnnotationToRelationMatch _) relationsToArguments.addSensor(relToArgument _) relationsToPredicates.addSensor(relToPredicate _) sentencesToStringTree.addSensor(textAnnotationToStringTree _) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/TextAnnotationFactory.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/TextAnnotationFactory.scala index cea15bce..d91f4512 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/TextAnnotationFactory.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/TextAnnotationFactory.scala @@ -9,9 +9,9 @@ package edu.illinois.cs.cogcomp.saulexamples.nlp import java.util.Properties import edu.illinois.cs.cogcomp.annotation.AnnotatorService -import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{TextAnnotation, TokenLabelView} -import edu.illinois.cs.cogcomp.core.utilities.configuration.{Configurator, Property, ResourceManager} -import edu.illinois.cs.cogcomp.curator.{CuratorConfigurator, CuratorFactory} +import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{ TextAnnotation, TokenLabelView } +import edu.illinois.cs.cogcomp.core.utilities.configuration.{ Configurator, Property, ResourceManager } +import edu.illinois.cs.cogcomp.curator.{ CuratorConfigurator, CuratorFactory } import edu.illinois.cs.cogcomp.pipeline.common.PipelineConfigurator import edu.illinois.cs.cogcomp.pipeline.main.PipelineFactory diff --git a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/AnnotatorTest.scala b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/AnnotatorTest.scala index 75aabeab..9a8ca4e1 100644 --- a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/AnnotatorTest.scala +++ b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/AnnotatorTest.scala @@ -7,10 +7,10 @@ package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling import edu.illinois.cs.cogcomp.core.datastructures.ViewNames -import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{PredicateArgumentView, TextAnnotation} +import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{ PredicateArgumentView, TextAnnotation } import edu.illinois.cs.cogcomp.core.utilities.DummyTextAnnotationGenerator import edu.illinois.cs.cogcomp.nlp.corpusreaders.AbstractSRLAnnotationReader -import org.scalatest.{FlatSpec, Matchers} +import org.scalatest.{ FlatSpec, Matchers } class AnnotatorTest extends FlatSpec with Matchers { val textAnnotation: TextAnnotation = DummyTextAnnotationGenerator.generateAnnotatedTextAnnotation( diff --git a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ConstraintsTest.scala b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ConstraintsTest.scala index 2391725f..85ae69ef 100644 --- a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ConstraintsTest.scala +++ b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ConstraintsTest.scala @@ -10,16 +10,16 @@ import edu.illinois.cs.cogcomp.core.datastructures.ViewNames import edu.illinois.cs.cogcomp.core.datastructures.textannotation._ import edu.illinois.cs.cogcomp.core.datastructures.trees.Tree import edu.illinois.cs.cogcomp.core.utilities.DummyTextAnnotationGenerator -import edu.illinois.cs.cogcomp.lbjava.infer.{FirstOrderConstant, FirstOrderConstraint} +import edu.illinois.cs.cogcomp.lbjava.infer.{ FirstOrderConstant, FirstOrderConstraint } import edu.illinois.cs.cogcomp.lbjava.learn.SparseNetworkLearner import edu.illinois.cs.cogcomp.pipeline.server.ServerClientAnnotator -import edu.illinois.cs.cogcomp.saul.classifier.{ConstrainedClassifier, Learnable} +import edu.illinois.cs.cogcomp.saul.classifier.{ ConstrainedClassifier, Learnable } import edu.illinois.cs.cogcomp.saul.constraint.ConstraintTypeConversion._ import edu.illinois.cs.cogcomp.saul.datamodel.DataModel import edu.illinois.cs.cogcomp.saulexamples.nlp.CommonSensors._ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLClassifiers.argumentTypeLearner import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLSensors._ -import org.scalatest.{FlatSpec, Matchers} +import org.scalatest.{ FlatSpec, Matchers } import scala.collection.JavaConverters._ import scala.collection.JavaConversions._ From a579ecbb9152b6f0288b0f1fe7503173d3ab0c1b Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Tue, 2 May 2017 00:56:48 -0500 Subject: [PATCH 10/28] Update SRL Model and minor changes to annotator. --- build.sbt | 2 +- .../nlp/SemanticRoleLabeling/SRLAnnotator.scala | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/build.sbt b/build.sbt index 3ab90a4d..3e3ef4f1 100644 --- a/build.sbt +++ b/build.sbt @@ -102,7 +102,7 @@ lazy val saulExamples = (project in file("saul-examples")). ccgGroupId % "illinois-pos" % cogcompNLPVersion, ccgGroupId % "saul-pos-tagger-models" % "1.4", ccgGroupId % "saul-er-models" % "1.8", - ccgGroupId % "saul-srl-models" % "1.3", + ccgGroupId % "saul-srl-models" % "1.4" classifier "verb-stanford", "org.json" % "json" % "20140107", "com.twitter" % "hbc-core" % "2.2.0", "org.rogach" %% "scallop" % "2.0.5" diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala index b20cc4b4..348ff9cb 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala @@ -31,7 +31,7 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: } override def addView(ta: TextAnnotation): Unit = { - checkPrequisites(ta) + checkPrerequisites(ta) SRLMultiGraphDataModel.clearInstances() @@ -57,8 +57,8 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: .headOption .orElse(ta.getView(ViewNames.TOKENS).getConstituentsCovering(predicate).headOption) - // TODO - Fix this with correct Sense identifier - predicate.addAttribute(AbstractSRLAnnotationReader.SenseIdentifier, "xx") + // TODO - Need to train a Predicate Sense identifier. + predicate.addAttribute(AbstractSRLAnnotationReader.SenseIdentifier, "01") predicate.addAttribute(AbstractSRLAnnotationReader.LemmaIdentifier, lemmaOrToken.map(_.getLabel).getOrElse("")) }) @@ -84,7 +84,7 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: ) } - def checkPrequisites(ta: TextAnnotation): Unit = { + def checkPrerequisites(ta: TextAnnotation): Unit = { val missingRequirements = requiredViewSet.diff(ta.getAvailableViews) if (missingRequirements.nonEmpty) { throw new AnnotatorException(s"Document ${ta.getId} is missing required views: $missingRequirements") @@ -103,15 +103,13 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: SRLMultiGraphDataModel.tokens.populate(CommonSensors.textAnnotationToTokens(ta), train = false, populateEdge = false) SRLMultiGraphDataModel.stringTree.populate(Seq(SRLSensors.textAnnotationToStringTree(ta)), train = false, populateEdge = false) - // TODO - Shim to select only Verb for now. + // Filter only verbs as candidates to the predicate classifier val predicateCandidates = ta.getView(ViewNames.POS) .getConstituents .filter(_.getLabel.startsWith("VB")) .map(_.cloneForNewView(getViewName)) SRLMultiGraphDataModel.predicates.populate(predicateCandidates, train = false, populateEdge = false) - // TODO - Figure out the constants in Boolean Property - // TODO - Constant for Predicate label predicateCandidates.filter(SRLClassifiers.predicateClassifier(_) == "true").map({ candidate: Constituent => candidate.cloneForNewViewWithDestinationLabel(getViewName, "Predicate") }) @@ -146,13 +144,13 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: SRLMultiGraphDataModel.relations.clear() SRLMultiGraphDataModel.relations.populate(finalRelationList, train = false, populateEdge = false) - finalRelationList.flatMap { relation: Relation => + finalRelationList.flatMap({ relation: Relation => val label = SRLConstrainedClassifiers.argTypeConstraintClassifier(relation) if (label == "candidate") None else Some(SRLAnnotator.cloneRelationWithNewLabelAndArgument(relation, label, getViewName)) - } + }) } } From 130f0b7273935f2c34c13bef754994109aa079f4 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Tue, 2 May 2017 13:14:53 -0500 Subject: [PATCH 11/28] Replace Gurobi -> OJAlgo so that unit test passes. --- .../nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala index 2c313d58..755d5c73 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala @@ -7,7 +7,7 @@ package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{ Relation, TextAnnotation } -import edu.illinois.cs.cogcomp.infer.ilp.{ GurobiHook, OJalgoHook } +import edu.illinois.cs.cogcomp.infer.ilp.OJalgoHook import edu.illinois.cs.cogcomp.saul.classifier.ConstrainedClassifier import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLClassifiers.{ argumentTypeLearner, argumentXuIdentifierGivenApredicate } import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLConstraints._ @@ -17,7 +17,7 @@ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLConstrai object SRLConstrainedClassifiers { import SRLMultiGraphDataModel._ - val erSolver = new GurobiHook + val erSolver = new OJalgoHook object argTypeConstraintClassifier extends ConstrainedClassifier[Relation, TextAnnotation](argumentTypeLearner) { def subjectTo = r_and_c_args From 7b038d46984fac81f8d547346ec008ee62cd87f2 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Tue, 2 May 2017 15:58:26 -0500 Subject: [PATCH 12/28] Some fixes for unit tests. --- build.sbt | 2 +- .../PopulateSRLDataModel.scala | 2 +- .../nlp/SemanticRoleLabeling/SRLSensors.scala | 21 +- .../SpRLDataModelReader.scala | 7 +- .../data/SRLFrameManagerTest.java | 7 +- .../resources/SRLToy/propbank/frames/give.xml | 292 ++++++++ .../resources/SRLToy/propbank/frames/go.xml | 656 ++++++++++++++++++ .../nlp/SemanticRoleLabeling/ModelsTest.scala | 6 +- .../cs/cogcomp/saulexamples/package.scala | 2 +- 9 files changed, 978 insertions(+), 17 deletions(-) create mode 100755 saul-examples/src/test/resources/SRLToy/propbank/frames/give.xml create mode 100755 saul-examples/src/test/resources/SRLToy/propbank/frames/go.xml diff --git a/build.sbt b/build.sbt index 3e3ef4f1..b800c2a6 100644 --- a/build.sbt +++ b/build.sbt @@ -102,7 +102,7 @@ lazy val saulExamples = (project in file("saul-examples")). ccgGroupId % "illinois-pos" % cogcompNLPVersion, ccgGroupId % "saul-pos-tagger-models" % "1.4", ccgGroupId % "saul-er-models" % "1.8", - ccgGroupId % "saul-srl-models" % "1.4" classifier "verb-stanford", + ccgGroupId % "saul-srl-models" % "1.4" classifier "verb-gold", "org.json" % "json" % "20140107", "com.twitter" % "hbc-core" % "2.2.0", "org.rogach" %% "scallop" % "2.0.5" diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala index 667b22e8..99442923 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala @@ -77,7 +77,7 @@ object PopulateSRLDataModel extends Logging { Some(ta) } catch { - case e: AnnotatorException => + case e: Exception => logger.warn(s"Annotation failed for sentence ${ta.getId}; removing it from the list.") None } diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLSensors.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLSensors.scala index 49101ca7..9716edc7 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLSensors.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLSensors.scala @@ -11,11 +11,12 @@ import edu.illinois.cs.cogcomp.core.datastructures.textannotation._ import edu.illinois.cs.cogcomp.core.datastructures.trees.Tree import edu.illinois.cs.cogcomp.edison.features._ import edu.illinois.cs.cogcomp.edison.features.factory.WordFeatureExtractorFactory +import edu.illinois.cs.cogcomp.saul.util.Logging import edu.illinois.cs.cogcomp.saulexamples.data.XuPalmerCandidateGenerator import scala.collection.JavaConversions._ -object SRLSensors { +object SRLSensors extends Logging { def sentenceToGoldPredicates(ta: TextAnnotation): List[Constituent] = { ta.getView(ViewNames.SRL_VERB).asInstanceOf[PredicateArgumentView].getPredicates.toList } @@ -29,13 +30,13 @@ object SRLSensors { def textAnnotationToTree(ta: TextAnnotation): Tree[Constituent] = { // We assume that there is only 1 sentence per TextAnnotation - val parseViewName: String = ViewNames.PARSE_GOLD + val parseViewName: String = SRLscalaConfigurator.SRL_PARSE_VIEW ta.getView(parseViewName).asInstanceOf[TreeView].getConstituentTree(0) } def textAnnotationToStringTree(ta: TextAnnotation): Tree[String] = { // We assume that there is only 1 sentence per TextAnnotation - val parseViewName: String = ViewNames.PARSE_GOLD + val parseViewName: String = SRLscalaConfigurator.SRL_PARSE_VIEW ta.getView(parseViewName).asInstanceOf[TreeView].getTree(0) } @@ -74,9 +75,15 @@ object SRLSensors { } def xuPalmerCandidate(x: Constituent, y: Tree[String]): List[Relation] = { - val p = XuPalmerCandidateGenerator.generateCandidates(x, y) - val z = p.map(y => new Relation("candidate", x.cloneForNewView(x.getViewName), y.cloneForNewView(y.getViewName), 0.0)) - z.toList + try { + val p = XuPalmerCandidateGenerator.generateCandidates(x, y) + val z = p.map(y => new Relation("candidate", x.cloneForNewView(x.getViewName), y.cloneForNewView(y.getViewName), 0.0)) + z.toList + } catch { + case _: Exception => + logger.warn("Exception while populating XuPalmer Candidates") + List() + } } def fexFeatureExtractor(x: Constituent, fex: FeatureExtractor): String = { @@ -88,4 +95,4 @@ object SRLSensors { contextFex.addFeatureExtractor(featureExtractor) fexFeatureExtractor(x, contextFex) } -} \ No newline at end of file +} diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SpatialRoleLabeling/SpRLDataModelReader.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SpatialRoleLabeling/SpRLDataModelReader.scala index a34f2019..c5fe6079 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SpatialRoleLabeling/SpRLDataModelReader.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SpatialRoleLabeling/SpRLDataModelReader.scala @@ -26,8 +26,11 @@ object SpRLDataModelReader extends Logging { def read(path: String, version: String): List[SpRLSentence] = { val settings = new Properties() - TextAnnotationFactory.enableSettings(settings, USE_POS, USE_DEP, USE_LEMMA, USE_SHALLOW_PARSE, USE_NER_CONLL, - USE_STANFORD_DEP, USE_STANFORD_PARSE, USE_SRL_VERB, USE_SRL_PREP, USE_SRL_COMMA, USE_QUANTIFIER) + // Note: We do not want to use SENTENCE_PIPELINE but the configuration property is incorrect + // in PipelineFactory. This will need to be updated when PipelineFactory is fixed. + TextAnnotationFactory.enableSettings(settings, USE_POS, USE_LEMMA, USE_NER_CONLL, USE_SHALLOW_PARSE, + USE_STANFORD_DEP, USE_STANFORD_PARSE, USE_SRL_VERB, USE_SENTENCE_PIPELINE) + val as = TextAnnotationFactory.createPipelineAnnotatorService(settings) val reader = new SpRLDataReader(path, classOf[SpRL2013Document]) reader.readData() diff --git a/saul-examples/src/test/java/edu/illinois/cs/cogcomp/saulexamples/data/SRLFrameManagerTest.java b/saul-examples/src/test/java/edu/illinois/cs/cogcomp/saulexamples/data/SRLFrameManagerTest.java index 081030f7..7b2ca7d6 100644 --- a/saul-examples/src/test/java/edu/illinois/cs/cogcomp/saulexamples/data/SRLFrameManagerTest.java +++ b/saul-examples/src/test/java/edu/illinois/cs/cogcomp/saulexamples/data/SRLFrameManagerTest.java @@ -7,9 +7,12 @@ package edu.illinois.cs.cogcomp.saulexamples.data; import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLscalaConfigurator; + import org.junit.Before; import org.junit.Test; +import org.scalatest.junit.JUnitSuite; + import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -17,7 +20,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -public class SRLFrameManagerTest { +public class SRLFrameManagerTest extends JUnitSuite { private SRLFrameManager frameManager; @@ -56,4 +59,4 @@ public void testGetLegalArguments() throws Exception { assertTrue(illegalArguments.contains("A4")); assertTrue(illegalArguments.contains("A5")); } -} \ No newline at end of file +} diff --git a/saul-examples/src/test/resources/SRLToy/propbank/frames/give.xml b/saul-examples/src/test/resources/SRLToy/propbank/frames/give.xml new file mode 100755 index 00000000..bb4d7d45 --- /dev/null +++ b/saul-examples/src/test/resources/SRLToy/propbank/frames/give.xml @@ -0,0 +1,292 @@ + + + + Frames file for 'give' based on survey of initial sentences of corpus + and comparison with 'take' + + + + + + + + + + + + + + + + The executives gave the chefs a standing ovation. + + The executives + gave + the chefs + a standing ovation + + + + + She had given the answers to two low-ability geography classes. + + She + had given + the answers + to two low ability geography classes + + + + + + The Beatles give way to baseball in the Nipponese version. + + The Beatles + give + way + to baseball + + + + + + This is very odd, *trace* given uncertainties about the auction. + + *trace* is not linked to anything + *trace* + given + uncertainties about the auction + + + + + + Given the weakness in both the junk bond market and the stock + market, traders fear that these transactions may be revised yet + again. + + given + the weakness in both the junk bond market and the + stock market + traders + + + + + + + + + + + + + + + + + + + They sometimes give away a few exact questions and answers + + They + sometimes + give away + a few exact questions and answers + + + + + Hearst might have virtually given the paper away + + Hearst + might have + virtually + given + the paper + + + + + + + +Mostly identical to "give" roleset above + + + + + + + + + + + + + + + Stocks gave back some of their gains. + + Stocks + gave back + some of their gains + + + + + + + + + + + + + + This radiator gives off a lot of heat. + + This radiator + gives off + a lot of heat + + + + + + + + + + + + + John's old truck finally gave out. + + John's old truc + finally + gave out + + + + + + + + + + + + + John gives out lots of candy on Halloween to the kids on his block. + + John + gives out + lots of candy + on Halloween + the kids on his block + + + + + + + + + + + + + + + John gave up after trying for a week. + + John + gave up + after trying for a week + + + + + John gave up on the puzzle. + + John + gave up + on the puzzle + + + + + John's old truck gave up the ghost. + + John's old truck + gave up + the ghost + + + + + + + + + + + + + + + Insurers buy this insurance protection for themselves by [*-3] + giving up a portion of the premiums they collect on a policy to + another firm -- a reinsurance company, which, in turn, accepts a + portion of any losses resulting from this policy. + + [*-3] -> Insurers + giving up + a portion of the premiums they collect on a policy + another firm -- a reinsurance company, which, in + turn, accepts a portion of any losses resulting from this policy + + + + + + + + + + + + + + + + + + + + Ms. Bartlett told her audience that she absolutely did not believe + in compromise or in [*] giving in to the client ``because I don't + think you can do watered-down versions of things.'' + + [*] -> she + giving in + the clients + + + + + + Finally, of course, the boss gives in, but he's still yelling: ``I + find myself explaining anything to Teddy Kennedy, you'll be + chasing stolen cars in Anchorage.'' + + Finally + of course + the boss + gives in + + + + + diff --git a/saul-examples/src/test/resources/SRLToy/propbank/frames/go.xml b/saul-examples/src/test/resources/SRLToy/propbank/frames/go.xml new file mode 100755 index 00000000..be08c5ba --- /dev/null +++ b/saul-examples/src/test/resources/SRLToy/propbank/frames/go.xml @@ -0,0 +1,656 @@ + + + + +Frames file for 'go' based on survey of initial sentences from big corpus +and comparison with 'rise' 'fall' 'become' and 'wander' + + + + + + + + + + + + + + + +Treebank uses every possible syntax for the stock-report usage of this +sense. You'll have to dig into constituents to pull these args +apart. The "direction" arg is more mandatory than the ArgM label +would suggest. + + + + + What flights go from Seattle to Boston via Minneapolis? + + what flights + go + Seattle + Boston + via Minneapolis + + + + + Imports have gone down 33% + + Imports + down + 33% + + + + + Woolworth went up 1 3/4 to 59 1/2. + + Woolworth + went + up + 1 3/4 + 59 1/2 + + + + + A lot of people would like TRACE to go back to 1970. + + TRACE -> a lot of people + go + back + 1970 + + + + + + At closely held Deltec Securities Corp., junk bond money managers + Amy K. Minella and Hannah H. Strasser say the problems of the + junk market go deeper than a temporary malaise. + + the problems of the junk market + go + deeper than a temporary malaise + + + + + + The protesters who greeted Mikhail Gorbachev at East Berlin's + airport earlier this month weren't shouting ``Go U.S.A'' -- they + were chanting ``Gorby, Help Us.'' + + Go + U.S.A + + + + + + + + + + + + + + + + + + He and two colleagues went on an overnight fishing trip. + + He and two colleagues + went + an overnight fishing trip + + + + + The lawyers went to work + + the lawyers + went + work + + + + + + Currently in the middle of a four-week, 20-city tour as a solo + pianist, Mr. Glass has left behind his synthesizers, equipment + and collaborators in favor of [*-1] going it alone. + + [*-1] -> Mr. Glass + going + it + alone + + + + + + ``I think Bush's going there is a helpful sign,'' said Sen. Terry + Sanford (D., N.C.) a member of the Foreign Relations Committee + who pushed to provide Costa Rica about the same amount of aid as + it received last year. + + Bush 's + going + there + + + + +Could conflate the two above senses + + + + + + + + + + + Portfolio managers go after the highest rates. + + Portfolio managers + go + the highest rates + + + + + + +Tag ONLY with rel and ArgMs + + + + I'm going to eat lunch now. + + + + + + + + + + + + + He went *trace* looking for a replacement for Mr. Landry. + + He + went + *trace* looking for a replacement for Mr. Landry + + + +This is dangerously close to the modal construction, except there's no +"to" (ie, "I'm going to leave now"). Includes the idiom "go begging". + + + + + + + + + + + + + + Boeing goes ahead with its plans for the 767. + + Boeing + goes + ahead + its plans for the 767 + + + + + Mrs. Yeagin went into education. + + Mrs. Yeagin + went + education + + + + + Boeing's plans for the 767 went without a hitch. + + Boeing's plans + went + without a hitch + + + + + They went about it with a systematic approach. + + they + it + with a systematic approach + + + + + + + + + + + + + + Chateau Yquem now goes for $100 a bottle + + Chateau Yquem + now + goes + $100 a bottle + + + + + + + + + + + + + + Georgia Gulf will go private. + + Georgia Gulf + go + private + + + + + The lights went off last night. + + the lights + went + off + last night + + + + + + Contel's Mr. Wohlstetter said the group of Big Board companies + isn't ready [*-1] to go public yet with its effort, and that he + doesn't plan to be the leader once it is public. + + [*-1] -> the group of Big Board companies + go + public + yet + its effort + + + + + + What's more, the losses they and the others caused ``are just + what we are stumbling over,'' says Mr. Stapf, adding that the + majority of misdeeds probably go [*-6] undetected. + + the majority of misdeeds + probably + go + [*-6] undetected + + + + + + + + + + + The order printers start *trace* to go on the trading floor. + + *trace*=the order printers + on the trading floor + + + + + + + + + + + + + + The rest went to investors from France and Hong Kong. + + the rest + went + investors from France and Hong Kong + + + + + $455 million will go for antitrust enforcement. + + $455 million + go + for antitrust enforcement + + + + + + Bethlehem had little choice but [*] to go with a European + steelmaker, because its competitors already have tapped the + Japanese and South Korean industry leaders, analysts noted. + + [*] -> Bethlehem + go + a European steelmaker + + + + + + + + + + + + The crime goes to character. + + The crime + goes + to character + + + + + + Which [*T*-1] goes [*-2] to show that the First Law applies in + insurance as in anything else: There is no free lunch, there is + only marketing. + + [*T*-1] -> Which + goes + [*-2] to show that the First Law applies in + insurance as in anything else: There is no free lunch, there + is only marketing + + + +This seems close to the benefactive, hence its placement here. I also +read the purpose clause as goes "to prove character". + + + + + + + + + + + + This market is still going through its pains + + the market + still + going + its pains + + + +Be wary of the distinction with motion through some medium, such as +"John goes through the park on his way to school." + + + + + + + + + + + + The opportunity to sell steel may be gone *trace* for now. + + *trace*=The opportunity to sell steel + may + gone + for now + + + + + The machine-gun-toting guards were gone. + + the machine gun toting guards + gone + + + +This is an incorrect parse from Treebank; "be gone" is clearly not +passive but rather copula+adjective. I hope this is an isolated example. + + + + + + + + + + + + With 15 seconds of trading *trace-C* *trace* to go, ... + + *trace*=*trace-C*=With 15 seconds of trading + go + + + +always infinitival? + + + + + + + + + + + + + + The one thing [0] Mr. Phillips clearly does have [*T*-1] going for + him is continuity, although it isn't certain if that will be + enough. + + [*T*-1] -> [0] -> The one thing + going + him + + + + + + + + + + + + + Instead, they focus on events in department stores and pour their + promotional budgets into gifts that [*T*-1] go along with + purchases. + + [*T*-1] -> that -> gifts + go + along with purchases + + + + + + + + + + + + + + The professor's droning voice goes on endlessly. + + the professor's droning voice + goes on + endlessly + + + + + A state court judge has allowed the charity to go on *trace* + soliciting funds. + + [the charity][*trace* soliciting funds] + go on + + + + + + + + + + + + + + + It's a time bomb just waiting *trace* to go off. + + *trace*=a time bomb + go off + + + + + + + + + + + + + + + + Airlines in 1989 came in like a bang and are going out like a whimper. + + Airlines in 1989 + going out + like a whimper + + + + + + + + + + + The light went out. + + The light + went out + + + +"Nirvana" does mean "going out, as a lamp". + + + + + + + + + + + + + + + The board and UAL's management can't go back to business as usual. + + The board and UAL's management + ca + n't + go back + business as usual + + + +Cf. get, return + + + + + + + + + + + + + + + We're in no way committed to a deal going through at all. + + a deal + going through + at all + + + +Note the distinction between this and other "go through" usages +(physical motion through a medium, undergoing some experience) + + + + + + + diff --git a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala index 0338b179..6a9699a1 100644 --- a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala +++ b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala @@ -77,9 +77,9 @@ class ModelsTest extends FlatSpec with Matchers { results.perLabel.foreach { result => result.label match { - case "A0" => result.f1 should be(0.95 +- 0.03) - case "A1" => result.f1 should be(0.95 +- 0.03) - case "A2" => result.f1 should be(0.85 +- 0.03) + case "A0" => result.f1 should be(0.95 +- 0.05) + case "A1" => result.f1 should be(0.95 +- 0.05) + case "A2" => result.f1 should be(0.85 +- 0.05) case _ => "" } } diff --git a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/package.scala b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/package.scala index 0c5207f0..611957b7 100644 --- a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/package.scala +++ b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/package.scala @@ -4,7 +4,7 @@ * Developed by: The Cognitive Computations Group, University of Illinois at Urbana-Champaign * http://cogcomp.cs.illinois.edu/ */ -package edu.illinois.cs.cogcomp.saulexamples; +package edu.illinois.cs.cogcomp.saulexamples import org.scalatest.Tag From ac3b3d6acbb0792ba012e24cc6d5f75e3c75189c Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Tue, 2 May 2017 18:43:23 -0500 Subject: [PATCH 13/28] Disable Pipeline caching for an SRL unit test. --- .../nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala | 9 ++++++--- .../nlp/SemanticRoleLabeling/ModelsTest.scala | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala index 99442923..c9540ec9 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala @@ -8,7 +8,7 @@ package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling import java.util.Properties -import edu.illinois.cs.cogcomp.annotation.AnnotatorException +import edu.illinois.cs.cogcomp.annotation.{ AnnotatorException, AnnotatorServiceConfigurator } import edu.illinois.cs.cogcomp.core.datastructures.ViewNames import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{ Constituent, TextAnnotation, TreeView } import edu.illinois.cs.cogcomp.core.datastructures.trees.Tree @@ -23,7 +23,6 @@ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLscalaCon import edu.illinois.cs.cogcomp.saulexamples.nlp.TextAnnotationFactory import scala.collection.JavaConversions._ - import SRLMultiGraphDataModel._ /** Created by Parisa on 1/17/16. @@ -32,7 +31,8 @@ object PopulateSRLDataModel extends Logging { def apply[T <: AnyRef]( testOnly: Boolean = false, useGoldPredicate: Boolean = false, - useGoldArgBoundaries: Boolean = false + useGoldArgBoundaries: Boolean = false, + usePipelineCaching: Boolean = true ): Unit = { val useCurator = SRLscalaConfigurator.USE_CURATOR @@ -49,6 +49,9 @@ object PopulateSRLDataModel extends Logging { if (!parseViewName.equals(ViewNames.PARSE_GOLD)) { TextAnnotationFactory.enableSettings(nonDefaultProps, USE_POS, USE_STANFORD_PARSE) } + if (!usePipelineCaching) { + TextAnnotationFactory.enableSettings(nonDefaultProps, AnnotatorServiceConfigurator.DISABLE_CACHE) + } TextAnnotationFactory.createPipelineAnnotatorService(nonDefaultProps) } diff --git a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala index 6a9699a1..31eaf8cc 100644 --- a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala +++ b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala @@ -19,7 +19,7 @@ class ModelsTest extends FlatSpec with Matchers { ) SRLMultiGraphDataModel.clearInstances() - PopulateSRLDataModel(testOnly = true, useGoldPredicate = true, useGoldArgBoundaries = true) + PopulateSRLDataModel(testOnly = true, useGoldPredicate = true, useGoldArgBoundaries = true, usePipelineCaching = false) "argument type classifier (aTr)" should "work." in { ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_aTr/", argumentTypeLearner) From 8523221e57c51c9c4ff4ddf1024357756c5d0e2a Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Wed, 3 May 2017 16:33:10 -0500 Subject: [PATCH 14/28] Add the matching sensor. --- .../SemanticRoleLabeling/SRLAnnotator.scala | 8 ++--- .../SRLMultiGraphDataModel.scala | 3 +- .../nlp/SemanticRoleLabeling/ModelsTest.scala | 29 +++++++++---------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala index 348ff9cb..4fdf4071 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala @@ -131,18 +131,18 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: SRLMultiGraphDataModel.predicates.populate(Seq(predicate), train = false, populateEdge = false) val candidateRelations = SRLSensors.xuPalmerCandidate(predicate, stringTree) - SRLMultiGraphDataModel.arguments.populate(candidateRelations.map(_.getTarget), train = false, populateEdge = false) - SRLMultiGraphDataModel.relations.populate(candidateRelations, train = false, populateEdge = false) + SRLMultiGraphDataModel.arguments.populate(candidateRelations.map(_.getTarget), train = false) + SRLMultiGraphDataModel.relations.populate(candidateRelations, train = false) val finalRelationList = candidateRelations.filter({ candidate: Relation => SRLClassifiers.argumentXuIdentifierGivenApredicate(candidate) == "true" }) SRLMultiGraphDataModel.arguments.clear() - SRLMultiGraphDataModel.arguments.populate(finalRelationList.map(_.getTarget), train = false, populateEdge = false) + SRLMultiGraphDataModel.arguments.populate(finalRelationList.map(_.getTarget), train = false) SRLMultiGraphDataModel.relations.clear() - SRLMultiGraphDataModel.relations.populate(finalRelationList, train = false, populateEdge = false) + SRLMultiGraphDataModel.relations.populate(finalRelationList, train = false) finalRelationList.flatMap({ relation: Relation => val label = SRLConstrainedClassifiers.argTypeConstraintClassifier(relation) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala index cb7874d5..cce63c34 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala @@ -52,8 +52,7 @@ object SRLMultiGraphDataModel extends DataModel { sentencesToTokens.addSensor(CommonSensors.textAnnotationToTokens _) sentencesToRelations.addSensor(textAnnotationToRelation _) - // TODO - Verify if we really need this matching sensor. This is very inefficient. - // sentencesToRelations.addSensor(textAnnotationToRelationMatch _) + sentencesToRelations.addSensor(textAnnotationToRelationMatch _) relationsToArguments.addSensor(relToArgument _) relationsToPredicates.addSensor(relToPredicate _) sentencesToStringTree.addSensor(textAnnotationToStringTree _) diff --git a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala index 31eaf8cc..d587df95 100644 --- a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala +++ b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala @@ -46,21 +46,20 @@ class ModelsTest extends FlatSpec with Matchers { } } - "L+I argument type classifier (aTr)" should "work." in { - //TODO solve the test problem with Gurobi licencing vs. OJalgoHook inefficiency - // ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_aTr/", argumentTypeLearner) - // val scores = argTypeConstraintClassifier.test(exclude = "candidate") - // scores.foreach { - // case (label, score) => { - // label match { - // case "A0" => (score._1 >= 0.9) should be(true) - // case "A1" => (score._1 >= 0.9) should be(true) - // case "A2" => (score._1 >= 0.6) should be(true) - // case _ => "" - // } - // } - // } - } + // "L+I argument type classifier (aTr)" should "work." in { + // //TODO solve the test problem with Gurobi licencing vs. OJalgoHook inefficiency + // ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_aTr/", argumentTypeLearner) + // val results = argTypeConstraintClassifier.test(exclude = "candidate") + // results.perLabel.foreach { + // result => + // result.label match { + // case "A0" => (result.f1 >= 0.9) should be(true) + // case "A1" => (result.f1 >= 0.9) should be(true) + // case "A2" => (result.f1 >= 0.6) should be(true) + // case _ => "" + // } + // } + // } "argument identifier (bTr)" should "perform higher than 0.95." in { ClassifierUtils.LoadClassifier(SRLscalaConfigurator.SRL_JAR_MODEL_PATH + "/models_bTr/", argumentXuIdentifierGivenApredicate) From fda3710ba293de7b0575b76a4ca6738c3016cd10 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Wed, 3 May 2017 19:35:53 -0500 Subject: [PATCH 15/28] Make SRLMultiGraphDataModel a class again. --- .../PopulateSRLDataModel.scala | 27 +++++----- .../PredArgViewGenerator.scala | 2 +- .../SemanticRoleLabeling/SRLAnnotator.scala | 51 ++++++++++--------- .../nlp/SemanticRoleLabeling/SRLApps.scala | 2 +- .../SemanticRoleLabeling/SRLClassifiers.scala | 6 +-- .../SRLConstrainedClassifiers.scala | 2 +- .../SemanticRoleLabeling/SRLConstraints.scala | 2 +- .../SRLMultiGraphDataModel.scala | 7 +-- .../SemanticRoleLabeling/DataModelTest.scala | 4 +- .../nlp/SemanticRoleLabeling/ModelsTest.scala | 4 +- 10 files changed, 55 insertions(+), 52 deletions(-) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala index c9540ec9..6ff93dbc 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala @@ -23,7 +23,6 @@ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLscalaCon import edu.illinois.cs.cogcomp.saulexamples.nlp.TextAnnotationFactory import scala.collection.JavaConversions._ -import SRLMultiGraphDataModel._ /** Created by Parisa on 1/17/16. */ @@ -95,30 +94,34 @@ object PopulateSRLDataModel extends Logging { } def populateDocument(a: TextAnnotation, isTrainingInstance: Boolean): Unit = { + // Data Model graph for a single sentence + val gr = new SRLMultiGraphDataModel(parseViewName) + if (!useGoldPredicate) { - sentences.populate(Seq(a), train = isTrainingInstance) + gr.sentences.populate(Seq(a), train = isTrainingInstance) - val predicateTrainCandidates = (sentences(a) ~> sentencesToTokens).collect({ - case x: Constituent if posTag(x).startsWith("VB") => x.cloneForNewView(ViewNames.SRL_VERB) + val predicateTrainCandidates = (gr.sentences(a) ~> gr.sentencesToTokens).collect({ + case x: Constituent if gr.posTag(x).startsWith("VB") => x.cloneForNewView(ViewNames.SRL_VERB) }) - predicates.populate(predicateTrainCandidates, train = isTrainingInstance) + gr.predicates.populate(predicateTrainCandidates, train = isTrainingInstance) } else { - sentences.populate(Seq(a), train = isTrainingInstance) + gr.sentences.populate(Seq(a), train = isTrainingInstance) } - logger.debug("gold relations for this train:" + (sentences(a) ~> sentencesToRelations).size) + logger.debug("gold relations for this train:" + (gr.sentences(a) ~> gr.sentencesToRelations).size) if (!useGoldArgBoundaries) { - val XuPalmerCandidateArgsTraining = (sentences(a) ~> sentencesToRelations ~> relationsToPredicates).flatMap({ - x => xuPalmerCandidate(x, (sentences(x.getTextAnnotation) ~> sentencesToStringTree).head) + val XuPalmerCandidateArgsTraining = (gr.sentences(a) ~> gr.sentencesToRelations ~> gr.relationsToPredicates).flatMap({ + x => xuPalmerCandidate(x, (gr.sentences(x.getTextAnnotation) ~> gr.sentencesToStringTree).head) }) - relations.populate(XuPalmerCandidateArgsTraining, train = isTrainingInstance) + gr.relations.populate(XuPalmerCandidateArgsTraining, train = isTrainingInstance) } - logger.debug("all relations for this test:" + (sentences(a) ~> sentencesToRelations).size) + logger.debug("all relations for this test:" + (gr.sentences(a) ~> gr.sentencesToRelations).size) - if (sentences().size % 1000 == 0) logger.info("loaded graphs in memory:" + sentences().size) + SRLClassifiers.SRLDataModel.addFromModel(gr) + if (gr.sentences().size % 1000 == 0) logger.info("loaded graphs in memory:" + gr.sentences().size) } if (!testOnly) { diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PredArgViewGenerator.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PredArgViewGenerator.scala index d94b66d2..77661f95 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PredArgViewGenerator.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PredArgViewGenerator.scala @@ -18,7 +18,7 @@ import scala.collection.JavaConversions._ */ object PredArgViewGenerator { - import SRLMultiGraphDataModel._ + import SRLClassifiers.SRLDataModel._ def toPredArgList(labelProp: TypedProperty[Relation, String]): Iterable[PredicateArgumentView] = { sentences().map { ta => diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala index 4fdf4071..ff7911ab 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala @@ -14,8 +14,9 @@ import edu.illinois.cs.cogcomp.edison.annotators.ClauseViewGenerator import edu.illinois.cs.cogcomp.nlp.corpusreaders.AbstractSRLAnnotationReader import edu.illinois.cs.cogcomp.saul.classifier.ClassifierUtils import edu.illinois.cs.cogcomp.saulexamples.nlp.CommonSensors +import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLClassifiers.SRLDataModel -import scala.collection.JavaConversions._ +import scala.collection.JavaConverters._ class SRLAnnotatorConfigurator extends AnnotatorConfigurator @@ -33,7 +34,7 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: override def addView(ta: TextAnnotation): Unit = { checkPrerequisites(ta) - SRLMultiGraphDataModel.clearInstances() + SRLDataModel.clearInstances() val finalView = new PredicateArgumentView(getViewName, SRLAnnotator.getClass.getCanonicalName, ta, 1.0) @@ -46,7 +47,7 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: val argumentList = getArguments(ta, predicate) finalView.addPredicateArguments( predicate, - argumentList.map(_.getTarget).toList, + argumentList.map(_.getTarget).toList.asJava, argumentList.map(_.getRelationName).toArray, argumentList.map(_.getScore).toArray ) @@ -54,18 +55,19 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: // Add additional attributes val lemmaOrToken = ta.getView(ViewNames.LEMMA) .getConstituentsCovering(predicate) + .asScala .headOption - .orElse(ta.getView(ViewNames.TOKENS).getConstituentsCovering(predicate).headOption) + .orElse(ta.getView(ViewNames.TOKENS).getConstituentsCovering(predicate).asScala.headOption) // TODO - Need to train a Predicate Sense identifier. predicate.addAttribute(AbstractSRLAnnotationReader.SenseIdentifier, "01") predicate.addAttribute(AbstractSRLAnnotationReader.LemmaIdentifier, lemmaOrToken.map(_.getLabel).getOrElse("")) }) - assert(finalView.getConstituents.forall(_.getViewName == getViewName), "Verify correct constituent view names.") + assert(finalView.getConstituents.asScala.forall(_.getViewName == getViewName), "Verify correct constituent view names.") ta.addView(getViewName, finalView) - SRLMultiGraphDataModel.clearInstances() + SRLDataModel.clearInstances() } override def initialize(rm: ResourceManager): Unit = { @@ -85,7 +87,7 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: } def checkPrerequisites(ta: TextAnnotation): Unit = { - val missingRequirements = requiredViewSet.diff(ta.getAvailableViews) + val missingRequirements = requiredViewSet.diff(ta.getAvailableViews.asScala) if (missingRequirements.nonEmpty) { throw new AnnotatorException(s"Document ${ta.getId} is missing required views: $missingRequirements") } @@ -97,18 +99,19 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: * @return Constituents that are not attached to any view yet. */ private def getPredicates(ta: TextAnnotation): Iterable[Constituent] = { - SRLMultiGraphDataModel.clearInstances() + SRLDataModel.clearInstances() - SRLMultiGraphDataModel.sentences.populate(Seq(ta), train = false, populateEdge = false) - SRLMultiGraphDataModel.tokens.populate(CommonSensors.textAnnotationToTokens(ta), train = false, populateEdge = false) - SRLMultiGraphDataModel.stringTree.populate(Seq(SRLSensors.textAnnotationToStringTree(ta)), train = false, populateEdge = false) + SRLDataModel.sentences.populate(Seq(ta), train = false, populateEdge = false) + SRLDataModel.tokens.populate(CommonSensors.textAnnotationToTokens(ta), train = false, populateEdge = false) + SRLDataModel.stringTree.populate(Seq(SRLSensors.textAnnotationToStringTree(ta)), train = false, populateEdge = false) // Filter only verbs as candidates to the predicate classifier val predicateCandidates = ta.getView(ViewNames.POS) .getConstituents + .asScala .filter(_.getLabel.startsWith("VB")) .map(_.cloneForNewView(getViewName)) - SRLMultiGraphDataModel.predicates.populate(predicateCandidates, train = false, populateEdge = false) + SRLDataModel.predicates.populate(predicateCandidates, train = false, populateEdge = false) predicateCandidates.filter(SRLClassifiers.predicateClassifier(_) == "true").map({ candidate: Constituent => candidate.cloneForNewViewWithDestinationLabel(getViewName, "Predicate") @@ -120,29 +123,29 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: * @return Relation between unattached predicate and arguments. */ private def getArguments(ta: TextAnnotation, predicate: Constituent): Iterable[Relation] = { - SRLMultiGraphDataModel.clearInstances() + SRLDataModel.clearInstances() val stringTree = SRLSensors.textAnnotationToStringTree(ta) // Prevent duplicate clearing of graphs. - SRLMultiGraphDataModel.sentences.populate(Seq(ta), train = false, populateEdge = false) - SRLMultiGraphDataModel.tokens.populate(CommonSensors.textAnnotationToTokens(ta), train = false, populateEdge = false) - SRLMultiGraphDataModel.stringTree.populate(Seq(stringTree), train = false, populateEdge = false) - SRLMultiGraphDataModel.predicates.populate(Seq(predicate), train = false, populateEdge = false) + SRLDataModel.sentences.populate(Seq(ta), train = false, populateEdge = false) + SRLDataModel.tokens.populate(CommonSensors.textAnnotationToTokens(ta), train = false, populateEdge = false) + SRLDataModel.stringTree.populate(Seq(stringTree), train = false, populateEdge = false) + SRLDataModel.predicates.populate(Seq(predicate), train = false, populateEdge = false) val candidateRelations = SRLSensors.xuPalmerCandidate(predicate, stringTree) - SRLMultiGraphDataModel.arguments.populate(candidateRelations.map(_.getTarget), train = false) - SRLMultiGraphDataModel.relations.populate(candidateRelations, train = false) + SRLDataModel.arguments.populate(candidateRelations.map(_.getTarget), train = false) + SRLDataModel.relations.populate(candidateRelations, train = false) val finalRelationList = candidateRelations.filter({ candidate: Relation => SRLClassifiers.argumentXuIdentifierGivenApredicate(candidate) == "true" }) - SRLMultiGraphDataModel.arguments.clear() - SRLMultiGraphDataModel.arguments.populate(finalRelationList.map(_.getTarget), train = false) + SRLDataModel.arguments.clear() + SRLDataModel.arguments.populate(finalRelationList.map(_.getTarget), train = false) - SRLMultiGraphDataModel.relations.clear() - SRLMultiGraphDataModel.relations.populate(finalRelationList, train = false) + SRLDataModel.relations.clear() + SRLDataModel.relations.populate(finalRelationList, train = false) finalRelationList.flatMap({ relation: Relation => val label = SRLConstrainedClassifiers.argTypeConstraintClassifier(relation) @@ -169,7 +172,7 @@ object SRLAnnotator { ): Relation = { val newTargetConstituent = sourceRelation.getTarget.cloneForNewView(targetViewName) val newRelation = new Relation(label, sourceRelation.getSource, newTargetConstituent, sourceRelation.getScore) - sourceRelation.getAttributeKeys.foreach({ key: String => + sourceRelation.getAttributeKeys.asScala.foreach({ key: String => newRelation.addAttribute(key, sourceRelation.getAttribute(key)) }) newRelation diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala index a65c7558..211a157f 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala @@ -101,7 +101,7 @@ object RunningApps extends App with Logging { // Here, the data is loaded into the graph PopulateSRLDataModel(testOnly = TEST_MODE, SRL_GOLD_PREDICATES, SRL_GOLD_ARG_BOUNDARIES) - import SRLMultiGraphDataModel._ + import SRLDataModel._ logger.info("all relations number after population:" + relations().size) logger.info("all sentences number after population:" + sentences().size) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLClassifiers.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLClassifiers.scala index d98480de..4b73d427 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLClassifiers.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLClassifiers.scala @@ -14,7 +14,9 @@ import edu.illinois.cs.cogcomp.saul.datamodel.property.Property /** Created by Parisa on 12/30/15. */ object SRLClassifiers { - import SRLMultiGraphDataModel._ + // Singleton data model instance for SRL classifiers. + val SRLDataModel: SRLMultiGraphDataModel = new SRLMultiGraphDataModel() + import SRLDataModel._ //TODO This needs to be overriden by the user; change it to be dynamic val parameters = new SparseAveragedPerceptron.Parameters() @@ -40,13 +42,11 @@ object SRLClassifiers { } object argumentXuIdentifierGivenApredicate extends Learnable[Relation](relations, parameters) { - def label = isArgumentXuGold override def feature = using(headwordRelation, syntacticFrameRelation, pathRelation, phraseTypeRelation, predPosTag, predLemmaR, linearPosition, argWordWindow, argPOSWindow, constituentLength, chunkLength, chunkEmbedding, chunkPathPattern, clauseFeatures, containsNEG, containsMOD) override lazy val classifier = new SparseNetworkLearner() } - } diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala index 755d5c73..bc2bb7dd 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala @@ -15,7 +15,7 @@ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLConstrai /** Created by Parisa on 12/27/15. */ object SRLConstrainedClassifiers { - import SRLMultiGraphDataModel._ + import SRLClassifiers.SRLDataModel._ val erSolver = new OJalgoHook diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstraints.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstraints.scala index 219bd63f..d6b6c4d2 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstraints.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstraints.scala @@ -16,7 +16,7 @@ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLClassifi import scala.collection.JavaConversions._ -import SRLMultiGraphDataModel._ +import SRLClassifiers.SRLDataModel._ /** Created by Parisa on 12/23/15. */ diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala index cce63c34..03f596b4 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala @@ -13,6 +13,7 @@ import edu.illinois.cs.cogcomp.edison.features.factory._ import edu.illinois.cs.cogcomp.nlp.corpusreaders.AbstractSRLAnnotationReader import edu.illinois.cs.cogcomp.saul.datamodel.DataModel import edu.illinois.cs.cogcomp.saul.datamodel.property.PairwiseConjunction +import edu.illinois.cs.cogcomp.saulexamples.data.SRLFrameManager import edu.illinois.cs.cogcomp.saulexamples.nlp.CommonSensors import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLSensors._ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLClassifiers._ @@ -20,12 +21,8 @@ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLConstrai import scala.collection.JavaConversions._ -object SRLMultiGraphDataModel extends DataModel { - val parseViewName = SRLscalaConfigurator.SRL_PARSE_VIEW - val frameManager = SRLscalaConfigurator.SRL_FRAME_MANAGER - +class SRLMultiGraphDataModel(val parseViewName: String = SRLscalaConfigurator.SRL_PARSE_VIEW, val frameManager: SRLFrameManager = SRLscalaConfigurator.SRL_FRAME_MANAGER) extends DataModel { // Nodes - val predicates = node[Constituent]((x: Constituent) => x.getTextAnnotation.getCorpusId + ":" + x.getTextAnnotation.getId + ":" + x.getSpan) val arguments = node[Constituent]((x: Constituent) => x.getTextAnnotation.getCorpusId + ":" + x.getTextAnnotation.getId + ":" + x.getSpan) diff --git a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/DataModelTest.scala b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/DataModelTest.scala index 2e0796e9..89999227 100644 --- a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/DataModelTest.scala +++ b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/DataModelTest.scala @@ -12,14 +12,14 @@ import edu.illinois.cs.cogcomp.core.utilities.DummyTextAnnotationGenerator import org.scalatest.{ FlatSpec, Matchers } class DataModelTest extends FlatSpec with Matchers { - import SRLMultiGraphDataModel._ + import SRLClassifiers.SRLDataModel._ val viewsToAdd = Array( ViewNames.LEMMA, ViewNames.POS, ViewNames.SHALLOW_PARSE, ViewNames.PARSE_GOLD, ViewNames.SRL_VERB ) - SRLMultiGraphDataModel.clearInstances() + clearInstances() val taTmp: TextAnnotation = DummyTextAnnotationGenerator.generateAnnotatedTextAnnotation(viewsToAdd, false, 1) sentences.populate(List(taTmp), train = false) diff --git a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala index d587df95..18314a5a 100644 --- a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala +++ b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/ModelsTest.scala @@ -18,7 +18,7 @@ class ModelsTest extends FlatSpec with Matchers { ViewNames.PARSE_GOLD, ViewNames.SRL_VERB ) - SRLMultiGraphDataModel.clearInstances() + SRLDataModel.clearInstances() PopulateSRLDataModel(testOnly = true, useGoldPredicate = true, useGoldArgBoundaries = true, usePipelineCaching = false) "argument type classifier (aTr)" should "work." in { @@ -58,7 +58,7 @@ class ModelsTest extends FlatSpec with Matchers { // case "A2" => (result.f1 >= 0.6) should be(true) // case _ => "" // } - // } + // } // } "argument identifier (bTr)" should "perform higher than 0.95." in { From a6f92bf85314d75de3c094b5407d27a574a8957c Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Thu, 4 May 2017 18:21:52 -0500 Subject: [PATCH 16/28] Mark SRL Annotator unit test as HighMemoryTest --- .../saulexamples/nlp/SemanticRoleLabeling/AnnotatorTest.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/AnnotatorTest.scala b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/AnnotatorTest.scala index 9a8ca4e1..86cdc3a1 100644 --- a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/AnnotatorTest.scala +++ b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/AnnotatorTest.scala @@ -10,6 +10,7 @@ import edu.illinois.cs.cogcomp.core.datastructures.ViewNames import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{ PredicateArgumentView, TextAnnotation } import edu.illinois.cs.cogcomp.core.utilities.DummyTextAnnotationGenerator import edu.illinois.cs.cogcomp.nlp.corpusreaders.AbstractSRLAnnotationReader +import edu.illinois.cs.cogcomp.saulexamples.HighMemoryTest import org.scalatest.{ FlatSpec, Matchers } class AnnotatorTest extends FlatSpec with Matchers { @@ -19,7 +20,7 @@ class AnnotatorTest extends FlatSpec with Matchers { 1 ) - "SRLAnnotator" should "work" in { + "SRLAnnotator" should "work" taggedAs (HighMemoryTest) in { val annotator = new SRLAnnotator(ViewNames.SRL_VERB) annotator.addView(textAnnotation) From 683e4f52147d7d87f8ffb503ebf1bde8cbf9c493 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Tue, 27 Jun 2017 16:45:04 -0500 Subject: [PATCH 17/28] Minor updates. --- .../PopulateSRLDataModel.scala | 42 +++++++++++++------ .../SRLMultiGraphDataModel.scala | 2 +- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala index 6ff93dbc..86f1f3ac 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala @@ -59,6 +59,12 @@ object PopulateSRLDataModel extends Logging { case ViewNames.PARSE_STANFORD => ClauseViewGenerator.STANFORD } + /** + * Add required views to the text annotations and filter out failed text annotations. + * + * @param taAll Input text annotations. + * @return Text annotations with required views populated.s + */ def addViewAndFilter(taAll: Iterable[TextAnnotation]): Iterable[TextAnnotation] = { taAll.flatMap({ ta => try { @@ -93,35 +99,45 @@ object PopulateSRLDataModel extends Logging { logger.debug(s"Number of $readerType data arguments: $numArguments") } + /** + * Adds a single Text Annotation to the DataModel graph. + * + * @param a Text Annotation instance to add to the graph.s + * @param isTrainingInstance Boolean indicating if the instance is a training instance.s + */ def populateDocument(a: TextAnnotation, isTrainingInstance: Boolean): Unit = { // Data Model graph for a single sentence - val gr = new SRLMultiGraphDataModel(parseViewName) + val singletonGraph = new SRLMultiGraphDataModel(parseViewName) + + // Populate the sentence node. + // Note: This does not populate the relation/predicates/arguments node s. + singletonGraph.sentences.populate(Seq(a), train = isTrainingInstance) if (!useGoldPredicate) { - gr.sentences.populate(Seq(a), train = isTrainingInstance) - val predicateTrainCandidates = (gr.sentences(a) ~> gr.sentencesToTokens).collect({ - case x: Constituent if gr.posTag(x).startsWith("VB") => x.cloneForNewView(ViewNames.SRL_VERB) + + val predicateTrainCandidates = (singletonGraph.sentences(a) ~> singletonGraph.sentencesToTokens).collect({ + case x: Constituent if singletonGraph.posTag(x).startsWith("VB") => x.cloneForNewView(ViewNames.SRL_VERB) }) - gr.predicates.populate(predicateTrainCandidates, train = isTrainingInstance) + singletonGraph.predicates.populate(predicateTrainCandidates, train = isTrainingInstance) } else { - gr.sentences.populate(Seq(a), train = isTrainingInstance) + singletonGraph.sentences.populate(Seq(a), train = isTrainingInstance) } - logger.debug("gold relations for this train:" + (gr.sentences(a) ~> gr.sentencesToRelations).size) + logger.debug("gold relations for this train:" + (singletonGraph.sentences(a) ~> singletonGraph.sentencesToRelations).size) if (!useGoldArgBoundaries) { - val XuPalmerCandidateArgsTraining = (gr.sentences(a) ~> gr.sentencesToRelations ~> gr.relationsToPredicates).flatMap({ - x => xuPalmerCandidate(x, (gr.sentences(x.getTextAnnotation) ~> gr.sentencesToStringTree).head) + val XuPalmerCandidateArgsTraining = (singletonGraph.sentences(a) ~> singletonGraph.sentencesToRelations ~> singletonGraph.relationsToPredicates).flatMap({ + x => xuPalmerCandidate(x, (singletonGraph.sentences(x.getTextAnnotation) ~> singletonGraph.sentencesToStringTree).head) }) - gr.relations.populate(XuPalmerCandidateArgsTraining, train = isTrainingInstance) + singletonGraph.relations.populate(XuPalmerCandidateArgsTraining, train = isTrainingInstance) } - logger.debug("all relations for this test:" + (gr.sentences(a) ~> gr.sentencesToRelations).size) + logger.debug("all relations for this test:" + (singletonGraph.sentences(a) ~> singletonGraph.sentencesToRelations).size) - SRLClassifiers.SRLDataModel.addFromModel(gr) - if (gr.sentences().size % 1000 == 0) logger.info("loaded graphs in memory:" + gr.sentences().size) + SRLClassifiers.SRLDataModel.addFromModel(singletonGraph) + if (singletonGraph.sentences().size % 1000 == 0) logger.info("loaded graphs in memory:" + singletonGraph.sentences().size) } if (!testOnly) { diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala index 03f596b4..0555c979 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala @@ -47,7 +47,7 @@ class SRLMultiGraphDataModel(val parseViewName: String = SRLscalaConfigurator.SR // Sensors sentencesToTokens.addSensor(CommonSensors.textAnnotationToTokens _) - sentencesToRelations.addSensor(textAnnotationToRelation _) + // sentencesToRelations.addSensor(textAnnotationToRelation _) sentencesToRelations.addSensor(textAnnotationToRelationMatch _) relationsToArguments.addSensor(relToArgument _) From 33b40f676691b6119f4503837c5854427d0b538d Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Tue, 27 Jun 2017 17:24:38 -0500 Subject: [PATCH 18/28] Fixes to population. --- .../PopulateSRLDataModel.scala | 67 +++++++++++-------- .../SRLMultiGraphDataModel.scala | 2 + .../nlp/SemanticRoleLabeling/SRLSensors.scala | 1 + 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala index 86f1f3ac..8b3531ac 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala @@ -10,7 +10,7 @@ import java.util.Properties import edu.illinois.cs.cogcomp.annotation.{ AnnotatorException, AnnotatorServiceConfigurator } import edu.illinois.cs.cogcomp.core.datastructures.ViewNames -import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{ Constituent, TextAnnotation, TreeView } +import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{ Constituent, PredicateArgumentView, TextAnnotation, TreeView } import edu.illinois.cs.cogcomp.core.datastructures.trees.Tree import edu.illinois.cs.cogcomp.curator.CuratorConfigurator._ import edu.illinois.cs.cogcomp.edison.annotators.ClauseViewGenerator @@ -23,6 +23,7 @@ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLscalaCon import edu.illinois.cs.cogcomp.saulexamples.nlp.TextAnnotationFactory import scala.collection.JavaConversions._ +import scala.collection.JavaConverters._ /** Created by Parisa on 1/17/16. */ @@ -36,6 +37,7 @@ object PopulateSRLDataModel extends Logging { val useCurator = SRLscalaConfigurator.USE_CURATOR val parseViewName = SRLscalaConfigurator.SRL_PARSE_VIEW + val srlGoldViewName = ViewNames.SRL_VERB val annotatorService = useCurator match { case true => @@ -59,8 +61,7 @@ object PopulateSRLDataModel extends Logging { case ViewNames.PARSE_STANFORD => ClauseViewGenerator.STANFORD } - /** - * Add required views to the text annotations and filter out failed text annotations. + /** Add required views to the text annotations and filter out failed text annotations. * * @param taAll Input text annotations. * @return Text annotations with required views populated.s @@ -99,57 +100,65 @@ object PopulateSRLDataModel extends Logging { logger.debug(s"Number of $readerType data arguments: $numArguments") } - /** - * Adds a single Text Annotation to the DataModel graph. + /** Adds a single Text Annotation to the DataModel graph. * * @param a Text Annotation instance to add to the graph.s * @param isTrainingInstance Boolean indicating if the instance is a training instance.s */ def populateDocument(a: TextAnnotation, isTrainingInstance: Boolean): Unit = { // Data Model graph for a single sentence - val singletonGraph = new SRLMultiGraphDataModel(parseViewName) + val singleInstanceGraph = new SRLMultiGraphDataModel(parseViewName) // Populate the sentence node. - // Note: This does not populate the relation/predicates/arguments node s. - singletonGraph.sentences.populate(Seq(a), train = isTrainingInstance) - - if (!useGoldPredicate) { - + // Note: This does not populate the relation/predicates/arguments nodes. + singleInstanceGraph.sentences.populate(Seq(a), train = isTrainingInstance) + + val predicateTrainCandidates = { + if (useGoldPredicate) { + a.getView(srlGoldViewName).asInstanceOf[PredicateArgumentView].getPredicates.asScala + } else { + (singleInstanceGraph.sentences(a) ~> singleInstanceGraph.sentencesToTokens).collect({ + case x: Constituent if singleInstanceGraph.posTag(x).startsWith("VB") => x.cloneForNewView(ViewNames.SRL_VERB) + }) + } + } - val predicateTrainCandidates = (singletonGraph.sentences(a) ~> singletonGraph.sentencesToTokens).collect({ - case x: Constituent if singletonGraph.posTag(x).startsWith("VB") => x.cloneForNewView(ViewNames.SRL_VERB) - }) + if (useGoldArgBoundaries) { + if (!useGoldPredicate) { + logger.error("Predicted Predicates with Gold Argument Boundaries is not supported.") + throw new UnsupportedOperationException("Predicted Predicates with Gold Argument Boundaries is not supported.") + } - singletonGraph.predicates.populate(predicateTrainCandidates, train = isTrainingInstance) + val goldRelations = a.getView(srlGoldViewName).asInstanceOf[PredicateArgumentView].getRelations.asScala + singleInstanceGraph.relations.populate(goldRelations, train = isTrainingInstance) } else { - singletonGraph.sentences.populate(Seq(a), train = isTrainingInstance) - } - logger.debug("gold relations for this train:" + (singletonGraph.sentences(a) ~> singletonGraph.sentencesToRelations).size) - - if (!useGoldArgBoundaries) { - val XuPalmerCandidateArgsTraining = (singletonGraph.sentences(a) ~> singletonGraph.sentencesToRelations ~> singletonGraph.relationsToPredicates).flatMap({ - x => xuPalmerCandidate(x, (singletonGraph.sentences(x.getTextAnnotation) ~> singletonGraph.sentencesToStringTree).head) + // Get XuPalmer Candidates for each predicate and populate the relations in the graph. + val XuPalmerCandidateArgsTraining = predicateTrainCandidates.flatMap({ + x => xuPalmerCandidate(x, (singleInstanceGraph.sentences(x.getTextAnnotation) ~> singleInstanceGraph.sentencesToStringTree).head) }) - singletonGraph.relations.populate(XuPalmerCandidateArgsTraining, train = isTrainingInstance) + singleInstanceGraph.relations.populate(XuPalmerCandidateArgsTraining, train = isTrainingInstance) } - logger.debug("all relations for this test:" + (singletonGraph.sentences(a) ~> singletonGraph.sentencesToRelations).size) + logger.debug("all relations for this test:" + (singleInstanceGraph.sentences(a) ~> singleInstanceGraph.sentencesToRelations).size) - SRLClassifiers.SRLDataModel.addFromModel(singletonGraph) - if (singletonGraph.sentences().size % 1000 == 0) logger.info("loaded graphs in memory:" + singletonGraph.sentences().size) + // Populate the classifier DataModel with this single instance graph. + // This is done due to performance reasons while populating a big data model graph directly. + SRLClassifiers.SRLDataModel.addFromModel(singleInstanceGraph) + if (singleInstanceGraph.sentences().size % 1000 == 0) logger.info("loaded graphs in memory:" + singleInstanceGraph.sentences().size) } if (!testOnly) { + logger.info(s"Reading training data from sections $TRAIN_SECTION_S to $TRAIN_SECTION_E") - val trainReader = new SRLDataReader(TREEBANK_HOME, PROPBANK_HOME, - TRAIN_SECTION_S, TRAIN_SECTION_E) + val trainReader = new SRLDataReader(TREEBANK_HOME, PROPBANK_HOME, TRAIN_SECTION_S, TRAIN_SECTION_E) trainReader.readData() + logger.info(s"Annotating ${trainReader.textAnnotations.size} training sentences") val filteredTa = addViewAndFilter(trainReader.textAnnotations) printNumbers(trainReader, "training") - logger.info("Populating SRLDataModel with training data.") + logger.info("Populating SRLDataModel with training data.") filteredTa.foreach(populateDocument(_, isTrainingInstance = true)) } diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala index 0555c979..aef3cedf 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala @@ -47,6 +47,8 @@ class SRLMultiGraphDataModel(val parseViewName: String = SRLscalaConfigurator.SR // Sensors sentencesToTokens.addSensor(CommonSensors.textAnnotationToTokens _) + + // This sensor is disabled. Relations are populated manually. // sentencesToRelations.addSensor(textAnnotationToRelation _) sentencesToRelations.addSensor(textAnnotationToRelationMatch _) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLSensors.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLSensors.scala index 9716edc7..4125dbbf 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLSensors.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLSensors.scala @@ -43,6 +43,7 @@ object SRLSensors extends Logging { def textAnnotationToRelation(ta: TextAnnotation): List[Relation] = { ta.getView(ViewNames.SRL_VERB).getRelations.toList } + def textAnnotationToRelationMatch(ta: TextAnnotation, r: Relation): Boolean = { (ta.getCorpusId + ":" + ta.getId).matches(r.getSource.getTextAnnotation.getCorpusId + ":" + r.getSource.getTextAnnotation.getId) } From 25bfbc70b6cba997094b3b07e4903a60409b78d6 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Tue, 27 Jun 2017 23:08:07 -0500 Subject: [PATCH 19/28] Some updates to the annotator. --- .../SemanticRoleLabeling/SRLAnnotator.scala | 27 ++++++++----------- .../SRLConstrainedClassifiers.scala | 2 +- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala index ff7911ab..195ea71d 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala @@ -13,11 +13,11 @@ import edu.illinois.cs.cogcomp.core.utilities.configuration.ResourceManager import edu.illinois.cs.cogcomp.edison.annotators.ClauseViewGenerator import edu.illinois.cs.cogcomp.nlp.corpusreaders.AbstractSRLAnnotationReader import edu.illinois.cs.cogcomp.saul.classifier.ClassifierUtils -import edu.illinois.cs.cogcomp.saulexamples.nlp.CommonSensors import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLClassifiers.SRLDataModel import scala.collection.JavaConverters._ +// XXX Add runtime configuration class SRLAnnotatorConfigurator extends AnnotatorConfigurator class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: ResourceManager = new SRLAnnotatorConfigurator().getDefaultConfig) @@ -101,17 +101,13 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: private def getPredicates(ta: TextAnnotation): Iterable[Constituent] = { SRLDataModel.clearInstances() - SRLDataModel.sentences.populate(Seq(ta), train = false, populateEdge = false) - SRLDataModel.tokens.populate(CommonSensors.textAnnotationToTokens(ta), train = false, populateEdge = false) - SRLDataModel.stringTree.populate(Seq(SRLSensors.textAnnotationToStringTree(ta)), train = false, populateEdge = false) - // Filter only verbs as candidates to the predicate classifier val predicateCandidates = ta.getView(ViewNames.POS) .getConstituents .asScala .filter(_.getLabel.startsWith("VB")) .map(_.cloneForNewView(getViewName)) - SRLDataModel.predicates.populate(predicateCandidates, train = false, populateEdge = false) + SRLDataModel.predicates.populate(predicateCandidates, train = false) predicateCandidates.filter(SRLClassifiers.predicateClassifier(_) == "true").map({ candidate: Constituent => candidate.cloneForNewViewWithDestinationLabel(getViewName, "Predicate") @@ -128,26 +124,25 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: val stringTree = SRLSensors.textAnnotationToStringTree(ta) // Prevent duplicate clearing of graphs. - SRLDataModel.sentences.populate(Seq(ta), train = false, populateEdge = false) - SRLDataModel.tokens.populate(CommonSensors.textAnnotationToTokens(ta), train = false, populateEdge = false) - SRLDataModel.stringTree.populate(Seq(stringTree), train = false, populateEdge = false) - SRLDataModel.predicates.populate(Seq(predicate), train = false, populateEdge = false) + SRLDataModel.sentences.populate(Seq(ta), train = false) val candidateRelations = SRLSensors.xuPalmerCandidate(predicate, stringTree) - SRLDataModel.arguments.populate(candidateRelations.map(_.getTarget), train = false) SRLDataModel.relations.populate(candidateRelations, train = false) val finalRelationList = candidateRelations.filter({ candidate: Relation => SRLClassifiers.argumentXuIdentifierGivenApredicate(candidate) == "true" }) - SRLDataModel.arguments.clear() - SRLDataModel.arguments.populate(finalRelationList.map(_.getTarget), train = false) + // Re-create graph if the size of candidates are different after filtering + if (finalRelationList.size != candidateRelations.size) { + SRLDataModel.clearInstances() - SRLDataModel.relations.clear() - SRLDataModel.relations.populate(finalRelationList, train = false) + // Prevent duplicate clearing of graphs. + SRLDataModel.sentences.populate(Seq(ta), train = false) + SRLDataModel.relations.populate(finalRelationList, train = false) + } - finalRelationList.flatMap({ relation: Relation => + candidateRelations.flatMap({ relation: Relation => val label = SRLConstrainedClassifiers.argTypeConstraintClassifier(relation) if (label == "candidate") None diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala index bc2bb7dd..0f604007 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLConstrainedClassifiers.scala @@ -7,7 +7,7 @@ package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling import edu.illinois.cs.cogcomp.core.datastructures.textannotation.{ Relation, TextAnnotation } -import edu.illinois.cs.cogcomp.infer.ilp.OJalgoHook +import edu.illinois.cs.cogcomp.infer.ilp.{ GurobiHook, OJalgoHook } import edu.illinois.cs.cogcomp.saul.classifier.ConstrainedClassifier import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLClassifiers.{ argumentTypeLearner, argumentXuIdentifierGivenApredicate } import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLConstraints._ From d805a9e77d45e12d5e5fb4d3fd3374b0933087f7 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Wed, 28 Jun 2017 00:51:29 -0500 Subject: [PATCH 20/28] Add an SRL Evaluation using the SRL Annotator. --- .../SemanticRoleLabeling/SRLEvaluation.scala | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala new file mode 100644 index 00000000..78baeaeb --- /dev/null +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala @@ -0,0 +1,102 @@ +/** This software is released under the University of Illinois/Research and Academic Use License. See + * the LICENSE file in the root folder for details. Copyright (c) 2016 + * + * Developed by: The Cognitive Computations Group, University of Illinois at Urbana-Champaign + * http://cogcomp.cs.illinois.edu/ + */ +package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling + +import java.util.Properties + +import edu.illinois.cs.cogcomp.annotation.AnnotatorServiceConfigurator +import edu.illinois.cs.cogcomp.core.datastructures.ViewNames +import edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView +import edu.illinois.cs.cogcomp.core.datastructures.trees.Tree +import edu.illinois.cs.cogcomp.core.experiments.ClassificationTester +import edu.illinois.cs.cogcomp.core.experiments.evaluators.PredicateArgumentEvaluator +import edu.illinois.cs.cogcomp.curator.CuratorConfigurator.RESPECT_TOKENIZATION +import edu.illinois.cs.cogcomp.nlp.utilities.ParseUtils +import edu.illinois.cs.cogcomp.pipeline.common.PipelineConfigurator.{ USE_LEMMA, USE_POS, USE_SHALLOW_PARSE, USE_STANFORD_PARSE } +import edu.illinois.cs.cogcomp.saul.util.Logging +import edu.illinois.cs.cogcomp.saulexamples.data.SRLDataReader +import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLscalaConfigurator.{ PROPBANK_HOME, TEST_SECTION, TREEBANK_HOME } +import edu.illinois.cs.cogcomp.saulexamples.nlp.TextAnnotationFactory + +import scala.collection.JavaConverters._ + +/** Evaluate the SRL Annotator using PredicateArgumentEvaluator. + * This evaluation honors settings in the SRLscalaConfigurator class. + */ +object SRLEvaluation extends App with Logging { + val parseViewName = SRLscalaConfigurator.SRL_PARSE_VIEW + val predictedViewName = ViewNames.SRL_VERB + "_PREDICTED" + val annotator = new SRLAnnotator(predictedViewName) + + val testReader = new SRLDataReader(TREEBANK_HOME, PROPBANK_HOME, TEST_SECTION, TEST_SECTION) + testReader.readData() + + val usePipelineCaching = true + val annotatorService = SRLscalaConfigurator.USE_CURATOR match { + case true => + val nonDefaultProps = new Properties() + TextAnnotationFactory.enableSettings(nonDefaultProps, RESPECT_TOKENIZATION) + TextAnnotationFactory.createCuratorAnnotatorService(nonDefaultProps) + case false => + val nonDefaultProps = new Properties() + TextAnnotationFactory.enableSettings(nonDefaultProps, USE_LEMMA, USE_SHALLOW_PARSE) + if (!parseViewName.equals(ViewNames.PARSE_GOLD)) { + TextAnnotationFactory.enableSettings(nonDefaultProps, USE_POS, USE_STANFORD_PARSE) + } + if (!usePipelineCaching) { + TextAnnotationFactory.enableSettings(nonDefaultProps, AnnotatorServiceConfigurator.DISABLE_CACHE) + } + TextAnnotationFactory.createPipelineAnnotatorService(nonDefaultProps) + } + + val annotatedDocumentsPartial = testReader.textAnnotations.asScala.map({ ta => + try { + annotatorService.addView(ta, ViewNames.LEMMA) + annotatorService.addView(ta, ViewNames.SHALLOW_PARSE) + if (!parseViewName.equals(ViewNames.PARSE_GOLD)) { + annotatorService.addView(ta, ViewNames.POS) + annotatorService.addView(ta, ViewNames.PARSE_STANFORD) + } + + // Clean up the trees + val tree: Tree[String] = ta.getView(parseViewName).asInstanceOf[TreeView].getTree(0) + val parseView = new TreeView(parseViewName, ta) + parseView.setParseTree(0, ParseUtils.stripFunctionTags(ParseUtils.snipNullNodes(tree))) + ta.addView(parseViewName, parseView) + + Some(ta) + } catch { + case _: Exception => + logger.warn(s"Annotation failed for sentence ${ta.getId}; removing it from the list.") + None + } + }).partition(_.isEmpty) + + logger.info(s"Annotation failures = ${annotatedDocumentsPartial._1.size}") + logger.info(s"Annotation success = ${annotatedDocumentsPartial._2.size}") + + val identifierTester = new ClassificationTester + val evaluator = new PredicateArgumentEvaluator + + // Annotate with SRL Annotator + var srlAnnotationFailures = 0 + annotatedDocumentsPartial._2 + .flatten + .foreach({ ta => + try { + annotator.addView(ta) + evaluator.evaluate(identifierTester, ta.getView(ViewNames.SRL_VERB), ta.getView(predictedViewName)) + } catch { + case _: Exception => + srlAnnotationFailures += 1 + logger.warn(s"SRL Annotation failed for sentence ${ta.getId}.") + } + }) + + logger.info(s"Documents which failed SRL Annotation = $srlAnnotationFailures") + println(identifierTester.getPerformanceTable(true).toTextTable) +} From cb3367ff58bdf7622ad81426e535e201e6e1f776 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Wed, 28 Jun 2017 01:06:00 -0500 Subject: [PATCH 21/28] Add some comments to the SRL Evaluator. --- .../nlp/SemanticRoleLabeling/SRLEvaluation.scala | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala index 78baeaeb..e1c89456 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala @@ -33,8 +33,11 @@ object SRLEvaluation extends App with Logging { val annotator = new SRLAnnotator(predictedViewName) val testReader = new SRLDataReader(TREEBANK_HOME, PROPBANK_HOME, TEST_SECTION, TEST_SECTION) + + logger.info("Reading the dataset.") testReader.readData() + logger.info(s"Intializing the annotator service: USE_CURATOR = ${SRLscalaConfigurator.USE_CURATOR}") val usePipelineCaching = true val annotatorService = SRLscalaConfigurator.USE_CURATOR match { case true => @@ -53,6 +56,7 @@ object SRLEvaluation extends App with Logging { TextAnnotationFactory.createPipelineAnnotatorService(nonDefaultProps) } + logger.info("Annotating documents with pre-requisite views") val annotatedDocumentsPartial = testReader.textAnnotations.asScala.map({ ta => try { annotatorService.addView(ta, ViewNames.LEMMA) @@ -78,6 +82,7 @@ object SRLEvaluation extends App with Logging { logger.info(s"Annotation failures = ${annotatedDocumentsPartial._1.size}") logger.info(s"Annotation success = ${annotatedDocumentsPartial._2.size}") + logger.info("Starting SRL Annotation and evaluation") val identifierTester = new ClassificationTester val evaluator = new PredicateArgumentEvaluator From a14818612e7cb8e761e735edb6742a0dc1e76d82 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Wed, 28 Jun 2017 16:20:10 -0500 Subject: [PATCH 22/28] Updates to annotator: Add configuration. --- .../SemanticRoleLabeling/SRLAnnotator.scala | 94 +++++++++++++------ 1 file changed, 65 insertions(+), 29 deletions(-) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala index 195ea71d..29897d5d 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala @@ -9,7 +9,7 @@ package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling import edu.illinois.cs.cogcomp.annotation.{ Annotator, AnnotatorConfigurator, AnnotatorException } import edu.illinois.cs.cogcomp.core.datastructures.ViewNames import edu.illinois.cs.cogcomp.core.datastructures.textannotation._ -import edu.illinois.cs.cogcomp.core.utilities.configuration.ResourceManager +import edu.illinois.cs.cogcomp.core.utilities.configuration.{ Configurator, Property, ResourceManager } import edu.illinois.cs.cogcomp.edison.annotators.ClauseViewGenerator import edu.illinois.cs.cogcomp.nlp.corpusreaders.AbstractSRLAnnotationReader import edu.illinois.cs.cogcomp.saul.classifier.ClassifierUtils @@ -17,15 +17,40 @@ import edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling.SRLClassifi import scala.collection.JavaConverters._ -// XXX Add runtime configuration -class SRLAnnotatorConfigurator extends AnnotatorConfigurator +class SRLAnnotatorConfigurator extends AnnotatorConfigurator { + override def getDefaultConfig: ResourceManager = { + val props = Array[Property]( + SRLAnnotatorConfigurator.USE_PREDICATE_CLASSIFIER, + SRLAnnotatorConfigurator.USE_ARGUMENT_IDENTIFIER, + SRLAnnotatorConfigurator.USE_VERB_SENSE_CLASSIFIER, + SRLAnnotatorConfigurator.PARSE_VIEW + ) + + val defaultRm = super.getDefaultConfig + Configurator.mergeProperties(defaultRm, new ResourceManager(generateProperties(props))) + } +} + +object SRLAnnotatorConfigurator { + // Use the predicate classifier if true else use all verbs as predicates + val USE_PREDICATE_CLASSIFIER = new Property("usePredicateClassifier", Configurator.TRUE) + + // Boolean denoting if we should perform Binary Argument Identification + val USE_ARGUMENT_IDENTIFIER = new Property("useArgumentIdentifier", Configurator.TRUE) + + // Verb Sense Classifier is not trained currently. + val USE_VERB_SENSE_CLASSIFIER = new Property("useVerbSenseClassifier", Configurator.FALSE) + + // Constituency Parse view to use for feature extraction + val PARSE_VIEW = new Property("parseView", ViewNames.PARSE_STANFORD) +} class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: ResourceManager = new SRLAnnotatorConfigurator().getDefaultConfig) - extends Annotator(finalViewName, SRLAnnotator.requiredViews, resourceManager) { + extends Annotator(finalViewName, SRLAnnotator.requiredViews :+ resourceManager.getString(SRLAnnotatorConfigurator.PARSE_VIEW), resourceManager) { val requiredViewSet: Set[String] = getRequiredViews.toSet lazy val clauseViewGenerator: ClauseViewGenerator = { - SRLscalaConfigurator.SRL_PARSE_VIEW match { + resourceManager.getString(SRLAnnotatorConfigurator.PARSE_VIEW) match { case ViewNames.PARSE_GOLD => new ClauseViewGenerator(ViewNames.PARSE_GOLD, "CLAUSES_GOLD") case ViewNames.PARSE_STANFORD => ClauseViewGenerator.STANFORD } @@ -59,8 +84,7 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: .headOption .orElse(ta.getView(ViewNames.TOKENS).getConstituentsCovering(predicate).asScala.headOption) - // TODO - Need to train a Predicate Sense identifier. - predicate.addAttribute(AbstractSRLAnnotationReader.SenseIdentifier, "01") + predicate.addAttribute(AbstractSRLAnnotationReader.SenseIdentifier, "XX") predicate.addAttribute(AbstractSRLAnnotationReader.LemmaIdentifier, lemmaOrToken.map(_.getLabel).getOrElse("")) }) @@ -99,19 +123,23 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: * @return Constituents that are not attached to any view yet. */ private def getPredicates(ta: TextAnnotation): Iterable[Constituent] = { - SRLDataModel.clearInstances() - // Filter only verbs as candidates to the predicate classifier val predicateCandidates = ta.getView(ViewNames.POS) .getConstituents .asScala .filter(_.getLabel.startsWith("VB")) .map(_.cloneForNewView(getViewName)) - SRLDataModel.predicates.populate(predicateCandidates, train = false) - predicateCandidates.filter(SRLClassifiers.predicateClassifier(_) == "true").map({ candidate: Constituent => - candidate.cloneForNewViewWithDestinationLabel(getViewName, "Predicate") - }) + if (resourceManager.getBoolean(SRLAnnotatorConfigurator.USE_PREDICATE_CLASSIFIER)) { + SRLDataModel.clearInstances() + SRLDataModel.predicates.populate(predicateCandidates, train = false) + + predicateCandidates.filter(SRLClassifiers.predicateClassifier(_) == "true").map({ candidate: Constituent => + candidate.cloneForNewViewWithDestinationLabel(getViewName, "Predicate") + }) + } else { + predicateCandidates + } } /** @param ta Input Text Annotation instance. @@ -121,28 +149,36 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: private def getArguments(ta: TextAnnotation, predicate: Constituent): Iterable[Relation] = { SRLDataModel.clearInstances() - val stringTree = SRLSensors.textAnnotationToStringTree(ta) - // Prevent duplicate clearing of graphs. SRLDataModel.sentences.populate(Seq(ta), train = false) + val stringTree = (SRLDataModel.sentences(ta) ~> SRLDataModel.sentencesToStringTree).head + val candidateRelations = SRLSensors.xuPalmerCandidate(predicate, stringTree) SRLDataModel.relations.populate(candidateRelations, train = false) - val finalRelationList = candidateRelations.filter({ candidate: Relation => - SRLClassifiers.argumentXuIdentifierGivenApredicate(candidate) == "true" - }) - - // Re-create graph if the size of candidates are different after filtering - if (finalRelationList.size != candidateRelations.size) { - SRLDataModel.clearInstances() - - // Prevent duplicate clearing of graphs. - SRLDataModel.sentences.populate(Seq(ta), train = false) - SRLDataModel.relations.populate(finalRelationList, train = false) + val finalRelationList = { + if (resourceManager.getBoolean(SRLAnnotatorConfigurator.USE_ARGUMENT_IDENTIFIER)) { + val filteredCandidates = candidateRelations.filter({ candidate: Relation => + SRLClassifiers.argumentXuIdentifierGivenApredicate(candidate) == "true" + }) + + // Re-create graph if the size of candidates are different after filtering + if (filteredCandidates.size != candidateRelations.size) { + SRLDataModel.clearInstances() + + // Prevent duplicate clearing of graphs. + SRLDataModel.sentences.populate(Seq(ta), train = false) + SRLDataModel.relations.populate(filteredCandidates, train = false) + } + + filteredCandidates + } else { + candidateRelations + } } - candidateRelations.flatMap({ relation: Relation => + finalRelationList.flatMap({ relation: Relation => val label = SRLConstrainedClassifiers.argTypeConstraintClassifier(relation) if (label == "candidate") None @@ -153,11 +189,11 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: } object SRLAnnotator { + // ParseView is added in the constructor private val requiredViews = Array( ViewNames.POS, ViewNames.LEMMA, - ViewNames.SHALLOW_PARSE, - SRLscalaConfigurator.SRL_PARSE_VIEW + ViewNames.SHALLOW_PARSE ) private def cloneRelationWithNewLabelAndArgument( From ac12007cbb67c5b5c1aa2b51a9921e00d3fc8cd1 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Wed, 28 Jun 2017 16:31:51 -0500 Subject: [PATCH 23/28] Fix some configuration: Remove PARSE_VIEW option.y --- .../nlp/SemanticRoleLabeling/SRLAnnotator.scala | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala index 29897d5d..0fbe7ace 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala @@ -22,8 +22,7 @@ class SRLAnnotatorConfigurator extends AnnotatorConfigurator { val props = Array[Property]( SRLAnnotatorConfigurator.USE_PREDICATE_CLASSIFIER, SRLAnnotatorConfigurator.USE_ARGUMENT_IDENTIFIER, - SRLAnnotatorConfigurator.USE_VERB_SENSE_CLASSIFIER, - SRLAnnotatorConfigurator.PARSE_VIEW + SRLAnnotatorConfigurator.USE_VERB_SENSE_CLASSIFIER ) val defaultRm = super.getDefaultConfig @@ -40,17 +39,14 @@ object SRLAnnotatorConfigurator { // Verb Sense Classifier is not trained currently. val USE_VERB_SENSE_CLASSIFIER = new Property("useVerbSenseClassifier", Configurator.FALSE) - - // Constituency Parse view to use for feature extraction - val PARSE_VIEW = new Property("parseView", ViewNames.PARSE_STANFORD) } class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: ResourceManager = new SRLAnnotatorConfigurator().getDefaultConfig) - extends Annotator(finalViewName, SRLAnnotator.requiredViews :+ resourceManager.getString(SRLAnnotatorConfigurator.PARSE_VIEW), resourceManager) { + extends Annotator(finalViewName, SRLAnnotator.requiredViews, resourceManager) { val requiredViewSet: Set[String] = getRequiredViews.toSet lazy val clauseViewGenerator: ClauseViewGenerator = { - resourceManager.getString(SRLAnnotatorConfigurator.PARSE_VIEW) match { + SRLscalaConfigurator.SRL_PARSE_VIEW match { case ViewNames.PARSE_GOLD => new ClauseViewGenerator(ViewNames.PARSE_GOLD, "CLAUSES_GOLD") case ViewNames.PARSE_STANFORD => ClauseViewGenerator.STANFORD } @@ -189,11 +185,11 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: } object SRLAnnotator { - // ParseView is added in the constructor private val requiredViews = Array( ViewNames.POS, ViewNames.LEMMA, - ViewNames.SHALLOW_PARSE + ViewNames.SHALLOW_PARSE, + SRLscalaConfigurator.SRL_PARSE_VIEW ) private def cloneRelationWithNewLabelAndArgument( From 1043287c459b68a0de2c84164c8834f13090d9b2 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Thu, 29 Jun 2017 00:50:42 -0500 Subject: [PATCH 24/28] Fix a SRL DataModel unit test. --- .../saulexamples/nlp/SemanticRoleLabeling/DataModelTest.scala | 3 +++ 1 file changed, 3 insertions(+) diff --git a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/DataModelTest.scala b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/DataModelTest.scala index 89999227..c4591164 100644 --- a/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/DataModelTest.scala +++ b/saul-examples/src/test/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/DataModelTest.scala @@ -23,6 +23,9 @@ class DataModelTest extends FlatSpec with Matchers { val taTmp: TextAnnotation = DummyTextAnnotationGenerator.generateAnnotatedTextAnnotation(viewsToAdd, false, 1) sentences.populate(List(taTmp), train = false) + val relationsList = SRLSensors.textAnnotationToRelation(taTmp) + relations.populate(relationsList, train = false) + "graph population" should "be correct" in { sentences().size should be(1) sentences().head.getText.trim should be("The construction of the John Smith library finished on time .") From 745dd60c82101affa7a10ba31a46b11aaacd0440 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Fri, 30 Jun 2017 21:10:05 -0500 Subject: [PATCH 25/28] Add a Greedy Decoder for SRL Arguments. --- .../SemanticRoleLabeling/GreedyDecoder.scala | 50 +++++++++++ .../SemanticRoleLabeling/SRLAnnotator.scala | 90 ++++++++++++++++--- .../SemanticRoleLabeling/SRLEvaluation.scala | 4 +- 3 files changed, 129 insertions(+), 15 deletions(-) create mode 100644 saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/GreedyDecoder.scala diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/GreedyDecoder.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/GreedyDecoder.scala new file mode 100644 index 00000000..cc3c1948 --- /dev/null +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/GreedyDecoder.scala @@ -0,0 +1,50 @@ +/** This software is released under the University of Illinois/Research and Academic Use License. See + * the LICENSE file in the root folder for details. Copyright (c) 2016 + * + * Developed by: The Cognitive Computations Group, University of Illinois at Urbana-Champaign + * http://cogcomp.cs.illinois.edu/ + */ +package edu.illinois.cs.cogcomp.saulexamples.nlp.SemanticRoleLabeling + +import edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation +import edu.illinois.cs.cogcomp.lbjava.classify.{ Score, ScoreSet } + +import scala.collection.mutable + +// Note: Targeted to in PredicateArgumentViews only. +// Used in SRL Annotation. +object GreedyDecoder { + def decodeNoOverlap(inputRelationWithScores: Seq[(Relation, ScoreSet)], labelsToExclude: Set[String] = Set.empty): Seq[(Relation, Score)] = { + val filterExcludes = inputRelationWithScores.map({ + case (relation, scoreset) => + val highScoreLabel = scoreset.highScoreValue() + (relation, scoreset.getScore(highScoreLabel)) + }).filterNot(x => labelsToExclude.contains(x._2.value)) + + if (filterExcludes.isEmpty) { + return Seq.empty + } + + val minSpan = filterExcludes.map(_._1.getTarget.getStartSpan).min + val maxSpan = filterExcludes.map(_._1.getTarget.getEndSpan).max + + val range = maxSpan - minSpan + 1 + val spanPosition = new mutable.BitSet(range) + + // Check if the sorting is stable + filterExcludes.sortBy(x => -x._2.score) // Sort from largest to lowest scores + .flatMap({ + case (relation, score) => + val startSpan = relation.getTarget.getStartSpan + val endSpan = relation.getTarget.getEndSpan - 1 + val hasOverlap = (startSpan to endSpan).exists(sp => spanPosition.contains(sp - minSpan)) + + if (!hasOverlap) { + spanPosition ++= Range(startSpan - minSpan, endSpan - minSpan + 1) + Some((relation, score)) + } else { + None + } + }) + } +} diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala index 0fbe7ace..b73d5c14 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala @@ -22,7 +22,10 @@ class SRLAnnotatorConfigurator extends AnnotatorConfigurator { val props = Array[Property]( SRLAnnotatorConfigurator.USE_PREDICATE_CLASSIFIER, SRLAnnotatorConfigurator.USE_ARGUMENT_IDENTIFIER, - SRLAnnotatorConfigurator.USE_VERB_SENSE_CLASSIFIER + SRLAnnotatorConfigurator.USE_VERB_SENSE_CLASSIFIER, + SRLAnnotatorConfigurator.USE_CONSTRAINTS, + SRLAnnotatorConfigurator.USE_GREEDY_INFERENCE_BINARY, + SRLAnnotatorConfigurator.USE_GREEDY_INFERENCE_TYPE ) val defaultRm = super.getDefaultConfig @@ -37,8 +40,17 @@ object SRLAnnotatorConfigurator { // Boolean denoting if we should perform Binary Argument Identification val USE_ARGUMENT_IDENTIFIER = new Property("useArgumentIdentifier", Configurator.TRUE) - // Verb Sense Classifier is not trained currently. + // Verb Sense Classifier is not trained currently val USE_VERB_SENSE_CLASSIFIER = new Property("useVerbSenseClassifier", Configurator.FALSE) + + // Constrained Inference + val USE_CONSTRAINTS = new Property("useConstraints", Configurator.TRUE) + + // Use Greedy Inference at the Binary Argument Identifier + val USE_GREEDY_INFERENCE_BINARY = new Property("useGreedyInferenceBinary", Configurator.FALSE) + + // Use Greedy Inference at the Argument Type Identifier + val USE_GREEDY_INFERENCE_TYPE = new Property("useGreedyInferenceType", Configurator.FALSE) } class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: ResourceManager = new SRLAnnotatorConfigurator().getDefaultConfig) @@ -113,6 +125,19 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: } clauseViewGenerator.addView(ta) + + // Check if the Annotator Configuration is compatible + val useConstraint = resourceManager.getBoolean(SRLAnnotatorConfigurator.USE_CONSTRAINTS) + val useGreedyInferenceBinary = resourceManager.getBoolean(SRLAnnotatorConfigurator.USE_GREEDY_INFERENCE_BINARY) + val useGreedyInferenceType = resourceManager.getBoolean(SRLAnnotatorConfigurator.USE_GREEDY_INFERENCE_TYPE) + + if (useConstraint && (useGreedyInferenceBinary || useGreedyInferenceType)) { + new UnsupportedOperationException("Incompatible configuration") + } else if (useGreedyInferenceBinary && (useConstraint || useGreedyInferenceType)) { + new UnsupportedOperationException("Incompatible configuration") + } else if (useGreedyInferenceType && (useConstraint || useGreedyInferenceBinary)) { + new UnsupportedOperationException("Incompatible configuration") + } } /** @param ta Input Text Annotation instance. @@ -155,9 +180,22 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: val finalRelationList = { if (resourceManager.getBoolean(SRLAnnotatorConfigurator.USE_ARGUMENT_IDENTIFIER)) { - val filteredCandidates = candidateRelations.filter({ candidate: Relation => - SRLClassifiers.argumentXuIdentifierGivenApredicate(candidate) == "true" - }) + val filteredCandidates = { + if (resourceManager.getBoolean(SRLAnnotatorConfigurator.USE_GREEDY_INFERENCE_BINARY)) { + val candidatesWithScores = candidateRelations.map({ + candidate => (candidate, SRLClassifiers.argumentXuIdentifierGivenApredicate.classifier.scores(candidate)) + }) + + // Greedy No Overlap decode + GreedyDecoder.decodeNoOverlap(candidatesWithScores, Set("false")) + .filter(x => x._2.value == "true") + .map(_._1) + } else { + candidateRelations.filter({ candidate: Relation => + SRLClassifiers.argumentXuIdentifierGivenApredicate(candidate) == "true" + }) + } + } // Re-create graph if the size of candidates are different after filtering if (filteredCandidates.size != candidateRelations.size) { @@ -174,13 +212,38 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: } } - finalRelationList.flatMap({ relation: Relation => - val label = SRLConstrainedClassifiers.argTypeConstraintClassifier(relation) - if (label == "candidate") - None - else - Some(SRLAnnotator.cloneRelationWithNewLabelAndArgument(relation, label, getViewName)) - }) + if (resourceManager.getBoolean(SRLAnnotatorConfigurator.USE_CONSTRAINTS)) { + finalRelationList.flatMap({ relation: Relation => + val label = SRLConstrainedClassifiers.argTypeConstraintClassifier(relation) + if (label == "candidate") + None + else + Some(SRLAnnotator.cloneRelationWithNewLabelAndArgument(relation, label, 1.0, getViewName)) + }) + } else { + val relationWithScores = finalRelationList.map({ relation: Relation => + (relation, SRLClassifiers.argumentTypeLearner.classifier.scores(relation)) + }) + + if (resourceManager.getBoolean(SRLAnnotatorConfigurator.USE_GREEDY_INFERENCE_TYPE)) { + GreedyDecoder.decodeNoOverlap(relationWithScores, Set("candidate")) + .filterNot(_._2.value == "candidate") + .map({ + case (relation, score) => + SRLAnnotator.cloneRelationWithNewLabelAndArgument(relation, score.value, score.score, getViewName) + }) + } else { + relationWithScores.map({ + case (relation, scoreset) => + val label = scoreset.highScoreValue() + (relation, scoreset.getScore(label)) + }).filterNot(_._2.value == "candidate") + .map({ + case (relation, score) => + SRLAnnotator.cloneRelationWithNewLabelAndArgument(relation, score.value, score.score, getViewName) + }) + } + } } } @@ -195,10 +258,11 @@ object SRLAnnotator { private def cloneRelationWithNewLabelAndArgument( sourceRelation: Relation, label: String, + score: Double, targetViewName: String ): Relation = { val newTargetConstituent = sourceRelation.getTarget.cloneForNewView(targetViewName) - val newRelation = new Relation(label, sourceRelation.getSource, newTargetConstituent, sourceRelation.getScore) + val newRelation = new Relation(label, sourceRelation.getSource, newTargetConstituent, score) sourceRelation.getAttributeKeys.asScala.foreach({ key: String => newRelation.addAttribute(key, sourceRelation.getAttribute(key)) }) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala index e1c89456..2f62ed48 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala @@ -96,9 +96,9 @@ object SRLEvaluation extends App with Logging { annotator.addView(ta) evaluator.evaluate(identifierTester, ta.getView(ViewNames.SRL_VERB), ta.getView(predictedViewName)) } catch { - case _: Exception => + case ex: Exception => srlAnnotationFailures += 1 - logger.warn(s"SRL Annotation failed for sentence ${ta.getId}.") + logger.error(s"SRL Annotation failed for sentence ${ta.getId}.", ex) } }) From 4e12063343c9e02e9bee7e9f1a61b53af1570e29 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Sun, 2 Jul 2017 15:58:18 -0500 Subject: [PATCH 26/28] Update SRLEvaluation to skip the "V" class. --- .../saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala | 2 ++ 1 file changed, 2 insertions(+) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala index 2f62ed48..5ec8d092 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala @@ -85,6 +85,8 @@ object SRLEvaluation extends App with Logging { logger.info("Starting SRL Annotation and evaluation") val identifierTester = new ClassificationTester + identifierTester.ignoreLabelFromSummary("V") + val evaluator = new PredicateArgumentEvaluator // Annotate with SRL Annotator From dee019e49df0968cde0edca0470ffcb04a966497 Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Sun, 2 Jul 2017 22:49:49 -0500 Subject: [PATCH 27/28] Add changes to support PARSE_CHARNIAK --- .../nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala | 5 ++++- .../nlp/SemanticRoleLabeling/SRLAnnotator.scala | 3 ++- .../nlp/SemanticRoleLabeling/SRLEvaluation.scala | 7 ++++--- .../nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala | 6 +++++- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala index 8b3531ac..035701ec 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala @@ -59,6 +59,7 @@ object PopulateSRLDataModel extends Logging { val clauseViewGenerator = parseViewName match { case ViewNames.PARSE_GOLD => new ClauseViewGenerator(parseViewName, "CLAUSES_GOLD") case ViewNames.PARSE_STANFORD => ClauseViewGenerator.STANFORD + case ViewNames.PARSE_CHARNIAK => new ClauseViewGenerator(parseViewName, ViewNames.CLAUSES_CHARNIAK) } /** Add required views to the text annotations and filter out failed text annotations. @@ -71,10 +72,12 @@ object PopulateSRLDataModel extends Logging { try { annotatorService.addView(ta, ViewNames.LEMMA) annotatorService.addView(ta, ViewNames.SHALLOW_PARSE) + if (!parseViewName.equals(ViewNames.PARSE_GOLD)) { annotatorService.addView(ta, ViewNames.POS) - annotatorService.addView(ta, ViewNames.PARSE_STANFORD) + annotatorService.addView(ta, parseViewName) } + // Add a clause view (needed for the clause relative position feature) clauseViewGenerator.addView(ta) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala index b73d5c14..8abeabb6 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLAnnotator.scala @@ -59,8 +59,9 @@ class SRLAnnotator(finalViewName: String = ViewNames.SRL_VERB, resourceManager: lazy val clauseViewGenerator: ClauseViewGenerator = { SRLscalaConfigurator.SRL_PARSE_VIEW match { - case ViewNames.PARSE_GOLD => new ClauseViewGenerator(ViewNames.PARSE_GOLD, "CLAUSES_GOLD") case ViewNames.PARSE_STANFORD => ClauseViewGenerator.STANFORD + case ViewNames.PARSE_GOLD => new ClauseViewGenerator(ViewNames.PARSE_GOLD, "CLAUSES_GOLD") + case ViewNames.PARSE_CHARNIAK => new ClauseViewGenerator(ViewNames.PARSE_CHARNIAK, ViewNames.CLAUSES_CHARNIAK) } } diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala index 5ec8d092..8222a7fe 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLEvaluation.scala @@ -61,9 +61,10 @@ object SRLEvaluation extends App with Logging { try { annotatorService.addView(ta, ViewNames.LEMMA) annotatorService.addView(ta, ViewNames.SHALLOW_PARSE) + if (!parseViewName.equals(ViewNames.PARSE_GOLD)) { annotatorService.addView(ta, ViewNames.POS) - annotatorService.addView(ta, ViewNames.PARSE_STANFORD) + annotatorService.addView(ta, parseViewName) } // Clean up the trees @@ -74,8 +75,8 @@ object SRLEvaluation extends App with Logging { Some(ta) } catch { - case _: Exception => - logger.warn(s"Annotation failed for sentence ${ta.getId}; removing it from the list.") + case ex: Exception => + logger.error(s"Annotation failed for sentence ${ta.getId}; removing it from the list.", ex) None } }).partition(_.isEmpty) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala index aef3cedf..1f1c2179 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLMultiGraphDataModel.scala @@ -181,7 +181,11 @@ class SRLMultiGraphDataModel(val parseViewName: String = SRLscalaConfigurator.SR /** Combines clause relative position and clause coverage */ val clauseFeatures = property(relations, "clauseFeats") { rel: Relation => - val clauseViewName = if (parseViewName.equals(ViewNames.PARSE_GOLD)) "CLAUSES_GOLD" else ViewNames.CLAUSES_STANFORD + val clauseViewName = parseViewName match { + case ViewNames.PARSE_GOLD => "CLAUSES_GOLD" + case ViewNames.PARSE_STANFORD => ViewNames.CLAUSES_STANFORD + case ViewNames.PARSE_CHARNIAK => ViewNames.CLAUSES_CHARNIAK + } fexFeatureExtractor(rel.getTarget, new ClauseFeatureExtractor(parseViewName, clauseViewName)) } From b85a5b4216d304914219d553713dd6802194502a Mon Sep 17 00:00:00 2001 From: Bhargav Mangipudi Date: Sun, 2 Jul 2017 23:45:58 -0500 Subject: [PATCH 28/28] Fix incorrect count of data model. --- .../nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala | 5 ++++- .../saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala index 035701ec..fe730d1b 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/PopulateSRLDataModel.scala @@ -148,7 +148,10 @@ object PopulateSRLDataModel extends Logging { // Populate the classifier DataModel with this single instance graph. // This is done due to performance reasons while populating a big data model graph directly. SRLClassifiers.SRLDataModel.addFromModel(singleInstanceGraph) - if (singleInstanceGraph.sentences().size % 1000 == 0) logger.info("loaded graphs in memory:" + singleInstanceGraph.sentences().size) + + if (SRLClassifiers.SRLDataModel.sentences().size % 1000 == 0) { + logger.info("loaded graphs in memory:" + SRLClassifiers.SRLDataModel.sentences().size) + } } if (!testOnly) { diff --git a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala index 211a157f..500e5561 100644 --- a/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala +++ b/saul-examples/src/main/scala/edu/illinois/cs/cogcomp/saulexamples/nlp/SemanticRoleLabeling/SRLApps.scala @@ -42,7 +42,7 @@ object SRLscalaConfigurator { val SRL_MODEL_DIR = "srl" val SRL_JAR_MODEL_PATH = "models" - // This is used to determine the parse view in SRL experiments (can be ViewNames.GOLD or ViewNames.STANFORD) + // This is used to determine the parse view in SRL experiments (can be ViewNames.GOLD or ViewNames.STANFORD or ViewNames.CHARNIAK) // For replicating the published experiments this needs to be GOLD val SRL_PARSE_VIEW = ViewNames.PARSE_GOLD @@ -95,6 +95,8 @@ object RunningApps extends App with Logging { else "" } + logger.info(s"Running experiment $expName") + val startTime = System.currentTimeMillis() logger.info("population starts.")