Skip to content

Commit

Permalink
Refactor tests and isolate bug
Browse files Browse the repository at this point in the history
  • Loading branch information
myedibleenso committed Feb 17, 2024
1 parent cfc446f commit f446804
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 116 deletions.
143 changes: 143 additions & 0 deletions core/src/test/scala/ai/lum/odinson/events/TestEventsWithState.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package ai.lum.odinson.events

import ai.lum.odinson.test.utils.OdinsonTest

class TestEventsWithState extends OdinsonTest {

"Odinson" should "match events referencing existing mentions when a in-memory state is used" in {
val ee = ot.extractorEngineWithSpecificState(ot.getDocument("step-bros"), "memory")

val grammar = """
rules:
- name: person-rule
type: basic
priority: 1
label: Person
pattern: |
[tag=NNP]{3}
- name: state-based-rule
type: event
label: EventBasedPersonRule
priority: 2
pattern: |
trigger = [lemma=play]
# NOTE: ending with @Person won't work
arg: Person = >nsubj
"""

val extractors = ee.compileRuleString(grammar).toVector

val mentions = ee.extractMentions(
extractors = extractors,
allowTriggerOverlaps = false,
disableMatchSelector = false
).toVector

// mentions.foreach{ m => println(s"${m.label.get}\t${m.foundBy}\t(${m.start}, ${m.end})")}
getMentionsWithLabel(mentions, "EventBasedPersonRule") should have size (1)
}

it should "not match events referencing existing mentions when no state is used" in {
val ee = ot.extractorEngineWithSpecificState(ot.getDocument("step-bros"), "mock")

val grammar = """
rules:
- name: person-rule
type: basic
priority: 1
label: Person
pattern: |
[tag=NNP]{3}
- name: state-based-rule
type: event
label: EventBasedPersonRule
priority: 2
pattern: |
trigger = [lemma=play]
# NOTE: ending with @Person won't work
arg: Person = >nsubj
"""

val extractors = ee.compileRuleString(grammar).toVector

val mentions = ee.extractMentions(
extractors = extractors,
allowTriggerOverlaps = false,
disableMatchSelector = false
).toVector

getMentionsWithLabel(mentions, "EventBasedPersonRule") should have size (0)
}

it should "match events referencing existing mentions when a in-memory state is used along with a metadataFilter" in {
val ee = ot.extractorEngineWithSpecificState(ot.getDocument("step-bros"), "memory")

val grammar = """
metadataFilters: doc_id == 'step-bros'
rules:
- name: person-rule
type: basic
priority: 1
label: Person
pattern: |
[tag=NNP]{3}
- name: state-based-rule
type: event
label: EventBasedPersonRule
priority: 2
pattern: |
trigger = [lemma=play]
# NOTE: ending with @Person won't work
arg: Person = >nsubj
"""

val extractors = ee.compileRuleString(grammar).toVector

val mentions = ee.extractMentions(
extractors = extractors,
allowTriggerOverlaps = false,
disableMatchSelector = false
).toVector

getMentionsWithLabel(mentions, "EventBasedPersonRule") should have size (1)
}

it should "not match events referencing existing mentions when no in-memory state is used (metadataQuery invariant)" in {
val ee = ot.extractorEngineWithSpecificState(ot.getDocument("step-bros"), "memory")

val grammar = """
metadataFilters: doc_id == 'step-bros'
rules:
- name: person-rule
type: basic
priority: 1
label: Person
pattern: |
[tag=NNP]{3}
- name: state-based-rule
type: event
label: EventBasedPersonRule
priority: 2
pattern: |
trigger = [lemma=play]
# NOTE: ending with @Person won't work
arg: Person = >nsubj
"""

val extractors = ee.compileRuleString(grammar).toVector

val mentions = ee.extractMentions(
extractors = extractors,
allowTriggerOverlaps = false,
disableMatchSelector = false
).toVector

getMentionsWithLabel(mentions, "EventBasedPersonRule") should have size (1)
}
}
76 changes: 0 additions & 76 deletions core/src/test/scala/ai/lum/odinson/events/TestProblemGrammar.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ai.lum.odinson.patterns

import ai.lum.odinson.test.utils.OdinsonTest

