Skip to content

Commit

Permalink
Move range change
Browse files Browse the repository at this point in the history
  • Loading branch information
kwalcock committed Feb 25, 2022
1 parent 22d7162 commit 8758be8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ abstract class EidosOntologyGrounder(val name: String, val domainOntology: Domai

// If there was an exact match, returns Some of a tuple including the SingleOntologyNodeGrounding and the
// Range of the match in the splitText so that we can tell how much of it was used. No match results in None.
def exactMatchForPreds(splitText: Array[String], embeddings: Seq[ConceptEmbedding]): Option[(OntologyNodeGrounding, Range)] = {
def exactMatchForPreds(splitText: Array[String], embeddings: Seq[ConceptEmbedding], range: Range): Option[(OntologyNodeGrounding, Range)] = {
// This looks for exact string overlap only!
// This tuple is designed so that Seq.min gets the intended result, the one with the min negLength
// (or max length) and in case of ties, the min position in the sentence, so the leftmost match.
// The embedding.namer should not be required to break ties. It goes along for the ride.
// The embedding.namer should not be required to break ties: it goes along for the ride.
// For expediency, the word count is used for length rather than the letter count.
val overlapTuples = embeddings.flatMap { embedding =>
val canonicalWords = embedding.namer.canonicalWords
Expand All @@ -121,14 +121,16 @@ abstract class EidosOntologyGrounder(val name: String, val domainOntology: Domai
val index = splitText.indexOfSlice(canonicalWords)
if (index < 0) None
// Part or maybe all of the split text was matched, indicated by 1, favored.
else Some(-canonicalWords.length, index, 1, embedding.namer)
// Add range.start because splitText does not always begin the sentence.
else Some(-canonicalWords.length, index + range.start, 1, embedding.namer)
}
else {
// Node name contains the text
val index = canonicalWords.indexOfSlice(splitText)
if (index < 0) None
// The entirety of splitText was matched, indicated by 2, disfavored.
else Some(-splitText.length, 0, 2, embedding.namer)
// Add range.start because splitText does not always begin the sentence.
else Some(-splitText.length, 0 + range.start, 2, embedding.namer)
}
}
val result = Collection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,12 @@ object PredicateTuple {
themeProcessProperties: OntologyGrounding,
predicates: Set[Int]
): PredicateTuple = new PredicateTuple(
theme.filterSlots(SRLCompositionalGrounder.CONCEPT),
themeProperties.filterSlots(SRLCompositionalGrounder.PROPERTY),
themeProcess.filterSlots(SRLCompositionalGrounder.PROCESS),
themeProcessProperties.filterSlots(SRLCompositionalGrounder.PROPERTY),
predicates
)
}
theme.filterSlots(SRLCompositionalGrounder.CONCEPT),
themeProperties.filterSlots(SRLCompositionalGrounder.PROPERTY),
themeProcess.filterSlots(SRLCompositionalGrounder.PROCESS),
themeProcessProperties.filterSlots(SRLCompositionalGrounder.PROPERTY),
predicates
)
}

class SRLCompositionalGrounder(name: String, domainOntology: DomainOntology, w2v: EidosWordToVec, canonicalizer: Canonicalizer, tokenizer: EidosTokenizer)
Expand Down Expand Up @@ -232,11 +231,11 @@ class SRLCompositionalGrounder(name: String, domainOntology: DomainOntology, w2v
Seq(PredicateGrounding(predicateTuple))
}

def findExactPredicateGroundingAndRange(mentionStrings: Array[String]): (PredicateGrounding, Range) = {
def findExactPredicateGroundingAndRange(mentionStrings: Array[String], mentionRange: Range): (PredicateGrounding, Range) = {
// Try to exact match entire token interval
val bestExactSingleOntologyNodeGroundingAndRangeOpt: Option[(OntologyNodeGrounding, Range)] = {
val exactSingleOntologyNodeGroundingAndRanges = SRLCompositionalGrounder.processOrConceptBranches.flatMap { branch =>
exactMatchForPreds(mentionStrings, conceptEmbeddingsMap(branch))
exactMatchForPreds(mentionStrings, conceptEmbeddingsMap(branch), mentionRange)
}
if (exactSingleOntologyNodeGroundingAndRanges.isEmpty) None
else if (exactSingleOntologyNodeGroundingAndRanges.length == 1)
Expand Down Expand Up @@ -293,8 +292,7 @@ class SRLCompositionalGrounder(name: String, domainOntology: DomainOntology, w2v
def groundWithPredicates(sentenceHelper: SentenceHelper): Seq[PredicateGrounding] = {
val mentionRange = Range(tokenInterval.start, tokenInterval.end)
val mentionStrings = mentionRange.map(s.lemmas.get(_).toLowerCase).toArray // used to be words, now lemmas
val (exactPredicateGrounding, relativeExactRange) = findExactPredicateGroundingAndRange(mentionStrings)
val exactRange = Range(relativeExactRange.start + mentionRange.start, relativeExactRange.end + mentionRange.start)
val (exactPredicateGrounding, exactRange) = findExactPredicateGroundingAndRange(mentionStrings, mentionRange)

if (exactRange.length >= mentionRange.length)
Seq(exactPredicateGrounding)
Expand Down

0 comments on commit 8758be8

Please sign in to comment.