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

Escape curly braces in the regexes #243

Merged
merged 2 commits into from
Sep 10, 2023
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
16 changes: 9 additions & 7 deletions outlines/text/json_schema.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import itertools
import json
import re
from typing import Dict

STRING = r'".*"'
STRING_INNER = r'(?:[^"\\]|\\.)'
STRING = f'"{STRING_INNER}*"'
INTEGER = r"(0|[1-9][0-9]*)"
NUMBER = rf"(-)?({INTEGER})(\.[0-9]+)?([eE][+-][0-9]+)?"
BOOLEAN = r"(true|false)"
Expand Down Expand Up @@ -192,7 +194,7 @@ def match_step_to_regex(step):
Parameters
----------
step:
A string that represents the schema's structure, or a dictionnary
A string that represents the schema's structure, or a dictionary
that represents a field in the schema.

Returns
Expand All @@ -203,13 +205,13 @@ def match_step_to_regex(step):
"""
match step:
case str() as step:
return step
return re.escape(step)

case {"enum": choices, "type": "string"}:
choices = [f'"{choice}"' for choice in choices]
choices = [f'"{re.escape(choice)}"' for choice in choices]
return f"({'|'.join(choices)})"
case {"enum": choices}:
choices = [str(choice) for choice in choices]
choices = [re.escape(str(choice)) for choice in choices]
return f"({'|'.join(choices)})"

case {"type": "array", "items": items}:
Expand All @@ -224,9 +226,9 @@ def match_step_to_regex(step):
return regex_str

case {"type": "string", "maxLength": max_length}:
return f'".{{,{max_length}}}"'
return f'"{STRING_INNER}{{,{max_length}}}"'
case {"type": "string", "minLength": min_length}:
return f'".{{{min_length},}}"'
return f'"{STRING_INNER}{{{min_length},}}"'

case {"type": field_type}:
return type_to_regex[field_type]
Expand Down
27 changes: 19 additions & 8 deletions tests/text/test_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
NULL,
NUMBER,
STRING,
STRING_INNER,
build_schedule_from_schema,
match_step_to_regex,
)
Expand Down Expand Up @@ -258,13 +259,13 @@ def test_match_number(pattern, does_match):
),
(
{"title": "Foo", "type": "string", "maxLength": 3},
'".{,3}"',
[('"ab"', True), ('"abcd"', False)],
f'"{STRING_INNER}{{,3}}"',
[('"ab"', True), ('"a""', False), ('"abcd"', False)],
),
(
{"title": "Foo", "type": "string", "minLength": 3},
'".{3,}"',
[('"ab"', False), ('"abcd"', True)],
f'"{STRING_INNER}{{3,}}"',
[('"ab"', False), ('"abcd"', True), ('"abc""', False)],
),
(
{"title": "Foo", "type": "boolean"},
Expand All @@ -290,6 +291,7 @@ def test_match_number(pattern, does_match):
f"({STRING}|{NUMBER})",
[
('"string"', True),
('"st"ring"', False),
("1000", True),
("true", False),
],
Expand All @@ -299,6 +301,11 @@ def test_match_number(pattern, does_match):
'("Marc"|"Jean")',
[('"Marc"', True), ('"Jean"', True), ('"John"', False)],
),
(
{"title": "Foo", "enum": [".*", r"\s*"], "type": "string"},
r'("\.\*"|"\\s\*")',
[('".*"', True), (r'"\s*"', True), (r'"\.\*"', False)],
Comment on lines +305 to +307
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a new test case that fails without the fixes in this PR.

),
(
{"title": "Foo", "enum": [0, 1], "type": "integer"},
"(0|1)",
Expand All @@ -310,7 +317,7 @@ def test_match_number(pattern, does_match):
"type": "object",
"properties": {"count": {"title": "Count", "type": "integer"}},
},
'{\n "count": ' + INTEGER + "\n}",
'\\{\\\n\\ \\ "count":\\ ' + INTEGER + "\\\n\\}",
[('{\n "count": 100\n}', True)],
),
(
Expand Down Expand Up @@ -339,16 +346,20 @@ def test_match_number(pattern, does_match):
}
},
},
'{\n "fuzz": {\n "spam": ' + INTEGER + "\n }\n}",
'\\{\\\n\\ \\ "fuzz":\\ \\{\\\n\\ \\ \\ \\ "spam":\\ '
+ INTEGER
+ "\\\n\\ \\ \\}\\\n\\}",
[('{\n "fuzz": {\n "spam": 100\n }\n}', True)],
),
],
)
def test_match(step, regex, examples):
assert match_step_to_regex(step) == regex
test_regex = match_step_to_regex(step)

assert test_regex == regex

for string, does_match in examples:
match = re.fullmatch(regex, string)
match = re.fullmatch(test_regex, string)
if does_match:
assert match[0] == string
assert match.span() == (0, len(string))
Expand Down
Loading