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

Update #1

Merged
merged 4 commits into from
Aug 20, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/77.fix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix IN_LIST operation bug - it was ignoring them
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "rita-dsl"
version = "0.5.6"
version = "0.5.7"
description = "DSL for building language rules"
authors = [
"Šarūnas Navickas <zaibacu@gmail.com>"
Expand Down
2 changes: 1 addition & 1 deletion rita/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

logger = logging.getLogger(__name__)

__version__ = (0, 5, 6, os.getenv("VERSION_PATCH"))
__version__ = (0, 5, 7, os.getenv("VERSION_PATCH"))


def get_version():
Expand Down
3 changes: 2 additions & 1 deletion rita/engine/translate_standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def apply_operator(syntax, op):


def any_of_parse(lst, op=None):
return apply_operator(r"(^|\s)(({0})\s?)".format("|".join(sorted(lst, key=lambda x: (-len(x), x)))), op)
clause = r"((^|\s)(({0})\s?))".format("|".join(sorted(lst, key=lambda x: (-len(x), x))))
return apply_operator(clause, op)


def regex_parse(r, op=None):
Expand Down
2 changes: 1 addition & 1 deletion rita/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def ASSIGN(k, v, config, op=None):

def IN_LIST(*args, config, op=None):
return "any_of", [resolve_value(arg, config=config)
for arg in flatten(args)], None
for arg in flatten(args)], op


def PATTERN(*args, config, op=None):
Expand Down
43 changes: 34 additions & 9 deletions tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,20 @@ def test_multiple_optionals(self):
]
}

def test_optional_list(self):
rules = self.compiler("""
elements = {"one", "two"}
{IN_LIST(elements)?}->MARK("OPTIONAL_LIST")
""")

print(rules)

assert len(rules) == 1
assert rules[0] == {
"label": "OPTIONAL_LIST",
"pattern": [{"LOWER": {"REGEX": "^(one|two)$"}, "OP": "?"}]
}


class TestStandalone(object):
@property
Expand Down Expand Up @@ -311,7 +325,7 @@ def test_multiple_words(self):
''')
print(rules)
assert len(rules) == 1
assert rules[0] == re.compile(r"(?P<MULTI_LABEL>(^|\s)((test1|test2)\s?))", self.flags)
assert rules[0] == re.compile(r"(?P<MULTI_LABEL>((^|\s)((test1|test2)\s?)))", self.flags)

def test_simple_pattern(self):
rules = self.compiler('''
Expand Down Expand Up @@ -349,19 +363,19 @@ def test_or_branch_multi_w_single(self):
print(rules)
assert len(rules) == 4
assert rules[0] == re.compile(
r"(?P<MULTI_SPLIT_LABEL>(test1\s?)([.,!;?:]\s?)?(^|\s)((three|one|two)\s?)([.,!;?:]\s?)?(test3\s?))",
r"(?P<MULTI_SPLIT_LABEL>(test1\s?)([.,!;?:]\s?)?((^|\s)((three|one|two)\s?))([.,!;?:]\s?)?(test3\s?))",
self.flags
)
assert rules[1] == re.compile(
r"(?P<MULTI_SPLIT_LABEL>(test2\s?)([.,!;?:]\s?)?(^|\s)((three|one|two)\s?)([.,!;?:]\s?)?(test3\s?))",
r"(?P<MULTI_SPLIT_LABEL>(test2\s?)([.,!;?:]\s?)?((^|\s)((three|one|two)\s?))([.,!;?:]\s?)?(test3\s?))",
self.flags
)
assert rules[2] == re.compile(
r"(?P<MULTI_SPLIT_LABEL>(test1\s?)([.,!;?:]\s?)?(^|\s)((three|one|two)\s?)([.,!;?:]\s?)?(test4\s?))",
r"(?P<MULTI_SPLIT_LABEL>(test1\s?)([.,!;?:]\s?)?((^|\s)((three|one|two)\s?))([.,!;?:]\s?)?(test4\s?))",
self.flags
)
assert rules[3] == re.compile(
r"(?P<MULTI_SPLIT_LABEL>(test2\s?)([.,!;?:]\s?)?(^|\s)((three|one|two)\s?)([.,!;?:]\s?)?(test4\s?))",
r"(?P<MULTI_SPLIT_LABEL>(test2\s?)([.,!;?:]\s?)?((^|\s)((three|one|two)\s?))([.,!;?:]\s?)?(test4\s?))",
self.flags
)

Expand All @@ -372,7 +386,7 @@ def test_branching_list(self):
''')
print(rules)
assert len(rules) == 2
assert rules[0] == re.compile(r"(?P<SPLIT_LIST>(^|\s)((test1|test2|test4)\s?))", self.flags)
assert rules[0] == re.compile(r"(?P<SPLIT_LIST>((^|\s)((test1|test2|test4)\s?)))", self.flags)
assert rules[1] == re.compile(r"(?P<SPLIT_LIST>(test-3\s?))", self.flags)

def test_word_with_accent(self):
Expand All @@ -381,7 +395,7 @@ def test_word_with_accent(self):
''')
print(rules)
assert len(rules) == 1
assert rules[0] == re.compile(r"(?P<TWO_WORDS>(^|\s)((Sarunas|Šarūnas)\s?))", self.flags)
assert rules[0] == re.compile(r"(?P<TWO_WORDS>((^|\s)((Sarunas|Šarūnas)\s?)))", self.flags)

def test_list_with_accent(self):
rules = self.compiler('''
Expand All @@ -390,7 +404,7 @@ def test_list_with_accent(self):
''')
print(rules)
assert len(rules) == 1
assert rules[0] == re.compile(r"(?P<EXTENDED_LIST>(^|\s)((Sarunas|Šarūnas|Jurgis|Jonas)\s?))", self.flags)
assert rules[0] == re.compile(r"(?P<EXTENDED_LIST>((^|\s)((Sarunas|Šarūnas|Jurgis|Jonas)\s?)))", self.flags)

def test_double_op(self):
rules = self.compiler('''
Expand All @@ -415,7 +429,7 @@ def test_prefix_on_list(self):
''')
print(rules)
assert len(rules) == 1
assert rules[0] == re.compile(r"(?P<META_LIST>(^|\s)((metamathematics|metaphysics)\s?))", self.flags)
assert rules[0] == re.compile(r"(?P<META_LIST>((^|\s)((metamathematics|metaphysics)\s?)))", self.flags)

def test_prefix_on_unknown_type(self):
rules = self.compiler('''
Expand All @@ -435,3 +449,14 @@ def test_save_and_load_rules_from_file(self):
engine.save(rules_path)
engine.load(rules_path)
engine.execute("Hello world")

def test_optional_list(self):
rules = self.compiler("""
elements = {"one", "two"}
{IN_LIST(elements)?}->MARK("OPTIONAL_LIST")
""")

print(rules)

assert len(rules) == 1
assert rules[0] == re.compile(r"(?P<OPTIONAL_LIST>((^|\s)((one|two)\s?))?)", self.flags)