class TestQuantifiedPatterns extends OdinsonTest {

"Odinson" should "match simple patterns with exact range quantifiers" in {

// create in-memory engine w/ a single doc w/ a single sentence
val ee = mkExtractorEngine("step-bros")
// "John", "C.", "Reilly", "played" ...
val pattern = "[lemma=play] >nsubj [tag=NNP]{3}"
val oq = ee.mkQuery(pattern)
val res = ee.query(oq)

numMatches(res) should be (1)
existsMatchWithSpan(odinResults = res, doc = 0, start = 0, end = 3) should be (1)
}

it should "match simple patterns with open range quantifiers and a metadata filter" in {

// create in-memory engine w/ a single doc w/ a single sentence
val ee = mkExtractorEngine("step-bros")
// "John", "C.", "Reilly", "played" ...
val pattern = "[lemma=play] >nsubj [tag=NNP]{,3}"
val mf = "doc_id == 'step-bros'"
val oq = ee.mkFilteredQuery(query = pattern, metadataFilter = mf)

val res = ee.query(oq)

numMatches(res) should be (1)
existsMatchWithSpan(odinResults = res, doc = 0, start = 0, end = 3) should be (1)
}

it should "match simple patterns with exact range quantifiers (>=2) and a metadata filter" in {

// create in-memory engine w/ a single doc w/ a single sentence
val ee = mkExtractorEngine("step-bros")
// "John", "C.", "Reilly", "played" ...
val pattern = "[lemma=play] >nsubj [tag=NNP]{3}"
val mf = "doc_id == 'step-bros'"
val oq = ee.mkFilteredQuery(query = pattern, metadataFilter = mf)

val res = ee.query(oq)

numMatches(res) should be (1)
existsMatchWithSpan(odinResults = res, doc = 0, start = 0, end = 3) should be (1)
}
}
49 changes: 49 additions & 0 deletions core/src/test/scala/ai/lum/odinson/state/TestMemoryState.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ai.lum.odinson.state

import ai.lum.odinson.test.utils.OdinsonTest

class TestMemoryState extends OdinsonTest {

val docGummy = getDocument("becky-gummy-bears-v2")

val eeGummyMemory = extractorEngineWithSpecificState(docGummy, "memory")

"MemoryState" should "return mentions" in {
val rules = """
|rules:
| - name: gummy-rule
| label: Bear
| type: basic
| priority: 1
| pattern: |
| gummy
|
| - name: eating-rule
| label: Consumption
| type: event
| priority: 2
| pattern: |
| trigger = [lemma=eat]
| subject: ^NP = >nsubj []
| object: ^NP = >dobj []
|
| - name: nomatch-rule
| label: Gummy
| type: event
| priority: 2
| pattern: |
| trigger = bears
| arg: Bear = >amod
""".stripMargin

val extractors = eeGummyMemory.ruleReader.compileRuleString(rules)
val mentions = eeGummyMemory.extractMentions(extractors).toArray

// the 3 main extractions + 2 promoted args
mentions should have size (5)

getMentionsWithLabel(mentions, "Gummy") should have size (1)

}

}
41 changes: 1 addition & 40 deletions core/src/test/scala/ai/lum/odinson/state/TestMockState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ class TestMockState extends OdinsonTest {
val docGummy = getDocument("becky-gummy-bears-v2")

val eeGummy = extractorEngineWithSpecificState(docGummy, "mock")
val eeGummyMemory = extractorEngineWithSpecificState(docGummy, "memory")

"MockState" should "return mentions" in {
"MockState" should "return mentions that don't rely on state-based retrieval" in {
val rules = """
|rules:
| - name: gummy-rule
Expand Down Expand Up @@ -47,42 +46,4 @@ class TestMockState extends OdinsonTest {

}

"MemoryState" should "return mentions" in {
val rules = """
|rules:
| - name: gummy-rule
| label: Bear
| type: basic
| priority: 1
| pattern: |
| gummy
|
| - name: eating-rule
| label: Consumption
| type: event
| priority: 2
| pattern: |
| trigger = [lemma=eat]
| subject: ^NP = >nsubj []
| object: ^NP = >dobj []
|
| - name: nomatch-rule
| label: Gummy
| type: event
| priority: 2
| pattern: |
| trigger = bears
| arg: Bear = >amod
""".stripMargin

val extractors = eeGummyMemory.ruleReader.compileRuleString(rules)
val mentions = eeGummyMemory.extractMentions(extractors).toArray

// the 3 main extractions + 2 promoted args
mentions should have size (5)

getMentionsWithLabel(mentions, "Gummy") should have size (1)

}

}

0 comments on commit f446804

Please sign in to comment.