Skip to content

Commit

Permalink
Work towards #136
Browse files Browse the repository at this point in the history
  • Loading branch information
johann-petrak committed Sep 20, 2021
1 parent 4b8188e commit 501d263
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 71 deletions.
82 changes: 79 additions & 3 deletions gatenlp/pam/pampac/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,60 @@

class Getter(ABC):
"""
Common base class of all Getter helper classes.
Common base class of all Getter helper classes. A getter is a callable that takes
the success objet, context, location and optional resultidx and matchidx and returns something
related to a matching parser result. What gets returned is entirely up to the getter.
Getters can be used when creating standard predefined Action objects to configure the action
to use a match dependent value instead of a fixed value for any of its init parameters.
"""

def __init__(self, name=None, resultidx=0, matchidx=0, silent_fail=True):
self.name = name
self.silent_fail = silent_fail
self.resultidx = resultidx
self.matchidx = matchidx

def _get_match(self, succ, resultidx, matchidx):
use_resultidx = self.resultidx
use_matchidx = self.matchidx
if use_resultidx is None:
use_resultidx = resultidx
if use_matchidx is None:
use_matchidx = matchidx
match = _get_match(
succ, self.name, use_resultidx, use_matchidx, self.silent_fail
)
return match

def _get_span(self, succ, resultidx, matchidx):
use_resultidx = self.resultidx
use_matchidx = self.matchidx
if use_resultidx is None:
use_resultidx = resultidx
if use_matchidx is None:
use_matchidx = matchidx
match = _get_span(
succ, self.name, use_resultidx, use_matchidx, self.silent_fail
)
return match

@abstractmethod
def __call__(self, succ, context=None, location=None):
def __call__(self, succ, context=None, location=None, resultidx=None, matchidx=None):
"""
Every Getter class should implement this method, getter functions should have the same
signature (without self, of course).
Args:
succ: a success object
context: a context object
location: a location object
resultidx: the result index for which the action is executed, if None, use whatever is configured
when the getter gets initialized
matchidx: the match index for which the aciton is executed, if None, use whatever is configured
when the getter gets initialized
Returns: any data related to the match(es) in the success object
"""
pass


Expand Down Expand Up @@ -88,7 +138,33 @@ def _get_span(succ, name, resultidx=0, matchidx=0, silent_fail=False):
return ret


class Actions:
class Action(ABC):
"""
Action base class, defines method for applying some action function to one or all results / matches.
"""

def _run4result(self, func, resultidx, matchidx, succ, context, location):
if matchidx is None:
for match in succ[resultidx].matches:
match = succ[resultidx].matches[matchidx]
func(match, succ, context, location, resultidx=resultidx, matchidx=matchidx)
else:
match = succ[resultidx].matches[matchidx]
func(match, succ, context, location, resultidx=resultidx, matchidx=matchidx)

def _run4results(self, func, resultidx, matchidx, success=None, context=None, location=None):
if resultidx is None:
for resultidx in range(len(success)):
self._run4result(func, resultidx, matchidx, success, context, location)
else:
self._run4result(func, resultidx, matchidx, success, context, location)

@abstractmethod
def __call__(self, succ, context=None, location=None):
pass


class Actions(Action):
"""
A container to run several actions for a rule.
"""
Expand Down
125 changes: 57 additions & 68 deletions gatenlp/pam/pampac/getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Module for PAMPAC getter helper classes
"""

from gatenlp.pam.pampac.actions import Getter, _get_span, _get_match
from gatenlp.pam.pampac.actions import Getter


class GetAnn(Getter):
Expand All @@ -16,19 +16,32 @@ def __init__(self, name, resultidx=0, matchidx=0, silent_fail=False):
Args:
name: the name of the match to use.
resultidx: the index of the result to use if there is more than one.
matchidx: the index of the match info element with the given name to use if there is more than one
resultidx: the index of the result to use if there is more than one, if None, use the one(s)
processed by the action.
matchidx: the index of the match info element with the given name to use if there is more than one, if
None, use the one processed by the action
silent_fail: if True, do not raise an exception if the annotation cannot be found, instead return
None.
"""
self.name = name
self.resultidx = resultidx
self.matchidx = matchidx
self.silent_fail = silent_fail

