You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ctakes.apache.org by st...@apache.org on 2013/01/29 02:32:14 UTC
svn commit: r1439730 - in
/incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae:
./ baselines/
Author: stevenbethard
Date: Tue Jan 29 01:32:13 2013
New Revision: 1439730
URL: http://svn.apache.org/viewvc?rev=1439730&view=rev
Log:
Refactors RelationExtractorAnnotator and subclasses to reduce code duplication and make it easier to extend.
Modified:
incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/DegreeOfRelationExtractorAnnotator.java
incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/EntityMentionPairRelationExtractorAnnotator.java
incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/RelationExtractorAnnotator.java
incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline1DegreeOfRelationExtractorAnnotator.java
incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline1EntityMentionPairRelationExtractorAnnotator.java
incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline2DegreeOfRelationExtractorAnnotator.java
incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline2EntityMentionPairRelationExtractorAnnotator.java
incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline3DegreeOfRelationExtractorAnnotator.java
incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline3EntityMentionPairRelationExtractorAnnotator.java
Modified: incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/DegreeOfRelationExtractorAnnotator.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/DegreeOfRelationExtractorAnnotator.java?rev=1439730&r1=1439729&r2=1439730&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/DegreeOfRelationExtractorAnnotator.java (original)
+++ incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/DegreeOfRelationExtractorAnnotator.java Tue Jan 29 01:32:13 2013
@@ -19,21 +19,13 @@
package org.apache.ctakes.relationextractor.ae;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
-import java.util.Map;
-import org.apache.uima.jcas.JCas;
-import org.apache.uima.jcas.tcas.Annotation;
-import org.uimafit.util.JCasUtil;
-
-import org.apache.ctakes.typesystem.type.relation.BinaryTextRelation;
import org.apache.ctakes.typesystem.type.textsem.EntityMention;
-import org.apache.ctakes.typesystem.type.textsem.IdentifiedAnnotation;
import org.apache.ctakes.typesystem.type.textsem.Modifier;
import org.apache.ctakes.typesystem.type.textspan.Sentence;
-import org.cleartk.classifier.CleartkProcessingException;
-import org.cleartk.classifier.Feature;
+import org.apache.uima.jcas.JCas;
+import org.uimafit.util.JCasUtil;
/**
* Identifies Degree_Of relation between entities and modifiers
@@ -56,17 +48,4 @@ public class DegreeOfRelationExtractorAn
}
return pairs;
}
-
- @Override
- protected String getRelationCategory(
- Map<List<Annotation>, BinaryTextRelation> relationLookup,
- IdentifiedAnnotation arg1, IdentifiedAnnotation arg2) {
- BinaryTextRelation relation = relationLookup.get(Arrays.asList(arg1, arg2));
- return (relation != null) ? relation.getCategory() : NO_RELATION_CATEGORY;
- }
-
- @Override
- public String classify(List<Feature> features) throws CleartkProcessingException {
- return this.classifier.classify(features);
- }
}
Modified: incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/EntityMentionPairRelationExtractorAnnotator.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/EntityMentionPairRelationExtractorAnnotator.java?rev=1439730&r1=1439729&r2=1439730&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/EntityMentionPairRelationExtractorAnnotator.java (original)
+++ incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/EntityMentionPairRelationExtractorAnnotator.java Tue Jan 29 01:32:13 2013
@@ -23,17 +23,14 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
-import org.apache.uima.jcas.JCas;
-import org.apache.uima.jcas.tcas.Annotation;
-import org.uimafit.descriptor.ConfigurationParameter;
-import org.uimafit.util.JCasUtil;
-
import org.apache.ctakes.typesystem.type.relation.BinaryTextRelation;
import org.apache.ctakes.typesystem.type.textsem.EntityMention;
import org.apache.ctakes.typesystem.type.textsem.IdentifiedAnnotation;
import org.apache.ctakes.typesystem.type.textspan.Sentence;
-import org.cleartk.classifier.CleartkProcessingException;
-import org.cleartk.classifier.Feature;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.jcas.tcas.Annotation;
+import org.uimafit.descriptor.ConfigurationParameter;
+import org.uimafit.util.JCasUtil;
/**
* Identifies and classifies relations between pairs of named entities
@@ -115,10 +112,4 @@ public class EntityMentionPairRelationEx
}
return category;
}
-
- @Override
- public String classify(List<Feature> features) throws CleartkProcessingException {
- return this.classifier.classify(features);
- }
-
}
Modified: incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/RelationExtractorAnnotator.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/RelationExtractorAnnotator.java?rev=1439730&r1=1439729&r2=1439730&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/RelationExtractorAnnotator.java (original)
+++ incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/RelationExtractorAnnotator.java Tue Jan 29 01:32:13 2013
@@ -95,14 +95,9 @@ public abstract class RelationExtractorA
/**
* Selects the relevant mentions/annotations within a sentence for relation identification/extraction.
*/
- public abstract List<IdentifiedAnnotationPair> getCandidateRelationArgumentPairs(JCas identifiedAnnotationView, Sentence sentence);
+ protected abstract List<IdentifiedAnnotationPair> getCandidateRelationArgumentPairs(JCas identifiedAnnotationView, Sentence sentence);
/*
- * Make a prediction given a list of features.
- */
- public abstract String classify(List<Feature> features) throws CleartkProcessingException;
-
- /*
* Implement the standard UIMA process method.
*/
@Override
@@ -124,7 +119,13 @@ public abstract class RelationExtractorA
Map<List<Annotation>, BinaryTextRelation> relationLookup;
relationLookup = new HashMap<List<Annotation>, BinaryTextRelation>();
if (this.isTraining()) {
- relationLookup = createRelationLookup(relationView);
+ relationLookup = new HashMap<List<Annotation>, BinaryTextRelation>();
+ for (BinaryTextRelation relation : JCasUtil.select(relationView, BinaryTextRelation.class)) {
+ Annotation arg1 = relation.getArg1().getArgument();
+ Annotation arg2 = relation.getArg2().getArgument();
+ // The key is a list of args so we can do bi-directional lookup
+ relationLookup.put(Arrays.asList(arg1, arg2), relation);
+ }
}
// walk through each sentence in the text
@@ -203,34 +204,33 @@ public abstract class RelationExtractorA
* @return If this category should not be processed for training return <i>null</i>
* otherwise it returns the label sent to the datawriter
*/
- protected abstract String getRelationCategory(Map<List<Annotation>, BinaryTextRelation> relationLookup,
- IdentifiedAnnotation arg1, IdentifiedAnnotation arg2);
+ protected String getRelationCategory(Map<List<Annotation>, BinaryTextRelation> relationLookup,
+ IdentifiedAnnotation arg1, IdentifiedAnnotation arg2) {
+ BinaryTextRelation relation = relationLookup.get(Arrays.asList(arg1, arg2));
+ String category;
+ if (relation != null) {
+ category = relation.getCategory();
+ } else if (coin.nextDouble() <= this.probabilityOfKeepingANegativeExample) {
+ category = NO_RELATION_CATEGORY;
+ } else {
+ category = null;
+ }
+ return category;
+ }
/**
- * Creates a lookup map between lists of arguments and their relation
- * This map does not key in simply on a HashableArgument because
- * @param relationView
- * @return
+ * Predict an outcome given a set of features.
+ * By default, this simply delegates to the object's <code>classifier</code>.
+ * Subclasses may override this method to implement more complex classification procedures.
+ *
+ * @param features The features to be classified.
+ * @return The predicted outcome (label) for the features.
*/
- private static Map<List<Annotation>, BinaryTextRelation> createRelationLookup(
- JCas relationView) {
- Map<List<Annotation>, BinaryTextRelation> relationLookup;
- relationLookup = new HashMap<List<Annotation>, BinaryTextRelation>();
- for (BinaryTextRelation relation : JCasUtil.select(relationView, BinaryTextRelation.class)) {
- Annotation arg1, arg2;
- if (relation.getArg1().getRole().equals("Argument")) {
- arg1 = relation.getArg1().getArgument();
- arg2 = relation.getArg2().getArgument();
- } else {
- arg2 = relation.getArg1().getArgument();
- arg1 = relation.getArg2().getArgument();
- }
- // The key is a list of args so we can do bi-directional lookup
- relationLookup.put(Arrays.asList(arg1, arg2), relation);
- }
- return relationLookup;
+ protected String classify(List<Feature> features) throws CleartkProcessingException {
+ return this.classifier.classify(features);
}
+
public static class IdentifiedAnnotationPair {
private final IdentifiedAnnotation arg1;
Modified: incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline1DegreeOfRelationExtractorAnnotator.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline1DegreeOfRelationExtractorAnnotator.java?rev=1439730&r1=1439729&r2=1439730&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline1DegreeOfRelationExtractorAnnotator.java (original)
+++ incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline1DegreeOfRelationExtractorAnnotator.java Tue Jan 29 01:32:13 2013
@@ -19,39 +19,29 @@
package org.apache.ctakes.relationextractor.ae.baselines;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
-import java.util.Map;
-import org.apache.ctakes.relationextractor.ae.RelationExtractorAnnotator;
-import org.apache.ctakes.typesystem.type.relation.BinaryTextRelation;
-import org.apache.ctakes.typesystem.type.textsem.EntityMention;
-import org.apache.ctakes.typesystem.type.textsem.IdentifiedAnnotation;
-import org.apache.ctakes.typesystem.type.textsem.Modifier;
+import org.apache.ctakes.relationextractor.ae.DegreeOfRelationExtractorAnnotator;
import org.apache.ctakes.typesystem.type.textspan.Sentence;
import org.apache.uima.jcas.JCas;
-import org.apache.uima.jcas.tcas.Annotation;
import org.cleartk.classifier.CleartkProcessingException;
import org.cleartk.classifier.Feature;
-import org.uimafit.util.JCasUtil;
/**
* Annotated degree_of relation in sentences containing a single entity mention
* of a valid degree_of type and a single modifier.
*/
-public class Baseline1DegreeOfRelationExtractorAnnotator extends RelationExtractorAnnotator {
+public class Baseline1DegreeOfRelationExtractorAnnotator extends DegreeOfRelationExtractorAnnotator {
@Override
public List<IdentifiedAnnotationPair> getCandidateRelationArgumentPairs(
JCas identifiedAnnotationView, Sentence sentence) {
-
- List<EntityMention> entities = JCasUtil.selectCovered(identifiedAnnotationView, EntityMention.class, sentence);
- List<Modifier> modifiers = JCasUtil.selectCovered(identifiedAnnotationView, Modifier.class, sentence);
+ List<IdentifiedAnnotationPair> pairs = super.getCandidateRelationArgumentPairs(identifiedAnnotationView, sentence);
// look for sentences with one entity and one modifier
List<IdentifiedAnnotationPair> result = new ArrayList<IdentifiedAnnotationPair>();
- if((entities.size() == 1) && (modifiers.size() == 1)) {
- IdentifiedAnnotationPair pair = new IdentifiedAnnotationPair(entities.get(0), modifiers.get(0));
+ if (pairs.size() == 1) {
+ IdentifiedAnnotationPair pair = pairs.get(0);
if(Utils.validateDegreeOfArgumentTypes(pair)) {
System.out.println(sentence.getCoveredText());
System.out.println("arg1: " + pair.getArg1().getCoveredText());
@@ -64,14 +54,6 @@ public class Baseline1DegreeOfRelationEx
return result;
}
- @Override
- protected String getRelationCategory(
- Map<List<Annotation>, BinaryTextRelation> relationLookup,
- IdentifiedAnnotation arg1, IdentifiedAnnotation arg2) {
- BinaryTextRelation relation = relationLookup.get(Arrays.asList(arg1, arg2));
- return (relation != null) ? relation.getCategory() : NO_RELATION_CATEGORY;
- }
-
@Override
public String classify(List<Feature> features) throws CleartkProcessingException {
return "degree_of";
Modified: incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline1EntityMentionPairRelationExtractorAnnotator.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline1EntityMentionPairRelationExtractorAnnotator.java?rev=1439730&r1=1439729&r2=1439730&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline1EntityMentionPairRelationExtractorAnnotator.java (original)
+++ incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline1EntityMentionPairRelationExtractorAnnotator.java Tue Jan 29 01:32:13 2013
@@ -19,40 +19,23 @@
package org.apache.ctakes.relationextractor.ae.baselines;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
-import java.util.Map;
import org.apache.ctakes.relationextractor.ae.RelationExtractorAnnotator;
-import org.apache.ctakes.typesystem.type.relation.BinaryTextRelation;
import org.apache.ctakes.typesystem.type.textsem.EntityMention;
-import org.apache.ctakes.typesystem.type.textsem.IdentifiedAnnotation;
import org.apache.ctakes.typesystem.type.textspan.Sentence;
import org.apache.uima.jcas.JCas;
-import org.apache.uima.jcas.tcas.Annotation;
import org.cleartk.classifier.Feature;
-import org.uimafit.descriptor.ConfigurationParameter;
import org.uimafit.util.JCasUtil;
+import com.google.common.collect.Lists;
+
/**
* Annotate location_of relation between two entities in sentences containing
* exactly two entities (where the entities are of the correct types).
- * This implementation assumes classifyBothDirections is set to true (i.e.
- * each pair of entities is considered twice).
*/
public class Baseline1EntityMentionPairRelationExtractorAnnotator extends RelationExtractorAnnotator {
- public static final String PARAM_CLASSIFY_BOTH_DIRECTIONS = "ClassifyBothDirections";
-
- @ConfigurationParameter(
- name = PARAM_CLASSIFY_BOTH_DIRECTIONS,
- mandatory = false,
- description = "run the classifier in both directions, that is, classify each pair of events "
- + "{X,Y} once in the order X-to-Y and once in the order Y-to-X (default: classify each "
- + "pair of events {X, Y} once, giving the label 'R' if a relation exists with the order "
- + "X-to-Y, and 'R-1' if a relation exists with the order Y-to-X)")
- protected boolean classifyBothDirections = false;
-
@Override
public List<IdentifiedAnnotationPair> getCandidateRelationArgumentPairs(
JCas identifiedAnnotationView, Sentence sentence) {
@@ -63,37 +46,21 @@ public class Baseline1EntityMentionPairR
EntityMention.class,
sentence);
- // Create pairings (this will change depending on the classification direction)
- List<IdentifiedAnnotationPair> pairs = new ArrayList<IdentifiedAnnotationPair>();
-
- for (int i = 0; i < args.size(); ++i) {
- EntityMention arg1 = args.get(i);
- int jStart = this.classifyBothDirections ? 0 : i + 1;
- for (int j = jStart; j < args.size(); ++j) {
- EntityMention arg2 = args.get(j);
- // skip identical entity mentions and mentions with identical spans
- if (i == j || (arg1.getBegin() == arg2.getBegin() && arg1.getEnd() == arg2.getEnd())) {
- continue;
- }
- pairs.add(new IdentifiedAnnotationPair(arg1, arg2));
- }
- }
-
// look for sentence with two entities
- // because each pair of entities is cosidered twice, pairs.size() should be 2.
- if(pairs.size() == 2) {
+ if (args.size() == 2) {
+ EntityMention arg1 = args.get(0);
+ EntityMention arg2 = args.get(1);
// there are two entities in this sentence
// are they of suitable types for location_of?
- for(IdentifiedAnnotationPair pair : pairs) {
- if(Utils.validateLocationOfArgumentTypes(pair)) {
+ for (IdentifiedAnnotationPair pair : Lists.newArrayList(
+ new IdentifiedAnnotationPair(arg1, arg2),
+ new IdentifiedAnnotationPair(arg2, arg1))) {
+ if (Utils.validateLocationOfArgumentTypes(pair)) {
System.out.println(sentence.getCoveredText());
System.out.println("arg1: " + pair.getArg1().getCoveredText());
System.out.println("arg2: " + pair.getArg2().getCoveredText());
System.out.println();
-
- List<IdentifiedAnnotationPair> result = new ArrayList<IdentifiedAnnotationPair>();
- result.add(pair);
- return result;
+ return Lists.newArrayList(pair);
}
}
}
@@ -102,45 +69,8 @@ public class Baseline1EntityMentionPairR
// for all other cases, return no entity pairs
return new ArrayList<IdentifiedAnnotationPair>();
}
-
- @Override
- protected String getRelationCategory(Map<List<Annotation>, BinaryTextRelation> relationLookup,
- IdentifiedAnnotation arg1, IdentifiedAnnotation arg2) {
-
- BinaryTextRelation relation = relationLookup.get(Arrays.asList(arg1, arg2));
- String category;
- if (this.classifyBothDirections) {
- // if classifying both directions, we'll see {X, Y} once when X is first and
- // once when Y is first, so just do the single direction lookup here
- if (relation != null) {
- category = relation.getCategory();
- } else if (coin.nextDouble() <= this.probabilityOfKeepingANegativeExample) {
- category = NO_RELATION_CATEGORY;
- } else {
- category = null;
- }
- } else {
- // if classifying in a single direction, we'll see {X, Y} only once,
- // so do lookups in both directions, and change the category name for
- // the relations in the reverse order
- if (relation != null) {
- category = relation.getCategory();
- } else {
- relation = relationLookup.get(Arrays.asList(arg2, arg1));
- if (relation != null) {
- // Change category name to show reverse order
- category = relation.getCategory() + "-1";
- } else if (coin.nextDouble() <= this.probabilityOfKeepingANegativeExample) {
- category = NO_RELATION_CATEGORY;
- } else {
- category = null;
- }
- }
- }
- return category;
- }
- @Override
+ @Override
public String classify(List<Feature> features) {
return "location_of";
}
Modified: incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline2DegreeOfRelationExtractorAnnotator.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline2DegreeOfRelationExtractorAnnotator.java?rev=1439730&r1=1439729&r2=1439730&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline2DegreeOfRelationExtractorAnnotator.java (original)
+++ incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline2DegreeOfRelationExtractorAnnotator.java Tue Jan 29 01:32:13 2013
@@ -19,22 +19,17 @@
package org.apache.ctakes.relationextractor.ae.baselines;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
-import java.util.Map;
import org.apache.ctakes.relationextractor.ae.RelationExtractorAnnotator;
-import org.apache.ctakes.typesystem.type.relation.BinaryTextRelation;
import org.apache.ctakes.typesystem.type.textsem.EntityMention;
-import org.apache.ctakes.typesystem.type.textsem.IdentifiedAnnotation;
import org.apache.ctakes.typesystem.type.textsem.Modifier;
import org.apache.ctakes.typesystem.type.textspan.Sentence;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.CASException;
import org.apache.uima.jcas.JCas;
-import org.apache.uima.jcas.tcas.Annotation;
import org.cleartk.classifier.CleartkProcessingException;
import org.cleartk.classifier.Feature;
import org.uimafit.util.JCasUtil;
@@ -105,16 +100,6 @@ public class Baseline2DegreeOfRelationEx
return result;
}
-
-
-
- @Override
- protected String getRelationCategory(
- Map<List<Annotation>, BinaryTextRelation> relationLookup,
- IdentifiedAnnotation arg1, IdentifiedAnnotation arg2) {
- BinaryTextRelation relation = relationLookup.get(Arrays.asList(arg1, arg2));
- return (relation != null) ? relation.getCategory() : NO_RELATION_CATEGORY;
- }
@Override
public String classify(List<Feature> features) throws CleartkProcessingException {
Modified: incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline2EntityMentionPairRelationExtractorAnnotator.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline2EntityMentionPairRelationExtractorAnnotator.java?rev=1439730&r1=1439729&r2=1439730&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline2EntityMentionPairRelationExtractorAnnotator.java (original)
+++ incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline2EntityMentionPairRelationExtractorAnnotator.java Tue Jan 29 01:32:13 2013
@@ -24,19 +24,14 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
-import java.util.Map;
import org.apache.ctakes.relationextractor.ae.RelationExtractorAnnotator;
-import org.apache.ctakes.typesystem.type.relation.BinaryTextRelation;
import org.apache.ctakes.typesystem.type.textsem.EntityMention;
-import org.apache.ctakes.typesystem.type.textsem.IdentifiedAnnotation;
import org.apache.ctakes.typesystem.type.textspan.Sentence;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.CASException;
import org.apache.uima.jcas.JCas;
-import org.apache.uima.jcas.tcas.Annotation;
import org.cleartk.classifier.Feature;
-import org.uimafit.descriptor.ConfigurationParameter;
import org.uimafit.util.JCasUtil;
import com.google.common.base.Function;
@@ -46,21 +41,9 @@ import com.google.common.collect.Orderin
/**
* Annotate location_of relation between two entities in sentences with multiple anatomical sites
* and a single legitimate location_of arg2. Use the pair of arguments that are the closest to each other.
- * This implementation assumes classifyBothDirections = true.
*/
public class Baseline2EntityMentionPairRelationExtractorAnnotator extends RelationExtractorAnnotator {
- public static final String PARAM_CLASSIFY_BOTH_DIRECTIONS = "ClassifyBothDirections";
-
- @ConfigurationParameter(
- name = PARAM_CLASSIFY_BOTH_DIRECTIONS,
- mandatory = false,
- description = "run the classifier in both directions, that is, classify each pair of events "
- + "{X,Y} once in the order X-to-Y and once in the order Y-to-X (default: classify each "
- + "pair of events {X, Y} once, giving the label 'R' if a relation exists with the order "
- + "X-to-Y, and 'R-1' if a relation exists with the order Y-to-X)")
- protected boolean classifyBothDirections = false;
-
@Override
public List<IdentifiedAnnotationPair> getCandidateRelationArgumentPairs(
JCas identifiedAnnotationView, Sentence sentence) {
@@ -71,16 +54,11 @@ public class Baseline2EntityMentionPairR
EntityMention.class,
sentence);
- // Create pairings (this will change depending on the classification direction)
+ // Create pairings
List<IdentifiedAnnotationPair> pairs = new ArrayList<IdentifiedAnnotationPair>();
-
- for (int i = 0; i < args.size(); ++i) {
- EntityMention arg1 = args.get(i);
- int jStart = this.classifyBothDirections ? 0 : i + 1;
- for (int j = jStart; j < args.size(); ++j) {
- EntityMention arg2 = args.get(j);
- // skip identical entity mentions and mentions with identical spans
- if (i == j || (arg1.getBegin() == arg2.getBegin() && arg1.getEnd() == arg2.getEnd())) {
+ for (EntityMention arg1 : args) {
+ for (EntityMention arg2 : args) {
+ if (arg1.getBegin() == arg2.getBegin() && arg1.getEnd() == arg2.getEnd()) {
continue;
}
pairs.add(new IdentifiedAnnotationPair(arg1, arg2));
@@ -136,43 +114,6 @@ public class Baseline2EntityMentionPairR
return result;
}
-
- @Override
- protected String getRelationCategory(Map<List<Annotation>, BinaryTextRelation> relationLookup,
- IdentifiedAnnotation arg1, IdentifiedAnnotation arg2) {
-
- BinaryTextRelation relation = relationLookup.get(Arrays.asList(arg1, arg2));
- String category;
- if (this.classifyBothDirections) {
- // if classifying both directions, we'll see {X, Y} once when X is first and
- // once when Y is first, so just do the single direction lookup here
- if (relation != null) {
- category = relation.getCategory();
- } else if (coin.nextDouble() <= this.probabilityOfKeepingANegativeExample) {
- category = NO_RELATION_CATEGORY;
- } else {
- category = null;
- }
- } else {
- // if classifying in a single direction, we'll see {X, Y} only once,
- // so do lookups in both directions, and change the category name for
- // the relations in the reverse order
- if (relation != null) {
- category = relation.getCategory();
- } else {
- relation = relationLookup.get(Arrays.asList(arg2, arg1));
- if (relation != null) {
- // Change category name to show reverse order
- category = relation.getCategory() + "-1";
- } else if (coin.nextDouble() <= this.probabilityOfKeepingANegativeExample) {
- category = NO_RELATION_CATEGORY;
- } else {
- category = null;
- }
- }
- }
- return category;
- }
@Override
public String classify(List<Feature> features) {
Modified: incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline3DegreeOfRelationExtractorAnnotator.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline3DegreeOfRelationExtractorAnnotator.java?rev=1439730&r1=1439729&r2=1439730&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline3DegreeOfRelationExtractorAnnotator.java (original)
+++ incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline3DegreeOfRelationExtractorAnnotator.java Tue Jan 29 01:32:13 2013
@@ -19,42 +19,24 @@
package org.apache.ctakes.relationextractor.ae.baselines;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
-import java.util.Map;
-import org.apache.ctakes.relationextractor.ae.RelationExtractorAnnotator;
-import org.apache.ctakes.typesystem.type.relation.BinaryTextRelation;
+import org.apache.ctakes.relationextractor.ae.DegreeOfRelationExtractorAnnotator;
import org.apache.ctakes.typesystem.type.syntax.TreebankNode;
-import org.apache.ctakes.typesystem.type.textsem.EntityMention;
import org.apache.ctakes.typesystem.type.textsem.IdentifiedAnnotation;
-import org.apache.ctakes.typesystem.type.textsem.Modifier;
import org.apache.ctakes.typesystem.type.textspan.Sentence;
import org.apache.uima.jcas.JCas;
-import org.apache.uima.jcas.tcas.Annotation;
-import org.cleartk.classifier.CleartkProcessingException;
-import org.cleartk.classifier.Feature;
-import org.uimafit.util.JCasUtil;
/**
- * Annotate location_of relation between two entities whenever
+ * Annotate degree_of relation between two entities whenever
* they are enclosed within the same noun phrse.
*/
-public class Baseline3DegreeOfRelationExtractorAnnotator extends RelationExtractorAnnotator {
+public class Baseline3DegreeOfRelationExtractorAnnotator extends DegreeOfRelationExtractorAnnotator {
@Override
public List<IdentifiedAnnotationPair> getCandidateRelationArgumentPairs(
JCas identifiedAnnotationView, Sentence sentence) {
-
- List<EntityMention> entities = JCasUtil.selectCovered(identifiedAnnotationView, EntityMention.class, sentence);
- List<Modifier> modifiers = JCasUtil.selectCovered(identifiedAnnotationView, Modifier.class, sentence);
-
- List<IdentifiedAnnotationPair> pairs = new ArrayList<IdentifiedAnnotationPair>();
- for (EntityMention entity : entities) {
- for (Modifier modifier : modifiers) {
- pairs.add(new IdentifiedAnnotationPair(entity, modifier));
- }
- }
+ List<IdentifiedAnnotationPair> pairs = super.getCandidateRelationArgumentPairs(identifiedAnnotationView, sentence);
// find pairs enclosed inside a noun phrase
List<IdentifiedAnnotationPair> result = new ArrayList<IdentifiedAnnotationPair>();
@@ -77,17 +59,4 @@ public class Baseline3DegreeOfRelationEx
return result;
}
-
- @Override
- protected String getRelationCategory(
- Map<List<Annotation>, BinaryTextRelation> relationLookup,
- IdentifiedAnnotation arg1, IdentifiedAnnotation arg2) {
- BinaryTextRelation relation = relationLookup.get(Arrays.asList(arg1, arg2));
- return (relation != null) ? relation.getCategory() : NO_RELATION_CATEGORY;
- }
-
- @Override
- public String classify(List<Feature> features) throws CleartkProcessingException {
- return this.classifier.classify(features);
- }
}
Modified: incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline3EntityMentionPairRelationExtractorAnnotator.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline3EntityMentionPairRelationExtractorAnnotator.java?rev=1439730&r1=1439729&r2=1439730&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline3EntityMentionPairRelationExtractorAnnotator.java (original)
+++ incubator/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/ae/baselines/Baseline3EntityMentionPairRelationExtractorAnnotator.java Tue Jan 29 01:32:13 2013
@@ -19,20 +19,15 @@
package org.apache.ctakes.relationextractor.ae.baselines;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
-import java.util.Map;
import org.apache.ctakes.relationextractor.ae.RelationExtractorAnnotator;
-import org.apache.ctakes.typesystem.type.relation.BinaryTextRelation;
import org.apache.ctakes.typesystem.type.syntax.TreebankNode;
import org.apache.ctakes.typesystem.type.textsem.EntityMention;
import org.apache.ctakes.typesystem.type.textsem.IdentifiedAnnotation;
import org.apache.ctakes.typesystem.type.textspan.Sentence;
import org.apache.uima.jcas.JCas;
-import org.apache.uima.jcas.tcas.Annotation;
import org.cleartk.classifier.Feature;
-import org.uimafit.descriptor.ConfigurationParameter;
import org.uimafit.util.JCasUtil;
/**
@@ -41,17 +36,6 @@ import org.uimafit.util.JCasUtil;
*/
public class Baseline3EntityMentionPairRelationExtractorAnnotator extends RelationExtractorAnnotator {
- public static final String PARAM_CLASSIFY_BOTH_DIRECTIONS = "ClassifyBothDirections";
-
- @ConfigurationParameter(
- name = PARAM_CLASSIFY_BOTH_DIRECTIONS,
- mandatory = false,
- description = "run the classifier in both directions, that is, classify each pair of events "
- + "{X,Y} once in the order X-to-Y and once in the order Y-to-X (default: classify each "
- + "pair of events {X, Y} once, giving the label 'R' if a relation exists with the order "
- + "X-to-Y, and 'R-1' if a relation exists with the order Y-to-X)")
- protected boolean classifyBothDirections = false;
-
@Override
public List<IdentifiedAnnotationPair> getCandidateRelationArgumentPairs(
JCas identifiedAnnotationView, Sentence sentence) {
@@ -62,21 +46,16 @@ public class Baseline3EntityMentionPairR
EntityMention.class,
sentence);
- // Create pairings (this will change depending on the classification direction)
- List<IdentifiedAnnotationPair> pairs = new ArrayList<IdentifiedAnnotationPair>();
-
- for (int i = 0; i < args.size(); ++i) {
- EntityMention arg1 = args.get(i);
- int jStart = this.classifyBothDirections ? 0 : i + 1;
- for (int j = jStart; j < args.size(); ++j) {
- EntityMention arg2 = args.get(j);
- // skip identical entity mentions and mentions with identical spans
- if (i == j || (arg1.getBegin() == arg2.getBegin() && arg1.getEnd() == arg2.getEnd())) {
- continue;
- }
- pairs.add(new IdentifiedAnnotationPair(arg1, arg2));
- }
- }
+ // Create pairings
+ List<IdentifiedAnnotationPair> pairs = new ArrayList<IdentifiedAnnotationPair>();
+ for (EntityMention arg1 : args) {
+ for (EntityMention arg2 : args) {
+ if (arg1.getBegin() == arg2.getBegin() && arg1.getEnd() == arg2.getEnd()) {
+ continue;
+ }
+ pairs.add(new IdentifiedAnnotationPair(arg1, arg2));
+ }
+ }
// find pairs enclosed inside a noun phrase
List<IdentifiedAnnotationPair> result = new ArrayList<IdentifiedAnnotationPair>();
@@ -100,43 +79,6 @@ public class Baseline3EntityMentionPairR
return result;
}
- @Override
- protected String getRelationCategory(Map<List<Annotation>, BinaryTextRelation> relationLookup,
- IdentifiedAnnotation arg1, IdentifiedAnnotation arg2) {
-
- BinaryTextRelation relation = relationLookup.get(Arrays.asList(arg1, arg2));
- String category;
- if (this.classifyBothDirections) {
- // if classifying both directions, we'll see {X, Y} once when X is first and
- // once when Y is first, so just do the single direction lookup here
- if (relation != null) {
- category = relation.getCategory();
- } else if (coin.nextDouble() <= this.probabilityOfKeepingANegativeExample) {
- category = NO_RELATION_CATEGORY;
- } else {
- category = null;
- }
- } else {
- // if classifying in a single direction, we'll see {X, Y} only once,
- // so do lookups in both directions, and change the category name for
- // the relations in the reverse order
- if (relation != null) {
- category = relation.getCategory();
- } else {
- relation = relationLookup.get(Arrays.asList(arg2, arg1));
- if (relation != null) {
- // Change category name to show reverse order
- category = relation.getCategory() + "-1";
- } else if (coin.nextDouble() <= this.probabilityOfKeepingANegativeExample) {
- category = NO_RELATION_CATEGORY;
- } else {
- category = null;
- }
- }
- }
- return category;
- }
-
@Override
public String classify(List<Feature> features) {
return "location_of";