Skip to content

Commit

Permalink
examples : support minItems/maxItems in JSON grammar converter (ggerg…
Browse files Browse the repository at this point in the history
…anov#5039)

* support minLength and maxLength in JSON schema grammar converter

* Update examples/json-schema-to-grammar.py

---------

Co-authored-by: Georgi Gerganov <[email protected]>
  • Loading branch information
nopperl and ggerganov authored Feb 19, 2024
1 parent 1387cf6 commit 9d679f0
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion examples/json-schema-to-grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,21 @@ def visit(self, schema, name):
elif schema_type == 'array' and 'items' in schema:
# TODO `prefixItems` keyword
item_rule_name = self.visit(schema['items'], f'{name}{"-" if name else ""}item')
rule = f'"[" space ({item_rule_name} ("," space {item_rule_name})*)? "]" space'
list_item_operator = f'("," space {item_rule_name})'
successive_items = ""
min_items = schema.get("minItems", 0)
if min_items > 0:
first_item = f"({item_rule_name})"
successive_items = list_item_operator * (min_items - 1)
min_items -= 1
else:
first_item = f"({item_rule_name})?"
max_items = schema.get("maxItems")
if max_items is not None and max_items > min_items:
successive_items += (list_item_operator + "?") * (max_items - min_items - 1)
else:
successive_items += list_item_operator + "*"
rule = f'"[" space {first_item} {successive_items} "]" space'
return self._add_rule(rule_name, rule)

else:
Expand Down

0 comments on commit 9d679f0

Please sign in to comment.