Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/regex tag #81

Merged
merged 6 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions changes/81.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
introduce the TAG element as a module. Needs a new parser for the SpaCy translate.
Would allow more flexible matching of detailed part-of-speech tag, like all adjectives or nouns: TAG("^NN|^JJ").

Implemented by:
Roland M. Mueller (https://github.com/rolandmueller)
13 changes: 13 additions & 0 deletions rita/engine/translate_spacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ def phrase_parse(value, config, op=None):
yield generic_parse("ORTH", value, config=config, op=None)


def tag_parse(r, config, op=None):
"""
For generating POS/TAG patterns based on a Regex
e.g. TAG("^NN|^JJ") for adjectives or nouns
"""
d = {"TAG": {"REGEX": r}}

if op:
d["OP"] = op
yield d


PARSERS = {
"any_of": any_of_parse,
"value": partial(generic_parse, "ORTH"),
Expand All @@ -84,6 +96,7 @@ def phrase_parse(value, config, op=None):
"punct": punct_parse,
"fuzzy": fuzzy_parse,
"phrase": phrase_parse,
"tag": tag_parse,
}


Expand Down
9 changes: 9 additions & 0 deletions rita/modules/tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rita.macros import resolve_value


def TAG(name, config, op=None):
"""
For generating POS/TAG patterns based on a Regex
e.g. TAG("^NN|^JJ") for nouns or adjectives
"""
return "tag", resolve_value(name, config=config), op
15 changes: 15 additions & 0 deletions tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,21 @@ def test_optional_list(self):
"pattern": [{"LOWER": {"REGEX": "^(one|two)$"}, "OP": "?"}]
}

def test_tag_module(self):
rules = self.compiler("""
!IMPORT("rita.modules.tag")

TAG("^NN|^JJ")->MARK("TEST_TAG")
""")

print(rules)

assert len(rules) == 1
assert rules[0] == {
"label": "TEST_TAG",
"pattern": [{"TAG": {"REGEX": "^NN|^JJ"}}]
}


class TestStandalone(object):
@property
Expand Down