Skip to content

Commit

Permalink
Merge pull request #111 from writerai/NLP-82--add-tag-to-relative-x-is-y
Browse files Browse the repository at this point in the history
Add tag to relative_x_is_y
  • Loading branch information
manhal-daaboul authored Feb 28, 2022
2 parents 3317650 + 9656b36 commit 00a5ff9
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions replacy/default_match_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ def relative_x_is_y(
"children_or_ancestors must be set to either `children` or `ancestors`"
)

if pos_or_dep not in ["pos", "dep"]:
raise ValueError("pos_or_dep must be set to either `pos` or `dep`!")
if pos_or_dep not in ["pos", "dep", "tag"]:
raise ValueError("pos_or_dep must be set to either `pos`, `dep`, or `tag`!")

def _in_children(doc, start, end):
if end >= len(doc):
Expand All @@ -227,9 +227,9 @@ def _in_children(doc, start, end):
[child.pos_ == val for tok in match_span for child in tok.children]
)
elif pos_or_dep == "dep":
return any(
[child.dep_ == val for tok in match_span for child in tok.children]
)
return any([child.dep_ == val for tok in match_span for child in tok.children])
elif pos_or_dep == "tag":
return any([child.tag_ == val for tok in match_span for child in tok.children])

def _in_ancestors(doc, start, end):
if end >= len(doc):
Expand All @@ -248,6 +248,12 @@ def _in_ancestors(doc, start, end):
if ancestor and ancestor.dep_ == val:
return True
return False
if pos_or_dep == "tag":
for t in match_span:
ancestor = list(t.ancestors)[0] if len(list(t.ancestors)) else None
if ancestor and ancestor.tag_ == val:
return True
return False

if children_or_ancestors == "children":
return _in_children
Expand Down

0 comments on commit 00a5ff9

Please # to comment.