Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ object EntityRelationClassifiers {
object OrganizationClassifier extends Learnable(tokens) {
def label: Property[ConllRawToken] = entityType is "Org"
override lazy val classifier = new SparsePerceptron()
override def feature = using(word, windowWithin[ConllRawSentence](EntityRelationDataModel, -2, 2, List(pos)), phrase,
override def feature = using(word, tokensWithinWindowPos, phrase,
containsSubPhraseMent, containsSubPhraseIng, wordLen)
// The gazetteer properties are temporarily removed: containsInPersonList, containsInCityList
}

object PersonClassifier extends Learnable(tokens) {
def label: Property[ConllRawToken] = entityType is "Peop"
override def feature = using(word, windowWithin[ConllRawSentence](EntityRelationDataModel, -2, 2, List(pos)), phrase,
override def feature = using(word, tokensWithinWindowPos, phrase,
containsSubPhraseMent, containsSubPhraseIng, wordLen)
override lazy val classifier = new SparsePerceptron()
// The gazetteer properties are temporarily removed: containsInPersonList, containsInCityList
}

object LocationClassifier extends Learnable(tokens) {
def label: Property[ConllRawToken] = entityType is "Loc"
override def feature = using(word, windowWithin[ConllRawSentence](EntityRelationDataModel, -2, 2, List(pos)), phrase, containsSubPhraseMent,
override def feature = using(word, tokensWithinWindowPos, phrase, containsSubPhraseMent,
containsSubPhraseIng, wordLen)
override lazy val classifier = new SparsePerceptron()
// The gazetteer properties are temporarily removed: containsInPersonList, containsInCityList
Expand All @@ -41,13 +41,13 @@ object EntityRelationClassifiers {
/** independent relation classifiers */
object WorksForClassifier extends Learnable(pairs) {
def label: Property[ConllRelation] = relationType is "Work_For"
override def feature = using(relFeature, relPos)
override def feature = using(relFeature, tokensWithinWindowRelPos)
override lazy val classifier = new SparsePerceptron()
}

object LivesInClassifier extends Learnable(pairs) {
def label: Property[ConllRelation] = relationType is "Live_In"
override def feature = using(relFeature, relPos)
override def feature = using(relFeature, tokensWithinWindowRelPos)
override lazy val classifier = new SparsePerceptron()
}

Expand All @@ -64,13 +64,13 @@ object EntityRelationClassifiers {
/** relation pipeline classifiers */
object WorksForClassifierPipeline extends Learnable(pairs) {
override def label: Property[ConllRelation] = relationType is "Work_For"
override def feature = using(relFeature, relPos, entityPrediction)
override def feature = using(relFeature, tokensWithinWindowRelPos, entityPrediction)
override lazy val classifier = new SparsePerceptron()
}

object LivesInClassifierPipeline extends Learnable(pairs) {
override def label: Property[ConllRelation] = relationType is "Live_In"
override def feature = using(relFeature, relPos, entityPrediction)
override def feature = using(relFeature, tokensWithinWindowRelPos, entityPrediction)
override lazy val classifier = new SparsePerceptron()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ package edu.illinois.cs.cogcomp.saulexamples.nlp.EntityRelation

import edu.illinois.cs.cogcomp.saul.datamodel.DataModel
import edu.illinois.cs.cogcomp.saulexamples.EntityMentionRelation.datastruct.{ ConllRawSentence, ConllRawToken, ConllRelation }
import edu.illinois.cs.cogcomp.saulexamples.EntityMentionRelation.reader.Conll04_Reader
import edu.illinois.cs.cogcomp.saulexamples.nlp.EntityRelation.EntityRelationClassifiers._
import edu.illinois.cs.cogcomp.saulexamples.nlp.EntityRelation.EntityRelationSensors._

import scala.collection.JavaConversions._
import scala.collection.JavaConverters._

object EntityRelationDataModel extends DataModel {

Expand Down Expand Up @@ -82,19 +81,20 @@ object EntityRelationDataModel extends DataModel {
Nil
}

val relPos = property(pairs) {
rela: ConllRelation =>
val e1 = rela.e1
val e2 = rela.e2
val tokensWithinWindowPos = property(tokens) { t: ConllRawToken =>
val allSentenceTokens = t.s.sentTokens.asScala
val indexOpt = allSentenceTokens.zipWithIndex.collectFirst { case (item, idx) if item == t => idx }
require(indexOpt.isDefined, "ERROR: the token not found in the sentence!")
val minInd = Math.max(indexOpt.get - 2, 0)
val maxInd = Math.min(indexOpt.get + 2, allSentenceTokens.length - 1)
val tokensInWindow = allSentenceTokens.slice(minInd, maxInd)
tokensInWindow.map(pos(_)).toList
}

this.tokens.getWithWindow(e1, -2, 2, _.sentId).zipWithIndex.map {
case (Some(t), idx) => s"left-$idx-pos-${t.POS} "
case (None, idx) => s"left-$idx-pos-EMPTY "
} ++
this.tokens.getWithWindow(e2, -2, 2, _.sentId).zipWithIndex.map {
case (Some(t), idx) => s"right-$idx-pos-${t.POS} "
case (None, idx) => s"right-$idx-pos-EMPTY} "
}
val tokensWithinWindowRelPos = property(pairs) { rela: ConllRelation =>
val e1 = rela.e1
val e2 = rela.e2
tokensWithinWindowPos(e1) ++ tokensWithinWindowPos(e2)
}

val entityPrediction = property[ConllRelation](pairs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,16 @@ object EntityRelationSensors {
def relationToSecondArg_MatchingSensor(r: ConllRelation, t: ConllRawToken): Boolean = {
r.sentId.equals(t.sentId) && r.e2.wordId == t.wordId
}

def getTokenBefore(t: ConllRawToken): ConllRawToken = {
val indexOpt = t.s.sentTokens.asScala.zipWithIndex.collectFirst { case (item, idx) if item == t => idx }
require(indexOpt.isDefined, "ERROR: the token not found in the sentence!")
t.s.sentTokens.get(Math.min(indexOpt.get - 1, 0))
}

def getTokenAfter(t: ConllRawToken): ConllRawToken = {
val indexOpt = t.s.sentTokens.asScala.zipWithIndex.collectFirst { case (item, idx) if item == t => idx }
require(indexOpt.isDefined, "ERROR: the token not found in the sentence!")
t.s.sentTokens.get(Math.min(indexOpt.get + 1, t.s.sentTokens.size() - 1))
}
}