Skip to content

Commit 86eccdf

Browse files
fix: update func api in debugging_custom_grammar.md (#56)
1 parent 8f8717a commit 86eccdf

File tree

2 files changed

+6
-25
lines changed

2 files changed

+6
-25
lines changed

docs/debugging_custom_grammars.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,18 @@ We provide a simple script to do this:
9090

9191
```python
9292
from transformers_cfg.parser import parse_ebnf
93-
from transformers_cfg.recognizer import GrammarRecognizer
93+
from transformers_cfg.recognizer import StringRecognizer
9494

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

9999
start_rule_id = parsed_grammar.symbol_table["root"]
100-
recognizer = GrammarRecognizer(parsed_grammar.grammar_encoding, start_rule_id)
100+
recognizer = StringRecognizer(parsed_grammar.grammar_encoding, start_rule_id)
101101

102102
# Test the grammar with a simple input
103103
json_input = '{"foo": "bar", "baz": "bat"}'
104-
is_accepted = recognizer._accept_prefix(json_input, recognizer.stacks)
104+
is_accepted = recognizer._accept_prefix(json_input)
105105
print(is_accepted)
106106
```
107107

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

113113
```python
114114
json_input = '{"foo": "bar"'
115-
is_accepted = recognizer._accept_prefix(json_input, recognizer.stacks)
115+
is_accepted = recognizer._accept_prefix(json_input)
116116
print(is_accepted)
117117
```
118118

transformers_cfg/recognizer.py

+2-21
Original file line numberDiff line numberDiff line change
@@ -444,28 +444,9 @@ def char_acceptance_at_element(self, element_offset):
444444
logging.debug(acceptance)
445445
return acceptance
446446

447-
# def _consume_code_points_new(
448-
# self, code_points: List[int], stacks: Set[Tuple[int]], verbose=False
449-
# ) -> Set[Tuple[int]]:
450-
# new_stacks: Set[Tuple[int]] = set()
451-
# for stack in stacks:
452-
# new_stacks.update(
453-
# self._consume_code_points_per_stack(tuple(code_points), stack, verbose)
454-
# )
455-
# return new_stacks
456-
#
457-
# @lru_cache(maxsize=30000)
458-
# def _consume_code_points_per_stack(
459-
# self, code_points: Tuple[int], stack: Tuple[int], verbose=False
460-
# ) -> Set[Tuple[int]]:
461-
# stacks = {stack}
462-
#
463-
# for code_point in code_points:
464-
# # Update the stacks variable by consuming each code point.
465-
# stacks = self._consume_code_point_for_all_stacks(code_point, (stack,))
466-
#
467-
# return stacks
468447

448+
# backward compatibility, add alias of StringRecognizer to GrammarRecognizer
449+
GrammarRecognizer = StringRecognizer
469450

470451
if __name__ == "__main__":
471452
# set logging level

0 commit comments

Comments
 (0)