-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cfc446f
commit f446804
Showing
5 changed files
with
242 additions
and
116 deletions.
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
core/src/test/scala/ai/lum/odinson/events/TestEventsWithState.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
76
core/src/test/scala/ai/lum/odinson/events/TestProblemGrammar.scala
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
core/src/test/scala/ai/lum/odinson/patterns/TestQuantifiedPatterns.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
core/src/test/scala/ai/lum/odinson/state/TestMemoryState.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters