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

fix: update func api in debugging_custom_grammar.md #56

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
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