def __call__(self, succ, context=None, location=None):
match = _get_match(
succ, self.name, self.resultidx, self.matchidx, self.silent_fail
super().__init__(name=name, resultidx=resultidx, matchidx=matchidx, silent_fail=silent_fail)

def __call__(self, succ, context=None, location=None, resultidx=None, matchidx=None):
"""
This is how the getter should get invoked by the action. If resultidx/matchidx is not None,
then we use these if our own init parms for these were None.
Args:
succ: succes object
context: context object
location: location object
resultidx: resultindex getting processed by the action
matchidx: match index getting processed by the action
Returns:
Something related to the match
"""
match = self._get_match(
succ, resultidx, matchidx
)
ann = match.get("ann")
if ann is None:
Expand All @@ -55,14 +68,11 @@ def __init__(self, name, resultidx=0, matchidx=0, silent_fail=False):
silent_fail: if True, do not raise an exception if the annotation cannot be found, instead return
None
"""
self.name = name
self.resultidx = resultidx
self.matchidx = matchidx
self.silent_fail = silent_fail

def __call__(self, succ, context=None, location=None):
match = _get_match(
succ, self.name, self.resultidx, self.matchidx, self.silent_fail
super().__init__(name=name, resultidx=resultidx, matchidx=matchidx, silent_fail=silent_fail)

def __call__(self, succ, context=None, location=None, resultidx=None, matchidx=None):
match = self._get_match(
succ, resultidx, matchidx
)
ann = match.get("ann")
if ann is None:
Expand All @@ -89,14 +99,11 @@ def __init__(self, name, resultidx=0, matchidx=0, silent_fail=False):
silent_fail: if True, do not raise an exception if the annotation cannot be found, instead return
None
"""
self.name = name
self.resultidx = resultidx
self.matchidx = matchidx
self.silent_fail = silent_fail

def __call__(self, succ, context=None, location=None):
match = _get_match(
succ, self.name, self.resultidx, self.matchidx, self.silent_fail
super().__init__(name=name, resultidx=resultidx, matchidx=matchidx, silent_fail=silent_fail)

def __call__(self, succ, context=None, location=None, resultidx=None, matchidx=None):
match = self._get_match(
succ, resultidx, matchidx
)
ann = match.get("ann")
if ann is None:
Expand All @@ -123,14 +130,11 @@ def __init__(self, name, resultidx=0, matchidx=0, silent_fail=False):
silent_fail: if True, do not raise an exception if the annotation cannot be found, instead return
None
"""
self.name = name
self.resultidx = resultidx
self.matchidx = matchidx
self.silent_fail = silent_fail

def __call__(self, succ, context=None, location=None):
match = _get_match(
succ, self.name, self.resultidx, self.matchidx, self.silent_fail
super().__init__(name=name, resultidx=resultidx, matchidx=matchidx, silent_fail=silent_fail)

def __call__(self, succ, context=None, location=None, resultidx=None, matchidx=None):
match = self._get_match(
succ, resultidx, matchidx
)
span = match["span"]
return span.start
Expand All @@ -152,14 +156,11 @@ def __init__(self, name, resultidx=0, matchidx=0, silent_fail=False):
silent_fail: if True, do not raise an exception if the annotation cannot be found, instead return
None
"""
self.name = name
self.resultidx = resultidx
self.matchidx = matchidx
self.silent_fail = silent_fail

def __call__(self, succ, context=None, location=None):
return _get_match(
succ, self.name, self.resultidx, self.matchidx, self.silent_fail
super().__init__(name=name, resultidx=resultidx, matchidx=matchidx, silent_fail=silent_fail)

def __call__(self, succ, context=None, location=None, resultidx=None, matchidx=None):
return self._get_match(
succ, resultidx, matchidx
)["span"].end


Expand All @@ -179,15 +180,12 @@ def __init__(self, name, featurename, resultidx=0, matchidx=0, silent_fail=False
silent_fail: if True, do not raise an exception if the annotation cannot be found, instead return
None
"""
self.name = name
self.resultidx = resultidx
self.matchidx = matchidx
self.silent_fail = silent_fail
super().__init__(name=name, resultidx=resultidx, matchidx=matchidx, silent_fail=silent_fail)
self.featurename = featurename

def __call__(self, succ, context=None, location=None):
match = _get_match(
succ, self.name, self.resultidx, self.matchidx, self.silent_fail
def __call__(self, succ, context=None, location=None, resultidx=None, matchidx=None):
match = self._get_match(
succ, resultidx, matchidx
)
ann = match.get("ann")
if ann is None:
Expand Down Expand Up @@ -215,18 +213,13 @@ def __init__(self, name=None, resultidx=0, matchidx=0, silent_fail=False):
silent_fail: if True, do not raise an exception if the annotation cannot be found, instead return
None
"""
self.name = name
self.resultidx = resultidx
self.matchidx = matchidx
self.silent_fail = silent_fail
super().__init__(name=name, resultidx=resultidx, matchidx=matchidx, silent_fail=silent_fail)

def __call__(self, succ, context=None, location=None):
def __call__(self, succ, context=None, location=None, resultidx=None, matchidx=None):
if self.name is None:
span = _get_span(succ, self.name, self.resultidx, self.matchidx, self.silent_fail)
span = self._get_span(succ, resultidx, matchidx)
else:
match = _get_match(
succ, self.name, self.resultidx, self.matchidx, self.silent_fail
)
match = self._get_match(succ, resultidx, matchidx)
span = match.get("span")
if span:
return context.doc[span]
Expand All @@ -253,15 +246,11 @@ def __init__(self, name, group=0, resultidx=0, matchidx=0, silent_fail=False):
silent_fail: if True, do not raise an exception if the annotation cannot be found, instead return
None
"""
self.name = name
self.resultidx = resultidx
self.matchidx = matchidx
self.group = group
self.silent_fail = silent_fail

def __call__(self, succ, context=None, location=None):
match = _get_match(
succ, self.name, self.resultidx, self.matchidx, self.silent_fail
super().__init__(name=name, resultidx=resultidx, matchidx=matchidx, silent_fail=silent_fail)

def __call__(self, succ, context=None, location=None, resultidx=None, matchidx=None):
match = self._get_match(
succ, resultidx, matchidx
)
groups = match.get("groups")
if groups:
Expand Down

0 comments on commit 501d263

Please sign in to comment.