Skip to content

Commit

Permalink
Allow a single pattern to match multiple traits
Browse files Browse the repository at this point in the history
  • Loading branch information
rafelafrance committed Oct 9, 2024
1 parent 4030ce3 commit f16ff3d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
22 changes: 17 additions & 5 deletions traiter/pylib/pipes/trait.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from spacy import util
from spacy.language import Language
from spacy.matcher import Matcher
from spacy.tokens import Doc
from spacy.tokens import Doc, Span

from traiter.pylib.pipes.reject_match import RejectMatch

Expand Down Expand Up @@ -64,17 +64,29 @@ def __call__(self, doc: Doc) -> Doc:
if ent_tokens & used_tokens:
continue

traits = None
if action := self.dispatch_table.get(label):
try:
ent._.trait = action(ent)
traits = action(ent)
except RejectMatch:
continue

used_tokens |= ent_tokens

self.relabel_ent(ent, label)

entities.append(ent)
if isinstance(traits, list):
for trait in traits:
sub_ent = Span(
doc=ent.doc,
start=trait._start_token,
end=trait._end_token,
label=trait._trait,
)
sub_ent._.trait = trait
entities.append(sub_ent)
else:
ent._.trait = traits
self.relabel_ent(ent, label)
entities.append(ent)

self.add_untouched_entities(doc, entities, used_tokens)

Expand Down
4 changes: 0 additions & 4 deletions traiter/pylib/rules/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ def from_ent(cls, ent, **kwargs):
kwargs["_text"] = ent.text
return cls(**kwargs)

@classmethod
def dummy_ent(cls, **kwargs):
return cls(**kwargs)

def to_dict(self) -> dict:
return {k: v for k, v in asdict(self).items() if v is not None and k[0] != "_"}

Expand Down

0 comments on commit f16ff3d

Please sign in to comment.