Skip to content

Commit

Permalink
fix: update func api in debugging_custom_grammar.md (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saibo-creator authored Jun 9, 2024
1 parent 8f8717a commit 86eccdf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 25 deletions.
8 changes: 4 additions & 4 deletions docs/debugging_custom_grammars.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@ We provide a simple script to do this:

```python
from transformers_cfg.parser import parse_ebnf
from transformers_cfg.recognizer import GrammarRecognizer
from transformers_cfg.recognizer import StringRecognizer

with open("examples/grammars/json.ebnf", "r") as file:
input_text = file.read()
parsed_grammar = parse_ebnf(input_text)

start_rule_id = parsed_grammar.symbol_table["root"]
recognizer = GrammarRecognizer(parsed_grammar.grammar_encoding, start_rule_id)
recognizer = StringRecognizer(parsed_grammar.grammar_encoding, start_rule_id)

# Test the grammar with a simple input
json_input = '{"foo": "bar", "baz": "bat"}'
is_accepted = recognizer._accept_prefix(json_input, recognizer.stacks)
is_accepted = recognizer._accept_prefix(json_input)
print(is_accepted)
```

Expand All @@ -112,7 +112,7 @@ N.B. the recognizer can accept partial input, so you can try the following:

```python
json_input = '{"foo": "bar"'
is_accepted = recognizer._accept_prefix(json_input, recognizer.stacks)
is_accepted = recognizer._accept_prefix(json_input)
print(is_accepted)
```

Expand Down
23 changes: 2 additions & 21 deletions transformers_cfg/recognizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,28 +444,9 @@ def char_acceptance_at_element(self, element_offset):
logging.debug(acceptance)
return acceptance

# def _consume_code_points_new(
# self, code_points: List[int], stacks: Set[Tuple[int]], verbose=False
# ) -> Set[Tuple[int]]:
# new_stacks: Set[Tuple[int]] = set()
# for stack in stacks:
# new_stacks.update(
# self._consume_code_points_per_stack(tuple(code_points), stack, verbose)
# )
# return new_stacks
#
# @lru_cache(maxsize=30000)
# def _consume_code_points_per_stack(
# self, code_points: Tuple[int], stack: Tuple[int], verbose=False
# ) -> Set[Tuple[int]]:
# stacks = {stack}
#
# for code_point in code_points:
# # Update the stacks variable by consuming each code point.
# stacks = self._consume_code_point_for_all_stacks(code_point, (stack,))
#
# return stacks

# backward compatibility, add alias of StringRecognizer to GrammarRecognizer
GrammarRecognizer = StringRecognizer

if __name__ == "__main__":
# set logging level
Expand Down

0 comments on commit 86eccdf

Please sign in to comment.